fix: optimize workDir copy cleanup and handle extension mismatch in replaceOriginalFile
Build / Lint & Test (push) Successful in 14s
Build / Lint & Test (push) Successful in 14s
This commit is contained in:
1 parent
07b730c81f
commit
f2e483ca1f
2 files changed
+40
-6
No files matched your search
@@ -37,7 +37,7 @@ const plugin = async (args) => {
|
|||||||
const { inputFileObj, variables, jobLog, updateWorker } = args;
|
const { inputFileObj, variables, jobLog, updateWorker } = args;
|
||||||
const source = inputFileObj._id;
|
const source = inputFileObj._id;
|
||||||
const configuredDestination = variables.originalLibraryFile || args.originalLibraryFile;
|
const configuredDestination = variables.originalLibraryFile || args.originalLibraryFile;
|
||||||
const destination = typeof configuredDestination === 'string'
|
let destination = typeof configuredDestination === 'string'
|
||||||
? configuredDestination
|
? configuredDestination
|
||||||
: configuredDestination && configuredDestination._id;
|
: configuredDestination && configuredDestination._id;
|
||||||
|
|
||||||
@@ -57,8 +57,15 @@ const plugin = async (args) => {
|
|||||||
throw new Error(`[replaceOriginalFile] Destination directory does not exist: ${destinationDir}`);
|
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 temporary = `${destination}.tdarr.tmp`;
|
||||||
const backup = `${destination}.partial.old`;
|
const backup = `${oldOriginalFile}.partial.old`;
|
||||||
let backupCreated = false;
|
let backupCreated = false;
|
||||||
let installed = false;
|
let installed = false;
|
||||||
|
|
||||||
@@ -77,14 +84,18 @@ const plugin = async (args) => {
|
|||||||
label: 'replaceOriginalFile',
|
label: 'replaceOriginalFile',
|
||||||
});
|
});
|
||||||
|
|
||||||
if (fs.existsSync(destination)) {
|
if (fs.existsSync(oldOriginalFile)) {
|
||||||
if (fs.existsSync(backup)) {
|
if (fs.existsSync(backup)) {
|
||||||
fs.unlinkSync(backup);
|
fs.unlinkSync(backup);
|
||||||
}
|
}
|
||||||
fs.renameSync(destination, backup);
|
fs.renameSync(oldOriginalFile, backup);
|
||||||
backupCreated = true;
|
backupCreated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fs.existsSync(destination) && destination !== oldOriginalFile) {
|
||||||
|
try { fs.unlinkSync(destination); } catch (_) { /* ignore */ }
|
||||||
|
}
|
||||||
|
|
||||||
fs.renameSync(temporary, destination);
|
fs.renameSync(temporary, destination);
|
||||||
installed = true;
|
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`);
|
jobLog(`[replaceOriginalFile] Replaced original file: ${destination}\n`);
|
||||||
return {
|
return {
|
||||||
outputNumber: 1,
|
outputNumber: 1,
|
||||||
|
|||||||
@@ -278,6 +278,13 @@ const plugin = async (args) => {
|
|||||||
throw new Error(`[transcode2PassX265] Video-copy/audio-transcode failed with exit code ${cliExitCode}`);
|
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`);
|
jobLog(`[transcode2PassX265] Done. Output: ${outFile}\n`);
|
||||||
return {
|
return {
|
||||||
outputNumber: 1,
|
outputNumber: 1,
|
||||||
@@ -351,11 +358,17 @@ const plugin = async (args) => {
|
|||||||
}
|
}
|
||||||
jobLog('[transcode2PassX265] Pass 2 complete.\n');
|
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) => {
|
[statsFile, `${statsFile}.mbtree`, `${statsFile}.cutree`].forEach((f) => {
|
||||||
try { if (fs.existsSync(f)) fs.unlinkSync(f); } catch (_) { /* ignore */ }
|
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 ────────────────────────────
|
// ── Return transcoded file for Tdarr to manage ────────────────────────────
|
||||||
jobLog(`[transcode2PassX265] Done. Output: ${outFile}\n`);
|
jobLog(`[transcode2PassX265] Done. Output: ${outFile}\n`);
|
||||||
|
|||||||
Reference in new issue
Block a user