Version 1.3.1-rc1 - Modification
This commit is contained in:
@@ -6,27 +6,52 @@ const path = require('path');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
async function getMediaInformation(instance, media, provider) {
|
async function getMediaInformation(instance, media, provider) {
|
||||||
|
let tmpFile = null;
|
||||||
|
|
||||||
try {
|
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) {
|
if (info.streams?.[0]?.duration_ts) {
|
||||||
instance.duration = info.streams[0].duration;
|
instance.duration = info.streams[0].duration;
|
||||||
instance.readduration = getReadableDuration(instance.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.thumbnail = info.streams?.[0]?.tags?.thumbnail ??
|
|
||||||
"https://radomisol.fr/wp-content/uploads/2016/08/cropped-note-radomisol-musique.png";
|
"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;
|
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;
|
instance.author = info.streams?.[0]?.tags?.artist ?? instance.author;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
clog.error("Impossible de récupérer les informations de la musique : " + media.attachment.name)
|
clog.error("Impossible de récupérer les informations de la musique : " + media.attachment.name);
|
||||||
clog.error(err)
|
console.error(err);
|
||||||
return null;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user