@@ -10,7 +10,7 @@ const ProgressBar = require('progress');
1010const BlueBirdPromise = require ( "bluebird" ) ;
1111
1212const logger = require ( '../lib/log' ) ;
13- const { CHUNK_SIZE } = require ( '../lib/constants' ) ;
13+ const { DEFAULT_CHUNK_SIZE , MAX_CHUNK } = require ( '../lib/constants' ) ;
1414const { generateAuthorization, getRegistryInfo } = require ( '../lib/utils' ) ;
1515
1616const { getExistChunks : _getExistChunks , uploadChunk : _uploadChunk , mergeAllChunks : _mergeAllChunks } = require ( '../lib/request' ) ;
@@ -25,14 +25,15 @@ let md5 = '';
2525let uploadId = '' ;
2626let fileSize = 0 ;
2727
28+ let chunkSize = DEFAULT_CHUNK_SIZE ;
29+ let totalChunk = 0 ;
30+
2831process . on ( 'uncaughtException' , error => {
2932 console . log ( chalk . red ( '\n程序发生了一些异常,请稍后重试\n' ) ) ;
3033 logger . error ( error . stack ) ;
3134} )
3235
3336const 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
173174const 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 )
0 commit comments