@@ -17,16 +17,16 @@ const proto = exports;
17
17
* @param {String } name
18
18
* @param {String|File } file
19
19
* @param {Object } options
20
- * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
21
- * {String} options.callback.url the OSS sends a callback request to this URL
22
- * {String} options.callback.host The host header value for initiating callback requests
23
- * {String} options.callback.body The value of the request body when a callback is initiated
24
- * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
25
- * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
26
- * customValue = {
27
- * key1: 'value1',
28
- * key2: 'value2'
29
- * }
20
+ * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
21
+ * {String} options.callback.url the OSS sends a callback request to this URL
22
+ * {String} options.callback.host The host header value for initiating callback requests
23
+ * {String} options.callback.body The value of the request body when a callback is initiated
24
+ * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
25
+ * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
26
+ * customValue = {
27
+ * key1: 'value1',
28
+ * key2: 'value2'
29
+ * }
30
30
*/
31
31
proto . multipartUpload = function * multipartUpload ( name , file , options ) {
32
32
this . resetCancelFlag ( ) ;
@@ -107,7 +107,7 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
107
107
const partOffs = this . _divideParts ( fileSize , partSize ) ;
108
108
const numParts = partOffs . length ;
109
109
110
- const uploadPartJob = function * ( self , partNo ) {
110
+ const uploadPartJob = function * uploadPartJob ( self , partNo ) {
111
111
if ( ! self . isCancel ( ) ) {
112
112
try {
113
113
const pi = partOffs [ partNo - 1 ] ;
@@ -117,18 +117,23 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
117
117
} ;
118
118
119
119
const result = yield self . _uploadPart ( name , uploadId , partNo , data ) ;
120
- doneParts . push ( {
121
- number : partNo ,
122
- etag : result . res . headers . etag ,
123
- } ) ;
124
- checkpoint . doneParts = doneParts ;
125
-
126
- if ( ! self . isCancel ( ) && options && options . progress ) {
127
- yield options . progress ( doneParts . length / numParts , checkpoint , result . res ) ;
120
+ if ( ! self . isCancel ( ) ) {
121
+ doneParts . push ( {
122
+ number : partNo ,
123
+ etag : result . res . headers . etag ,
124
+ } ) ;
125
+ checkpoint . doneParts = doneParts ;
126
+
127
+ if ( options && options . progress ) {
128
+ yield options . progress ( doneParts . length / numParts , checkpoint , result . res ) ;
129
+ }
128
130
}
129
131
} catch ( err ) {
130
- err . partNum = partNo ;
131
- throw err ;
132
+ if ( ! self . isCancel ( ) ) {
133
+ self . cancel ( ) ;
134
+ err . partNum = partNo ;
135
+ throw err ;
136
+ }
132
137
}
133
138
}
134
139
} ;
@@ -156,24 +161,25 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
156
161
// start uploads jobs
157
162
const errors = yield this . _thunkPool ( jobs , parallel ) ;
158
163
159
- if ( this . isCancel ( ) ) {
160
- jobs = null ;
161
- throw this . _makeCancelEvent ( ) ;
162
- }
163
-
164
164
// check errors after all jobs are completed
165
165
if ( errors && errors . length > 0 ) {
166
+ this . resetCancelFlag ( ) ;
166
167
const err = errors [ 0 ] ;
167
168
err . message = `Failed to upload some parts with error: ${ err . toString ( ) } part_num: ${ err . partNum } ` ;
168
169
throw err ;
169
170
}
171
+
172
+ if ( this . isCancel ( ) ) {
173
+ jobs = null ;
174
+ throw this . _makeCancelEvent ( ) ;
175
+ }
170
176
}
171
177
return yield this . completeMultipartUpload ( name , uploadId , doneParts , options ) ;
172
178
} ;
173
179
174
180
175
- is . file = function ( file ) {
176
- return typeof ( File ) !== 'undefined' && file instanceof File ;
181
+ is . file = function file ( obj ) {
182
+ return typeof ( File ) !== 'undefined' && obj instanceof File ;
177
183
} ;
178
184
179
185
/**
@@ -241,7 +247,7 @@ WebFileReadStream.prototype._read = function _read(size) {
241
247
size = size || defaultReadSize ;
242
248
243
249
const that = this ;
244
- this . reader . onload = function ( e ) {
250
+ this . reader . onload = function onload ( e ) {
245
251
that . fileBuffer = new Buffer ( new Uint8Array ( e . target . result ) ) ;
246
252
that . file = null ;
247
253
that . readFileAndPush ( size ) ;
@@ -270,7 +276,7 @@ proto._createStream = function _createStream(file, start, end) {
270
276
271
277
proto . _getPartSize = function _getPartSize ( fileSize , partSize ) {
272
278
const maxNumParts = 10 * 1000 ;
273
- const defaultPartSize = 1 * 1024 * 1024 ;
279
+ const defaultPartSize = 1024 * 1024 ;
274
280
275
281
if ( ! partSize ) {
276
282
return defaultPartSize ;
@@ -300,10 +306,9 @@ proto._divideParts = function _divideParts(fileSize, partSize) {
300
306
} ;
301
307
302
308
// cancel is not error , so create an object
303
- proto . _makeCancelEvent = function ( ) {
304
- const cancelEvent = {
309
+ proto . _makeCancelEvent = function _makeCancelEvent ( ) {
310
+ return {
305
311
status : 0 ,
306
312
name : 'cancel' ,
307
313
} ;
308
- return cancelEvent ;
309
314
} ;
0 commit comments