From fc8e9ebc48913840777a4022abd617718dac2308 Mon Sep 17 00:00:00 2001 From: Joris Bertomeu Date: Mon, 25 Aug 2025 14:48:59 +0200 Subject: [PATCH] Fix relative to missing subtiles issue --- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++--------- package.json | 8 ++++---- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 6f3d4fd..0f14ccf 100644 --- a/index.js +++ b/index.js @@ -334,17 +334,22 @@ const parseMPDStream = async (mpdUrl) => { rootProp: 'SUBTITLES', subProp: 'subs', targetProp: 'subtitles' - }] + }]; + toParse.forEach(({ rootProp, subProp, targetProp }) => { - for (const [key, value] of Object.entries(parsedManifest?.mediaGroups?.[rootProp]?.[subProp])) { - for (let i = 0; i < value.playlists.length; i++) { - obj[targetProp].push({ - name: key, - language: value.language, - attributes: value.playlists[i].attributes - }); + try { + for (const [key, value] of Object.entries(parsedManifest?.mediaGroups?.[rootProp]?.[subProp])) { + for (let i = 0; i < value.playlists.length; i++) { + obj[targetProp].push({ + name: key, + language: value.language, + attributes: value.playlists[i].attributes + }); + } } + } catch(e) { + console.log(`No ${targetProp} found in manifest`); } }); for (let i = 0; i < parsedManifest.playlists.length; i++) { @@ -374,7 +379,35 @@ app.post('/processMPD', async (req, res, next) => { app.use((err, req, res, next) => { res.status(500).json({ error: err.message || err.toString() || 'An error occured' }); -}) +}); + +const safeMove = async (source, destination) => { + try { + const destDir = path.dirname(destination); + if (!fs.existsSync(destDir)) { + fs.mkdirSync(destDir, { recursive: true }); + } + + fs.renameSync(source, destination); + console.log(`✓ Moved: ${path.basename(source)} -> ${destination}`); + + } catch (error) { + if (error.code === 'EXDEV') { + console.log(`⚠️ Cross-device detected, copying: ${path.basename(source)}`); + fs.copyFileSync(source, destination); + fs.unlinkSync(source); + console.log(`✓ Copied: ${path.basename(source)} -> ${destination}`); + + } else if (error.code === 'ENOENT') { + console.error(`❌ Source file not found: ${source}`); + throw new Error(`Source file not found: ${source}`); + + } else { + console.error(`❌ Move failed:`, error); + throw error; + } + } +}; // Processus de la file d'attente videoQueue.process((job) => { @@ -464,11 +497,15 @@ videoQueue.process((job) => { let counter = 1; for (const file of subFiles) { if (file.startsWith(`${mp4Filename}_encrypted`) && file.endsWith('.srt')) { - fs.renameSync(`${workdir}/${file}`, `${finalPath}/${mp4Filename}_${counter}.srt`); + const sourcePath = `${workdir}/${file}`; + const destPath = `${finalPath}/${mp4Filename}_${counter}.srt`; + + await safeMove(sourcePath, destPath); counter++; } } + // Nettoyage (commenté pour correspondre au script original) await runCommand(`rm -fr ${workdir}`); diff --git a/package.json b/package.json index 6f89ace..f47d5c5 100644 --- a/package.json +++ b/package.json @@ -9,11 +9,11 @@ "author": "", "license": "ISC", "dependencies": { - "axios": "^1.7.7", - "bull": "^4.16.4", - "cheerio": "^1.0.0", + "axios": "^1.11.0", + "bull": "^4.16.5", + "cheerio": "^1.1.2", "cors": "^2.8.5", - "express": "^4.21.1", + "express": "^5.1.0", "fs": "^0.0.1-security", "mpd-parser": "^1.3.1", "path": "^0.12.7",