1 parent
2cf22ef628
commit
febd68973b
4 files changed
+71
-19
No files matched your search
@@ -2,12 +2,19 @@
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const copyWithProgress = async ({ source, destination, jobLog, updateProgress, label }) => {
|
||||
const COPY_BUFFER_SIZE = 4 * 1024 * 1024;
|
||||
|
||||
const copyWithProgress = async ({
|
||||
source,
|
||||
destination,
|
||||
jobLog,
|
||||
updateProgress,
|
||||
label,
|
||||
maxRetries = 2,
|
||||
retryDelayMs = 1000,
|
||||
}) => {
|
||||
const sourceSize = fs.statSync(source).size;
|
||||
const sourceSizeMb = (sourceSize / 1024 / 1024).toFixed(1);
|
||||
const startMs = Date.now();
|
||||
let copiedBytes = 0;
|
||||
let nextProgressPercent = 0;
|
||||
|
||||
const report = (percent, message) => {
|
||||
const boundedPercent = Math.min(Math.max(percent, 0), 100);
|
||||
@@ -20,9 +27,16 @@ const copyWithProgress = async ({ source, destination, jobLog, updateProgress, l
|
||||
if (typeof jobLog === 'function') jobLog(`${message}\n`);
|
||||
};
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const rd = fs.createReadStream(source);
|
||||
const wr = fs.createWriteStream(destination);
|
||||
const copyOnce = () => new Promise((resolve, reject) => {
|
||||
const startMs = Date.now();
|
||||
let copiedBytes = 0;
|
||||
let nextProgressPercent = 0;
|
||||
// Larger buffers avoid issuing thousands of small writes when the
|
||||
// destination is an SMB/CIFS mount with noticeable round-trip latency.
|
||||
const rd = fs.createReadStream(source, { highWaterMark: COPY_BUFFER_SIZE });
|
||||
const wr = fs.createWriteStream(destination, {
|
||||
highWaterMark: COPY_BUFFER_SIZE,
|
||||
});
|
||||
let settled = false;
|
||||
const fail = (error) => {
|
||||
if (settled) return;
|
||||
@@ -37,7 +51,7 @@ const copyWithProgress = async ({ source, destination, jobLog, updateProgress, l
|
||||
wr.on('finish', () => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve();
|
||||
resolve(((Date.now() - startMs) / 1000).toFixed(1));
|
||||
});
|
||||
rd.on('data', (chunk) => {
|
||||
copiedBytes += chunk.length;
|
||||
@@ -57,15 +71,42 @@ const copyWithProgress = async ({ source, destination, jobLog, updateProgress, l
|
||||
rd.pipe(wr);
|
||||
});
|
||||
|
||||
const destinationSize = fs.statSync(destination).size;
|
||||
if (destinationSize !== sourceSize) {
|
||||
throw new Error(
|
||||
`[${label}] Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
|
||||
);
|
||||
let lastError;
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
|
||||
if (attempt > 0) {
|
||||
report(0, `[${label}] Retrying copy (${attempt}/${maxRetries})...`);
|
||||
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
||||
}
|
||||
try {
|
||||
if (fs.existsSync(destination)) fs.unlinkSync(destination);
|
||||
const elapsed = await copyOnce();
|
||||
const destinationSize = fs.statSync(destination).size;
|
||||
if (destinationSize !== sourceSize) {
|
||||
throw new Error(
|
||||
`Copy size mismatch: expected ${sourceSize} bytes, got ${destinationSize}`,
|
||||
);
|
||||
}
|
||||
report(100, `[${label}] Done: ${sourceSizeMb} MB copied in ${elapsed}s`);
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
try {
|
||||
if (fs.existsSync(destination)) fs.unlinkSync(destination);
|
||||
} catch (cleanupError) {
|
||||
lastError = new Error(
|
||||
`${error.message}; could not remove partial copy: ${cleanupError.message}`,
|
||||
);
|
||||
}
|
||||
if (attempt === maxRetries) break;
|
||||
if (typeof jobLog === 'function') {
|
||||
jobLog(`[${label}] Copy attempt ${attempt + 1} failed: ${error.message}\n`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const elapsed = ((Date.now() - startMs) / 1000).toFixed(1);
|
||||
report(100, `[${label}] Done: ${sourceSizeMb} MB copied in ${elapsed}s`);
|
||||
throw new Error(
|
||||
`[${label}] Copy failed after ${maxRetries + 1} attempts: ${lastError.message}`,
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = { copyWithProgress };
|
||||
Reference in new issue
Block a user