fix: optimize workDir copy cleanup and handle extension mismatch in replaceOriginalFile
Build / Lint & Test (push) Successful in 14s

This commit is contained in:
PickleRick committed 2026-09-30 18:35:35 +02:00
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 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,