From ccb73807c09807d56513a07524d1be85358d76bc Mon Sep 17 00:00:00 2001 From: Raphix Date: Mon, 6 Oct 2025 23:30:41 +0200 Subject: [PATCH] Version 1.3.1-rc1 - Modification --- src/media/MediaInformation.js | 49 ++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/media/MediaInformation.js b/src/media/MediaInformation.js index 7cbefca..b8b4466 100644 --- a/src/media/MediaInformation.js +++ b/src/media/MediaInformation.js @@ -6,27 +6,52 @@ const path = require('path'); const fs = require('fs'); async function getMediaInformation(instance, media, provider) { + let tmpFile = null; + try { - const info = await ffprobe(media.attachment.url, { path: ffprobeStatic.path }); + // 1. Vérifier si ./tmp existe, sinon le créer + const tmpDir = path.resolve("./tmp"); + if (!fs.existsSync(tmpDir)) { + fs.mkdirSync(tmpDir, { recursive: true }); + } + + // 2. Télécharger le fichier depuis l'URL + const url = media.attachment.url; + const res = await fetch(url); + if (!res.ok) throw new Error(`Erreur HTTP ${res.status}`); + + const buffer = Buffer.from(await res.arrayBuffer()); + tmpFile = path.join(tmpDir, `${Date.now()}-${media.attachment.name}`); + fs.writeFileSync(tmpFile, buffer); + + // 3. Lancer ffprobe sur le fichier local + const info = await ffprobe(tmpFile, { path: ffprobeStatic.path }); + + // 4. Récupérer les informations if (info.streams?.[0]?.duration_ts) { instance.duration = info.streams[0].duration; - instance.readduration = getReadableDuration(instance.duration) - } - - // Vérification pour éviter une erreur si `streams[0]` ou `tags` n'existe pas - instance.thumbnail = info.streams?.[0]?.tags?.thumbnail ?? + instance.readduration = getReadableDuration(instance.duration); + } + + instance.thumbnail = info.streams?.[0]?.tags?.thumbnail ?? "https://radomisol.fr/wp-content/uploads/2016/08/cropped-note-radomisol-musique.png"; - - // Obtenir le titre (sinon utiliser le nom du fichier) instance.title = info.streams?.[0]?.tags?.title ?? media.attachment.name; - - // Obtenir l'auteur (s'il existe) instance.author = info.streams?.[0]?.tags?.artist ?? instance.author; + return true; } catch (err) { - clog.error("Impossible de récupérer les informations de la musique : " + media.attachment.name) - clog.error(err) + clog.error("Impossible de récupérer les informations de la musique : " + media.attachment.name); + console.error(err); return null; + } finally { + // 5. Nettoyage : supprimer le fichier temporaire + if (tmpFile && fs.existsSync(tmpFile)) { + try { + fs.unlinkSync(tmpFile); + } catch (e) { + console.error("Erreur lors du unlink:", e); + } + } } }