1
+ var session = require ( './session' ) ;
1
2
var Async = require ( './async' ) ;
2
3
var EventProxy = require ( './event' ) . EventProxy ;
3
4
var util = require ( './util' ) ;
@@ -41,13 +42,13 @@ function sliceUploadFile(params, callback) {
41
42
SliceList : UploadData . SliceList ,
42
43
} , function ( err , data ) {
43
44
if ( ! self . _isRunningTask ( TaskId ) ) return ;
44
- delete uploadIdUsing [ UploadData . UploadId ] ;
45
+ session . removeUsing ( UploadData . UploadId ) ;
45
46
if ( err ) {
46
47
onProgress ( null , true ) ;
47
48
return ep . emit ( 'error' , err ) ;
48
49
}
50
+ session . removeUploadId ( UploadData . UploadId ) ;
49
51
onProgress ( { loaded : FileSize , total : FileSize } , true ) ;
50
- removeUploadId . call ( self , UploadData . UploadId ) ;
51
52
ep . emit ( 'upload_complete' , data ) ;
52
53
} ) ;
53
54
} ) ;
@@ -56,14 +57,9 @@ function sliceUploadFile(params, callback) {
56
57
ep . on ( 'get_upload_data_finish' , function ( UploadData ) {
57
58
58
59
// 处理 UploadId 缓存
59
- var uuid = util . getFileUUID ( Body , params . ChunkSize ) ;
60
- uuid && setUploadId . call ( self , uuid , UploadData . UploadId ) ; // 缓存 UploadId
61
- uploadIdUsing [ UploadData . UploadId ] = true ; // 标记 UploadId 为正在使用
62
- TaskId && self . on ( 'inner-kill-task' , function ( data ) {
63
- if ( data . TaskId === TaskId && data . toState === 'canceled' ) {
64
- delete uploadIdUsing [ UploadData . UploadId ] ; // 去除 UploadId 正在使用的标记
65
- }
66
- } ) ;
60
+ var uuid = session . getFileId ( Body , params . ChunkSize , Bucket , Key ) ;
61
+ uuid && session . saveUploadId ( uuid , UploadData . UploadId , self . options . UploadIdCacheLimit ) ; // 缓存 UploadId
62
+ session . setUsing ( UploadData . UploadId ) ; // 标记 UploadId 为正在使用
67
63
68
64
// 获取 UploadId
69
65
onProgress ( null , true ) ; // 任务状态开始 uploading
@@ -157,75 +153,6 @@ function sliceUploadFile(params, callback) {
157
153
158
154
}
159
155
160
-
161
- // 按照文件特征值,缓存 UploadId
162
- var uploadIdCache ;
163
- var uploadIdUsing = { } ;
164
- var uploadIdCacheKey = 'cos_sdk_upload_cache' ;
165
- function initUploadId ( ) {
166
- var cacheLimit = this . options . UploadIdCacheLimit ;
167
- if ( ! uploadIdCache ) {
168
- if ( cacheLimit ) {
169
- try {
170
- uploadIdCache = JSON . parse ( localStorage . getItem ( uploadIdCacheKey ) ) || [ ] ;
171
- } catch ( e ) { }
172
- }
173
- if ( ! uploadIdCache ) {
174
- uploadIdCache = [ ] ;
175
- }
176
- }
177
- }
178
- function setUploadId ( uuid , UploadId , isDisabled ) {
179
- initUploadId . call ( this ) ;
180
- for ( var i = uploadIdCache . length - 1 ; i >= 0 ; i -- ) {
181
- if ( uploadIdCache [ i ] [ 0 ] === uuid && uploadIdCache [ i ] [ 1 ] === UploadId ) {
182
- uploadIdCache . splice ( i , 1 ) ;
183
- }
184
- }
185
- uploadIdCache . unshift ( [ uuid , UploadId ] ) ;
186
- var cacheLimit = this . options . UploadIdCacheLimit ;
187
- if ( uploadIdCache . length > cacheLimit ) {
188
- uploadIdCache . splice ( cacheLimit ) ;
189
- }
190
- cacheLimit && setTimeout ( function ( ) {
191
- try {
192
- localStorage . setItem ( uploadIdCacheKey , JSON . stringify ( uploadIdCache ) ) ;
193
- } catch ( e ) { }
194
- } ) ;
195
- }
196
- function removeUploadId ( UploadId ) {
197
- initUploadId . call ( this ) ;
198
- delete uploadIdUsing [ UploadId ] ;
199
- for ( var i = uploadIdCache . length - 1 ; i >= 0 ; i -- ) {
200
- if ( uploadIdCache [ i ] [ 1 ] === UploadId ) {
201
- uploadIdCache . splice ( i , 1 )
202
- }
203
- }
204
- var cacheLimit = this . options . UploadIdCacheLimit ;
205
- if ( uploadIdCache . length > cacheLimit ) {
206
- uploadIdCache . splice ( cacheLimit ) ;
207
- }
208
- cacheLimit && setTimeout ( function ( ) {
209
- try {
210
- if ( uploadIdCache . length ) {
211
- localStorage . setItem ( uploadIdCacheKey , JSON . stringify ( uploadIdCache ) ) ;
212
- } else {
213
- localStorage . removeItem ( uploadIdCacheKey ) ;
214
- }
215
- } catch ( e ) { }
216
- } ) ;
217
- }
218
- function getUploadId ( uuid ) {
219
- initUploadId . call ( this ) ;
220
- var CacheUploadIdList = [ ] ;
221
- for ( var i = 0 ; i < uploadIdCache . length ; i ++ ) {
222
- if ( uploadIdCache [ i ] [ 0 ] === uuid ) {
223
- CacheUploadIdList . push ( uploadIdCache [ i ] [ 1 ] ) ;
224
- }
225
- }
226
- return CacheUploadIdList . length ? CacheUploadIdList : null ;
227
- }
228
-
229
156
// 获取上传任务的 UploadId
230
157
function getUploadIdAndPartList ( params , callback ) {
231
158
var TaskId = params . TaskId ;
@@ -316,7 +243,7 @@ function getUploadIdAndPartList(params, callback) {
316
243
} ) ;
317
244
318
245
// 存在 UploadId
319
- ep . on ( 'upload_id_ready ' , function ( UploadData ) {
246
+ ep . on ( 'upload_id_available ' , function ( UploadData ) {
320
247
// 转换成 map
321
248
var map = { } ;
322
249
var list = [ ] ;
@@ -364,18 +291,18 @@ function getUploadIdAndPartList(params, callback) {
364
291
if ( ! UploadId ) {
365
292
return callback ( { Message : 'no upload id' } ) ;
366
293
}
367
- ep . emit ( 'upload_id_ready ' , { UploadId : UploadId , PartList : [ ] } ) ;
294
+ ep . emit ( 'upload_id_available ' , { UploadId : UploadId , PartList : [ ] } ) ;
368
295
} ) ;
369
296
} ) ;
370
297
371
298
// 如果已存在 UploadId,找一个可以用的 UploadId
372
- ep . on ( 'has_upload_id ' , function ( UploadIdList ) {
299
+ ep . on ( 'has_and_check_upload_id ' , function ( UploadIdList ) {
373
300
// 串行地,找一个内容一致的 UploadId
374
301
UploadIdList = UploadIdList . reverse ( ) ;
375
302
Async . eachLimit ( UploadIdList , 1 , function ( UploadId , asyncCallback ) {
376
303
if ( ! self . _isRunningTask ( TaskId ) ) return ;
377
304
// 如果正在上传,跳过
378
- if ( uploadIdUsing [ UploadId ] ) {
305
+ if ( session . using [ UploadId ] ) {
379
306
asyncCallback ( ) ; // 检查下一个 UploadId
380
307
return ;
381
308
}
@@ -388,7 +315,7 @@ function getUploadIdAndPartList(params, callback) {
388
315
} , function ( err , PartListData ) {
389
316
if ( ! self . _isRunningTask ( TaskId ) ) return ;
390
317
if ( err ) {
391
- removeUploadId . call ( self , UploadId ) ;
318
+ session . removeUsing ( UploadId ) ;
392
319
return ep . emit ( 'error' , err ) ;
393
320
}
394
321
var PartList = PartListData . PartList ;
@@ -414,7 +341,7 @@ function getUploadIdAndPartList(params, callback) {
414
341
if ( ! self . _isRunningTask ( TaskId ) ) return ;
415
342
onHashProgress ( null , true ) ;
416
343
if ( AvailableUploadData && AvailableUploadData . UploadId ) {
417
- ep . emit ( 'upload_id_ready ' , AvailableUploadData ) ;
344
+ ep . emit ( 'upload_id_available ' , AvailableUploadData ) ;
418
345
} else {
419
346
ep . emit ( 'no_available_upload_id' ) ;
420
347
}
@@ -424,50 +351,52 @@ function getUploadIdAndPartList(params, callback) {
424
351
// 在本地缓存找可用的 UploadId
425
352
ep . on ( 'seek_local_avail_upload_id' , function ( RemoteUploadIdList ) {
426
353
// 在本地找可用的 UploadId
427
- var uuid = util . getFileUUID ( params . Body , params . ChunkSize ) , LocalUploadIdList ;
428
- if ( uuid && ( LocalUploadIdList = getUploadId . call ( self , uuid ) ) ) {
429
- var next = function ( index ) {
430
- // 如果本地找不到可用 UploadId,再一个个遍历校验远端
431
- if ( index >= LocalUploadIdList . length ) {
432
- ep . emit ( 'has_upload_id' , RemoteUploadIdList ) ;
433
- return ;
434
- }
435
- var UploadId = LocalUploadIdList [ index ] ;
436
- // 如果不在远端 UploadId 列表里,跳过并删除
437
- if ( ! util . isInArray ( RemoteUploadIdList , UploadId ) ) {
438
- removeUploadId . call ( self , UploadId ) ;
439
- next ( index + 1 ) ;
440
- return ;
441
- }
442
- // 如果正在上传,跳过
443
- if ( uploadIdUsing [ UploadId ] ) {
354
+ var uuid = session . getFileId ( params . Body , params . ChunkSize , Bucket , Key ) ;
355
+ var LocalUploadIdList = session . getUploadIdList ( uuid ) ;
356
+ if ( ! uuid || ! LocalUploadIdList ) {
357
+ ep . emit ( 'has_and_check_upload_id' , RemoteUploadIdList ) ;
358
+ return ;
359
+ }
360
+ var next = function ( index ) {
361
+ // 如果本地找不到可用 UploadId,再一个个遍历校验远端
362
+ if ( index >= LocalUploadIdList . length ) {
363
+ ep . emit ( 'has_and_check_upload_id' , RemoteUploadIdList ) ;
364
+ return ;
365
+ }
366
+ var UploadId = LocalUploadIdList [ index ] ;
367
+ // 如果不在远端 UploadId 列表里,跳过并删除
368
+ if ( ! util . isInArray ( RemoteUploadIdList , UploadId ) ) {
369
+ session . removeUploadId ( UploadId ) ;
370
+ next ( index + 1 ) ;
371
+ return ;
372
+ }
373
+ // 如果正在上传,跳过
374
+ if ( session . using [ UploadId ] ) {
375
+ next ( index + 1 ) ;
376
+ return ;
377
+ }
378
+ // 判断 UploadId 是否存在线上
379
+ wholeMultipartListPart . call ( self , {
380
+ Bucket : Bucket ,
381
+ Region : Region ,
382
+ Key : Key ,
383
+ UploadId : UploadId ,
384
+ } , function ( err , PartListData ) {
385
+ if ( ! self . _isRunningTask ( TaskId ) ) return ;
386
+ if ( err ) {
387
+ // 如果 UploadId 获取会出错,跳过并删除
388
+ session . removeUploadId ( UploadId ) ;
444
389
next ( index + 1 ) ;
445
- return ;
390
+ } else {
391
+ // 找到可用 UploadId
392
+ ep . emit ( 'upload_id_available' , {
393
+ UploadId : UploadId ,
394
+ PartList : PartListData . PartList ,
395
+ } ) ;
446
396
}
447
- // 判断 UploadId 是否存在线上
448
- wholeMultipartListPart . call ( self , {
449
- Bucket : Bucket ,
450
- Region : Region ,
451
- Key : Key ,
452
- UploadId : UploadId ,
453
- } , function ( err , PartListData ) {
454
- if ( ! self . _isRunningTask ( TaskId ) ) return ;
455
- if ( err ) {
456
- removeUploadId . call ( self , UploadId ) ;
457
- next ( index + 1 ) ;
458
- } else {
459
- // 找到可用 UploadId
460
- ep . emit ( 'upload_id_ready' , {
461
- UploadId : UploadId ,
462
- PartList : PartListData . PartList ,
463
- } ) ;
464
- }
465
- } ) ;
466
- } ;
467
- next ( 0 ) ;
468
- } else {
469
- ep . emit ( 'has_upload_id' , RemoteUploadIdList ) ;
470
- }
397
+ } ) ;
398
+ } ;
399
+ next ( 0 ) ;
471
400
} ) ;
472
401
473
402
// 获取线上 UploadId 列表
@@ -491,10 +420,11 @@ function getUploadIdAndPartList(params, callback) {
491
420
if ( RemoteUploadIdList . length ) {
492
421
ep . emit ( 'seek_local_avail_upload_id' , RemoteUploadIdList ) ;
493
422
} else {
494
- var uuid = util . getFileUUID ( params . Body , params . ChunkSize ) , LocalUploadIdList ;
495
- if ( uuid && ( LocalUploadIdList = getUploadId . call ( self , uuid ) ) ) {
423
+ // 远端没有 UploadId,清理缓存的 UploadId
424
+ var uuid = session . getFileId ( params . Body , params . ChunkSize , Bucket , Key ) , LocalUploadIdList ;
425
+ if ( uuid && ( LocalUploadIdList = session . getUploadIdList ( uuid ) ) ) {
496
426
util . each ( LocalUploadIdList , function ( UploadId ) {
497
- removeUploadId . call ( self , UploadId ) ;
427
+ session . removeUploadId ( UploadId ) ;
498
428
} ) ;
499
429
}
500
430
ep . emit ( 'no_available_upload_id' ) ;
0 commit comments