@@ -10,7 +10,7 @@ const ProgressBar = require('progress');
10
10
const BlueBirdPromise = require ( "bluebird" ) ;
11
11
12
12
const logger = require ( '../lib/log' ) ;
13
- const { CHUNK_SIZE } = require ( '../lib/constants' ) ;
13
+ const { DEFAULT_CHUNK_SIZE , MAX_CHUNK } = require ( '../lib/constants' ) ;
14
14
const { generateAuthorization, getRegistryInfo } = require ( '../lib/utils' ) ;
15
15
16
16
const { getExistChunks : _getExistChunks , uploadChunk : _uploadChunk , mergeAllChunks : _mergeAllChunks } = require ( '../lib/request' ) ;
@@ -25,14 +25,15 @@ let md5 = '';
25
25
let uploadId = '' ;
26
26
let fileSize = 0 ;
27
27
28
+ let chunkSize = DEFAULT_CHUNK_SIZE ;
29
+ let totalChunk = 0 ;
30
+
28
31
process . on ( 'uncaughtException' , error => {
29
32
console . log ( chalk . red ( '\n程序发生了一些异常,请稍后重试\n' ) ) ;
30
33
logger . error ( error . stack ) ;
31
34
} )
32
35
33
36
const upload = async ( filePath , parts = [ ] ) => {
34
- const totalChunk = Math . ceil ( fileSize / CHUNK_SIZE ) ;
35
-
36
37
const bar = new ProgressBar ( ':bar [:current/:total] :percent ' , { total : totalChunk } ) ;
37
38
const uploadChunk = async ( currentChunk , currentChunkIndex , parts , isRetry ) => {
38
39
if ( parts . some ( ( { partNumber, size } ) => partNumber === currentChunkIndex && size === currentChunk . length ) ) {
@@ -82,8 +83,8 @@ const upload = async (filePath, parts = []) => {
82
83
const chunkIndexs = new Array ( totalChunk ) . fill ( "" ) . map ( ( _ , index ) => index + 1 )
83
84
84
85
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 ;
87
88
const stream = fs . createReadStream ( filePath , { start, end } )
88
89
let buf = [ ] ;
89
90
return new Promise ( ( resolve ) => {
@@ -114,7 +115,7 @@ const upload = async (filePath, parts = []) => {
114
115
115
116
116
117
117
- const merge = async ( ) =>
118
+ const merge = async ( ) =>
118
119
await _mergeAllChunks ( requestUrl , {
119
120
version,
120
121
uploadId,
@@ -171,15 +172,19 @@ const getFileMD5Success = async (filePath) => {
171
172
}
172
173
173
174
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
+ }
175
180
const spark = new SparkMD5 . ArrayBuffer ( ) ;
176
181
try {
177
182
console . log ( `\n开始计算 MD5\n` )
178
183
logger . info ( '开始计算 MD5' )
179
184
180
185
const bar = new ProgressBar ( ':bar [:current/:total] :percent ' , { total : totalChunk } ) ;
181
186
await new Promise ( resolve => {
182
- stream = fs . createReadStream ( filePath , { highWaterMark : CHUNK_SIZE } )
187
+ stream = fs . createReadStream ( filePath , { highWaterMark : chunkSize } )
183
188
stream . on ( 'data' , chunk => {
184
189
bar . tick ( ) ;
185
190
spark . append ( chunk )
0 commit comments