Fix copy progress, remove v2.0.0, fix subtitle default naming, enforce strict no-upcoding on audio
Build / Lint & Test (push) Successful in 3m15s

This commit is contained in:
PickleRick committed 2026-09-30 17:26:19 +02:00
1 parent 7ee5a5bc48
commit 86448988ed
12 files changed
+470 -348

No files matched your search

+146 -53
View File
@@ -1,27 +1,70 @@
'use strict';
const fs = require('fs');
const fs = require('fs');
const path = require('path');
const MAX_RETRIES = 2;
const RETRY_DELAY_MS = 1000;
const PROGRESS_INTERVAL_MS = 250;
const LOG_INTERVAL_MS = 3000;
const sleep = (milliseconds) => new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
const copyWithProgress = async ({ source, destination, jobLog, updateProgress, label }) => {
const sourceSize = fs.statSync(source).size;
const sourceSizeMb = sourceSize / 1024 / 1024;
let lastError;
const formatEta = (seconds) => {
if (!Number.isFinite(seconds) || seconds < 0) return '00:00:00';
const hrs = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
return `${String(hrs).padStart(2, '0')}:${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
};
const report = (progress, message) => {
/**
* Copies a file from source to destination with real-time stream-based
* progress reporting, speed / ETA tracking, Tdarr worker updates, size verification,
* and automatic retry with cleanup.
*/
const copyWithProgress = async ({
source,
destination,
jobLog,
updateWorker,
updateProgress,
label = 'copy',
}) => {
if (!fs.existsSync(source)) {
throw new Error(`[${label}] Source file not found: ${source}`);
}
const sourceStat = fs.statSync(source);
const sourceSize = sourceStat.size;
const sourceSizeMb = sourceSize / 1024 / 1024;
const destDir = path.dirname(destination);
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') {
updateWorker({
percentage: Math.min(percent, 100),
ETA: eta || '00:00:00',
});
}
if (typeof updateProgress === 'function') {
updateProgress({ progress, jobLog: message });
updateProgress({
progress: Math.min(percent, 100),
jobLog: message,
});
}
if (message && typeof jobLog === 'function') {
jobLog(`${message}\n`);
}
if (typeof jobLog === 'function') jobLog(`${message}\n`);
};
let lastError;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
if (attempt > 0) {
report(0, `[${label}] Retrying copy (${attempt}/${MAX_RETRIES})...`);
@@ -29,49 +72,98 @@ const copyWithProgress = async ({ source, destination, jobLog, updateProgress, l
}
try {
if (fs.existsSync(destination)) fs.unlinkSync(destination);
const startedAt = Date.now();
let lastReportedPercent = -1;
let previousBytes = 0;
let previousAt = startedAt;
let copying = true;
const progressTimer = setInterval(() => {
if (!copying || !fs.existsSync(destination)) return;
let copiedBytes;
try {
copiedBytes = fs.statSync(destination).size;
} catch (error) {
if (error.code === 'ENOENT') return;
return;
}
const percent = sourceSize === 0
? 99
: Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
if (percent <= lastReportedPercent) return;
const now = Date.now();
const intervalSeconds = Math.max((now - previousAt) / 1000, 0.001);
const intervalSpeed = (copiedBytes - previousBytes) / 1024 / 1024 / intervalSeconds;
report(
percent,
`[${label}] Progress: ${percent}% `
+ `(${(copiedBytes / 1024 / 1024).toFixed(1)}/${sourceSizeMb.toFixed(1)} MB, `
+ `${intervalSpeed.toFixed(1)} MB/s)`,
);
lastReportedPercent = percent;
previousBytes = copiedBytes;
previousAt = now;
}, PROGRESS_INTERVAL_MS);
try {
await fs.promises.copyFile(source, destination);
} finally {
copying = false;
clearInterval(progressTimer);
if (fs.existsSync(destination)) {
fs.unlinkSync(destination);
}
// Special case: empty file (0 bytes)
if (sourceSize === 0) {
fs.writeFileSync(destination, Buffer.alloc(0));
report(100, `[${label}] Copied 0-byte file successfully.`, '00:00:00');
return;
}
const startedAt = Date.now();
let copiedBytes = 0;
let lastReportedPercent = -1;
let lastLoggedPercent = -10;
let lastLoggedTime = startedAt;
let intervalBytes = 0;
let intervalTime = startedAt;
await new Promise((resolve, reject) => {
const rd = fs.createReadStream(source, { highWaterMark: 1024 * 1024 });
const wr = fs.createWriteStream(destination, { highWaterMark: 1024 * 1024 });
rd.on('error', (err) => {
wr.destroy();
reject(err);
});
wr.on('error', (err) => {
rd.destroy();
reject(err);
});
wr.on('finish', resolve);
rd.on('data', (chunk) => {
copiedBytes += chunk.length;
intervalBytes += chunk.length;
const now = Date.now();
const percent = Math.min(Math.floor((copiedBytes / sourceSize) * 100), 99);
// Calculate ETA and speed
const totalElapsedSec = Math.max((now - startedAt) / 1000, 0.001);
const avgSpeedBps = copiedBytes / totalElapsedSec;
const remainingBytes = Math.max(0, sourceSize - copiedBytes);
const remainingSec = avgSpeedBps > 0 ? remainingBytes / avgSpeedBps : 0;
const eta = formatEta(remainingSec);
// Update Tdarr UI on every percentage increase
if (percent > lastReportedPercent) {
if (typeof updateWorker === 'function') {
updateWorker({
percentage: percent,
ETA: eta,
});
}
if (typeof updateProgress === 'function') {
updateProgress({ progress: percent });
}
lastReportedPercent = percent;
}
// Throttle jobLog output: every 10% or every LOG_INTERVAL_MS
const timeSinceLastLog = now - lastLoggedTime;
if (percent >= lastLoggedPercent + 10 || (timeSinceLastLog >= LOG_INTERVAL_MS && percent !== lastLoggedPercent)) {
const intSec = Math.max((now - intervalTime) / 1000, 0.001);
const currentSpeedMb = (intervalBytes / 1024 / 1024 / intSec).toFixed(1);
const copiedMb = (copiedBytes / 1024 / 1024).toFixed(1);
if (typeof jobLog === 'function') {
jobLog(
`[${label}] Progress: ${percent}% `
+ `(${copiedMb}/${sourceSizeMb.toFixed(1)} MB, `
+ `${currentSpeedMb} MB/s, ETA: ${eta})\n`,
);
}
lastLoggedPercent = percent;
lastLoggedTime = now;
intervalBytes = 0;
intervalTime = now;
}
});
rd.pipe(wr);
});
// Verify destination size
if (!fs.existsSync(destination)) {
throw new Error(`Destination file does not exist after copy: ${destination}`);
}
const destinationSize = fs.statSync(destination).size;
if (destinationSize !== sourceSize) {
throw new Error(
@@ -79,12 +171,13 @@ const copyWithProgress = async ({ source, destination, jobLog, updateProgress, l
);
}
const elapsedSeconds = Math.max((Date.now() - startedAt) / 1000, 0.001);
const averageSpeed = sourceSize / 1024 / 1024 / elapsedSeconds;
const totalElapsed = Math.max((Date.now() - startedAt) / 1000, 0.001);
const avgSpeed = (sourceSizeMb / totalElapsed).toFixed(1);
report(
100,
`[${label}] Done: ${sourceSizeMb.toFixed(1)} MB copied in `
+ `${elapsedSeconds.toFixed(1)}s (${averageSpeed.toFixed(1)} MB/s average)`,
+ `${totalElapsed.toFixed(1)}s (${avgSpeed} MB/s average)`,
'00:00:00',
);
return;
} catch (error) {