Skip to content

Commit 0c0aa32

Browse files
author
shsgear
committed
feat: 动态分配分块大小保证分块不超过阈值
1 parent e53feb0 commit 0c0aa32

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

bin/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ProgressBar = require('progress');
1010
const BlueBirdPromise = require("bluebird");
1111

1212
const logger = require('../lib/log');
13-
const { CHUNK_SIZE } = require('../lib/constants');
13+
const { DEFAULT_CHUNK_SIZE, MAX_CHUNK } = require('../lib/constants');
1414
const { generateAuthorization, getRegistryInfo } = require('../lib/utils');
1515

1616
const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, mergeAllChunks: _mergeAllChunks } = require('../lib/request');
@@ -25,14 +25,15 @@ let md5 = '';
2525
let uploadId = '';
2626
let fileSize = 0;
2727

28+
let chunkSize = DEFAULT_CHUNK_SIZE;
29+
let totalChunk = 0;
30+
2831
process.on('uncaughtException', error => {
2932
console.log(chalk.red('\n程序发生了一些异常,请稍后重试\n'));
3033
logger.error(error.stack);
3134
})
3235

3336
const upload = async (filePath, parts = []) => {
34-
const totalChunk = Math.ceil(fileSize / CHUNK_SIZE);
35-
3637
const bar = new ProgressBar(':bar [:current/:total] :percent ', { total: totalChunk });
3738
const uploadChunk = async (currentChunk, currentChunkIndex, parts, isRetry) => {
3839
if (parts.some(({ partNumber, size }) => partNumber === currentChunkIndex && size === currentChunk.length)) {
@@ -82,8 +83,8 @@ const upload = async (filePath, parts = []) => {
8283
const chunkIndexs = new Array(totalChunk).fill("").map((_,index) => index+1)
8384

8485
await BlueBirdPromise.map(chunkIndexs,(currentChunkIndex)=>{
85-
const start = (currentChunkIndex - 1) * CHUNK_SIZE;
86-
const end = ((start + CHUNK_SIZE) >= fileSize) ? fileSize : start + CHUNK_SIZE - 1;
86+
const start = (currentChunkIndex - 1) * chunkSize;
87+
const end = ((start + chunkSize) >= fileSize) ? fileSize : start + chunkSize - 1;
8788
const stream = fs.createReadStream(filePath, { start, end })
8889
let buf = [];
8990
return new Promise((resolve) => {
@@ -114,7 +115,7 @@ const upload = async (filePath, parts = []) => {
114115

115116

116117

117-
const merge = async () =>
118+
const merge = async () =>
118119
await _mergeAllChunks(requestUrl, {
119120
version,
120121
uploadId,
@@ -171,15 +172,19 @@ const getFileMD5Success = async (filePath) => {
171172
}
172173

173174
const getFileMD5 = async (filePath) => {
174-
const totalChunk = Math.ceil(fileSize / CHUNK_SIZE);
175+
totalChunk = Math.ceil(fileSize / DEFAULT_CHUNK_SIZE);
176+
if (totalChunk > MAX_CHUNK) {
177+
chunkSize = Math.ceil(fileSize / MAX_CHUNK);
178+
totalChunk = Math.ceil(fileSize / chunkSize);
179+
}
175180
const spark = new SparkMD5.ArrayBuffer();
176181
try {
177182
console.log(`\n开始计算 MD5\n`)
178183
logger.info('开始计算 MD5')
179184

180185
const bar = new ProgressBar(':bar [:current/:total] :percent ', { total: totalChunk });
181186
await new Promise(resolve => {
182-
stream = fs.createReadStream(filePath, { highWaterMark: CHUNK_SIZE })
187+
stream = fs.createReadStream(filePath, { highWaterMark: chunkSize })
183188
stream.on('data', chunk => {
184189
bar.tick();
185190
spark.append(chunk)

lib/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2-
CHUNK_SIZE: 1024 * 1024 * 5
2+
DEFAULT_CHUNK_SIZE: 5 * 1024 * 1024,
3+
MAX_CHUNK: 10000,
34
}

lib/request.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const uploadChunk = (requestUrl, {
4242
Authorization
4343
}) => {
4444
return axios({
45+
maxContentLength: Infinity,
46+
maxBodyLength: Infinity,
4547
method: 'post',
4648
url: `${requestUrl}?version=${version}&uploadId=${uploadId}&partNumber=${partNumber}&size=${size}&action=part-upload`,
4749
data: currentChunk,

lib/withRetry.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const withRetry = (fn, retryCount, retryDelay) => new Promise((resolve, reject)
1515
.then(resolve)
1616
.catch((err) => {
1717
if (retryCount > 0) {
18+
logger.error(err);
1819
console.log(chalk.cyan('遇到了一个小问题,重试中...'))
1920
return delay(retryDelay)
2021
.then(withRetry.bind(null, fn, retryCount - 1, retryDelay))

0 commit comments

Comments
 (0)