diff --git a/LocalFlowPlugins/utility/replaceOriginalFile/1.0.0/index.js b/LocalFlowPlugins/utility/replaceOriginalFile/1.0.0/index.js index 0879c0d..464a075 100644 --- a/LocalFlowPlugins/utility/replaceOriginalFile/1.0.0/index.js +++ b/LocalFlowPlugins/utility/replaceOriginalFile/1.0.0/index.js @@ -37,7 +37,7 @@ const plugin = async (args) => { const { inputFileObj, variables, jobLog, updateWorker } = args; const source = inputFileObj._id; const configuredDestination = variables.originalLibraryFile || args.originalLibraryFile; - const destination = typeof configuredDestination === 'string' + let destination = typeof configuredDestination === 'string' ? configuredDestination : configuredDestination && configuredDestination._id; @@ -57,8 +57,15 @@ const plugin = async (args) => { throw new Error(`[replaceOriginalFile] Destination directory does not exist: ${destinationDir}`); } + const sourceExt = path.extname(source); + const destExt = path.extname(destination); + const oldOriginalFile = destination; + if (sourceExt && destExt && sourceExt.toLowerCase() !== destExt.toLowerCase()) { + destination = path.join(destinationDir, `${path.basename(destination, destExt)}${sourceExt}`); + } + const temporary = `${destination}.tdarr.tmp`; - const backup = `${destination}.partial.old`; + const backup = `${oldOriginalFile}.partial.old`; let backupCreated = false; let installed = false; @@ -77,14 +84,18 @@ const plugin = async (args) => { label: 'replaceOriginalFile', }); - if (fs.existsSync(destination)) { + if (fs.existsSync(oldOriginalFile)) { if (fs.existsSync(backup)) { fs.unlinkSync(backup); } - fs.renameSync(destination, backup); + fs.renameSync(oldOriginalFile, backup); backupCreated = true; } + if (fs.existsSync(destination) && destination !== oldOriginalFile) { + try { fs.unlinkSync(destination); } catch (_) { /* ignore */ } + } + fs.renameSync(temporary, destination); installed = true; @@ -104,6 +115,16 @@ const plugin = async (args) => { } } + // Clean up temporary workDir copy (_old) if still present + if (variables.workDirCopyApplied === 'true') { + const workDir = path.dirname(source); + const oldBase = variables.workDirSourceBaseName || path.basename(destination, sourceExt); + const oldCandidate = path.join(workDir, `${oldBase}_old${destExt}`); + if (fs.existsSync(oldCandidate)) { + try { fs.unlinkSync(oldCandidate); } catch (_) { /* ignore */ } + } + } + jobLog(`[replaceOriginalFile] Replaced original file: ${destination}\n`); return { outputNumber: 1, diff --git a/LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js b/LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js index 6f6eb41..945d22f 100644 --- a/LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js +++ b/LocalFlowPlugins/video/transcode2PassX265/1.0.0/index.js @@ -278,6 +278,13 @@ const plugin = async (args) => { throw new Error(`[transcode2PassX265] Video-copy/audio-transcode failed with exit code ${cliExitCode}`); } + if (variables.workDirCopyApplied === 'true' && fs.existsSync(inputFile)) { + try { + fs.unlinkSync(inputFile); + jobLog(`[transcode2PassX265] Cleaned up temporary workDir copy: ${path.basename(inputFile)}\n`); + } catch (_) { /* ignore */ } + } + jobLog(`[transcode2PassX265] Done. Output: ${outFile}\n`); return { outputNumber: 1, @@ -351,11 +358,17 @@ const plugin = async (args) => { } jobLog('[transcode2PassX265] Pass 2 complete.\n'); - // ── Cleanup 2-pass log files ────────────────────────────────────────────── + // ── Cleanup 2-pass log files and temporary input copy ──────────────────── [statsFile, `${statsFile}.mbtree`, `${statsFile}.cutree`].forEach((f) => { try { if (fs.existsSync(f)) fs.unlinkSync(f); } catch (_) { /* ignore */ } }); - jobLog('[transcode2PassX265] Temp log files cleaned up.\n'); + if (variables.workDirCopyApplied === 'true' && fs.existsSync(inputFile)) { + try { + fs.unlinkSync(inputFile); + jobLog(`[transcode2PassX265] Cleaned up temporary workDir copy: ${path.basename(inputFile)}\n`); + } catch (_) { /* ignore */ } + } + jobLog('[transcode2PassX265] Temp files cleaned up.\n'); // ── Return transcoded file for Tdarr to manage ──────────────────────────── jobLog(`[transcode2PassX265] Done. Output: ${outFile}\n`);