修复问题3

This commit is contained in:
CN-JS-HuiBai
2026-04-03 23:27:56 +08:00
parent 2772ff22b8
commit bfef6f651d
2 changed files with 12 additions and 0 deletions

View File

@@ -392,6 +392,8 @@ const ensureSourceCached = async ({ s3Client, bucket, key, targetPath, onProgres
const s3Stream = response.Body;
const totalBytes = response.ContentLength || 0;
let downloadedBytes = 0;
let lastLoggedPercent = -1;
let lastLoggedBytes = 0;
logger.log(`[download] response received bucket=${bucket} key=${key} totalBytes=${totalBytes || 0}`);
@@ -403,6 +405,14 @@ const ensureSourceCached = async ({ s3Client, bucket, key, targetPath, onProgres
const writeStream = fs.createWriteStream(targetPath);
s3Stream.on('data', (chunk) => {
downloadedBytes += chunk.length;
const percent = totalBytes ? Math.min(100, Math.round((downloadedBytes / totalBytes) * 100)) : 0;
const shouldLogPercent = totalBytes && percent >= lastLoggedPercent + 5;
const shouldLogBytes = !totalBytes && downloadedBytes >= lastLoggedBytes + (5 * 1024 * 1024);
if (shouldLogPercent || shouldLogBytes) {
logger.log(`[download] progress bucket=${bucket} key=${key} downloadedBytes=${downloadedBytes} totalBytes=${totalBytes || 0} percent=${percent}`);
lastLoggedPercent = percent;
lastLoggedBytes = downloadedBytes;
}
if (typeof onProgress === 'function') {
onProgress({ totalBytes, downloadedBytes, cacheExists: false });
}