修复问题3
This commit is contained in:
10
server.js
10
server.js
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user