From 403e900ce678ef36e11cb6ab2f8929a2f5885d1c Mon Sep 17 00:00:00 2001 From: unfr Date: Thu, 7 Aug 2025 16:13:22 +0200 Subject: [PATCH] ajout du support des bdinfo au niveau du lien mediainfo --- autopost/server.js | 48 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/autopost/server.js b/autopost/server.js index 1ab3eff..0a1cd8f 100644 --- a/autopost/server.js +++ b/autopost/server.js @@ -490,21 +490,43 @@ autopostRouter.get('/mediainfo', (req, res) => { if (!filename) { return res.status(400).json({ error: "Nom du fichier non spécifié." }); } - let base = filename.includes('.') ? filename.split('.').slice(0, -1).join('.') : filename; - const mediainfoFilePath = path.join(config.infodirectory, base + '.json'); - fs.readFile(mediainfoFilePath, 'utf8', (err, data) => { - if (err) { - console.error(err.message); - return res.status(500).json({ error: "Erreur lors du chargement du fichier mediainfo." }); - } - try { - const obj = JSON.parse(data); - const pretty = JSON.stringify(obj, null, 2); - res.json({ content: pretty }); - } catch(e) { - res.json({ content: data }); + const base = filename.includes('.') ? filename.split('.').slice(0, -1).join('.') : filename; + const jsonPath = path.join(config.infodirectory, base + '.json'); + const bdinfoPath = path.join(config.infodirectory, base + '.bdinfo.txt'); + + // Vérifie lequel des deux existe, priorité au .json + fs.access(jsonPath, fs.constants.F_OK, (errJson) => { + if (!errJson) { + // .json existe + readAndSend(jsonPath); + } else { + // .json n'existe pas, on essaie .bdinfo.txt + fs.access(bdinfoPath, fs.constants.F_OK, (errBdinfo) => { + if (!errBdinfo) { + readAndSend(bdinfoPath); + } else { + res.status(404).json({ error: "Aucun fichier mediainfo trouvé." }); + } + }); } }); + + function readAndSend(filePath) { + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error(err.message); + return res.status(500).json({ error: "Erreur lors du chargement du fichier mediainfo." }); + } + // Essaie de prettifier si JSON + try { + const obj = JSON.parse(data); + const pretty = JSON.stringify(obj, null, 2); + res.json({ content: pretty }); + } catch (e) { + res.json({ content: data }); + } + }); + } }); // --------------------------- Download -----------------------------