1
- ( function ( ) {
2
-
3
- if ( "performance" in self === false ) {
4
- self . performance = { } ;
5
- }
6
-
7
- Date . now = ( Date . now || function ( ) { // thanks IE8
8
- return new Date ( ) . getTime ( ) ;
9
- } ) ;
10
-
11
- if ( "now" in self . performance === false ) {
12
- var nowOffset = Date . now ( ) ;
13
-
14
- if ( performance . timing && performance . timing . navigationStart ) {
15
- nowOffset = performance . timing . navigationStart ;
1
+ ( function ( ) {
2
+
3
+ if ( "performance" in self === false ) {
4
+ self . performance = { } ;
5
+ }
6
+
7
+ Date . now = ( Date . now || function ( ) { // thanks IE8
8
+ return new Date ( ) . getTime ( ) ;
9
+ } ) ;
10
+
11
+ if ( "now" in self . performance === false ) {
12
+ var nowOffset = Date . now ( ) ;
13
+
14
+ if ( performance . timing && performance . timing . navigationStart ) {
15
+ nowOffset = performance . timing . navigationStart ;
16
+ }
17
+
18
+ self . performance . now = function now ( ) {
19
+ return Date . now ( ) - nowOffset ;
20
+ } ;
16
21
}
17
-
18
- self . performance . now = function now ( ) {
19
- return Date . now ( ) - nowOffset ;
20
- } ;
21
- }
22
22
} ) ( ) ;
23
23
24
- ( function ( ) {
24
+ ( function ( ) {
25
25
var arrayBufferUtils = ( function ( ) {
26
26
function arraybuffer2WordArray ( arrayBuffer ) {
27
27
return CryptoJS . lib . WordArray . create ( arrayBuffer ) ;
39
39
}
40
40
41
41
IterativeMd5 . prototype . append = function ( arrayBuffer ) {
42
- if ( this . _md5 === null ) {
42
+ if ( this . _md5 === null ) {
43
43
this . _md5 = CryptoJS . algo . MD5 . create ( ) ;
44
44
}
45
45
63
63
debug : function ( message ) {
64
64
self . postMessage ( { type : 'log' , logType : 'debug' , message : message } ) ;
65
65
} ,
66
- error : function ( message ) {
66
+ error : function ( message ) {
67
67
self . postMessage ( { type : 'log' , logType : 'error' , message : message } ) ;
68
68
}
69
69
} ;
138
138
function readNextSetOfBlocks ( config ) {
139
139
var numberOfBlocksToRead = 10 ;
140
140
141
- var done = config . done || function ( ) { } ;
142
- var doneOne = config . doneOne || function ( ) { } ;
143
- var doneHalf = config . doneHalf || function ( ) { } ;
144
- var alreadyReading = config . alreadyReading || function ( ) { $log . debug ( 'Already reading next set of blocks!' ) ; } ;
141
+ var done = config . done || function ( ) { } ;
142
+ var doneOne = config . doneOne || function ( ) { } ;
143
+ var doneHalf = config . doneHalf || function ( ) { } ;
144
+ var alreadyReading = config . alreadyReading || function ( ) { $log . debug ( 'Already reading next set of blocks!' ) ; } ;
145
145
146
146
if ( config . state . readingNextSetOfBlocks ) {
147
147
alreadyReading ( ) ;
150
150
151
151
config . state . readingNextSetOfBlocks = true ;
152
152
153
- var fileReader = new FileReader ( ) ;
153
+ var fileReader = new FileReaderSync ( ) ;
154
154
var skip = state . blocksReadIndex ;
155
155
var numberOfBlocks = state . numberOfBlocks ;
156
156
166
166
var blocksToRead = config . state . blocks . slice ( skip , end ) ;
167
167
var currentIndex = 0 ;
168
168
169
- var readNextBlock = function ( ) {
169
+ var readNextBlock = function ( ) {
170
170
var fileContent = config . state . file . slice ( blocksToRead [ currentIndex ] . pointer , blocksToRead [ currentIndex ] . end ) ;
171
- fileReader . readAsArrayBuffer ( fileContent ) ;
171
+ var result = fileReader . readAsArrayBuffer ( fileContent ) ;
172
+ loaded ( result ) ;
172
173
} ;
173
174
174
- fileReader . onload = function ( e ) {
175
- if ( e . target . readyState === FileReader . DONE && ! config . state . cancelled ) {
176
- var currentBlock = blocksToRead [ currentIndex ] ;
177
- $log . debug ( 'Read block ' + currentBlock . blockId ) ;
175
+ var loaded = function ( result ) {
176
+ var currentBlock = blocksToRead [ currentIndex ] ;
177
+ $log . debug ( 'Read block ' + currentBlock . blockId ) ;
178
178
179
- currentBlock . read = true ;
180
- currentBlock . data = new Uint8Array ( e . target . result ) ;
179
+ currentBlock . read = true ;
180
+ currentBlock . data = result ;
181
181
182
- // Calculate block MD5
183
- var blockMd5Start = performance . now ( ) ;
184
- currentBlock . md5 = arrayBufferUtils . getArrayBufferMd5 ( currentBlock . data ) ;
185
- var blockMd5End = performance . now ( ) ;
186
- $log . debug ( "Call to getArrayBufferMd5 for block " + currentBlock . blockId + " took " + ( blockMd5End - blockMd5Start ) + " milliseconds." ) ;
182
+ // Calculate block MD5
183
+ var blockMd5Start = performance . now ( ) ;
184
+ currentBlock . md5 = arrayBufferUtils . getArrayBufferMd5 ( currentBlock . data ) ;
185
+ var blockMd5End = performance . now ( ) ;
186
+ $log . debug ( "Call to getArrayBufferMd5 for block " + currentBlock . blockId + " took " + ( blockMd5End - blockMd5Start ) + " milliseconds." ) ;
187
187
188
- // Iterate file MD5
189
- if ( config . state . calculateFileMd5 ) {
190
- config . state . fileMd5 . append ( currentBlock . data ) ;
191
- }
188
+ // Iterate file MD5
189
+ if ( config . state . calculateFileMd5 ) {
190
+ config . state . fileMd5 . append ( currentBlock . data ) ;
191
+ }
192
192
193
- // Useful to keep things fast
194
- if ( currentIndex === 0 ) {
195
- doneOne ( ) ;
196
- }
193
+ // Useful to keep things fast
194
+ if ( currentIndex === 0 ) {
195
+ doneOne ( ) ;
196
+ }
197
197
198
- if ( currentIndex === ( numberOfBlocksToRead / 2 - 1 ) ) {
199
- doneHalf ( ) ;
200
- }
198
+ if ( currentIndex === ( numberOfBlocksToRead / 2 - 1 ) ) {
199
+ doneHalf ( ) ;
200
+ }
201
201
202
- ++ currentIndex ;
203
- if ( currentIndex < blocksToRead . length ) {
204
- readNextBlock ( ) ;
205
- } else {
206
- done ( ) ;
207
- config . state . blocksReadIndex = config . state . blocksReadIndex + currentIndex ;
208
- config . state . readingNextSetOfBlocks = false ;
209
- }
202
+ ++ currentIndex ;
203
+ if ( currentIndex < blocksToRead . length ) {
204
+ readNextBlock ( ) ;
205
+ } else {
206
+ done ( ) ;
207
+ config . state . blocksReadIndex = config . state . blocksReadIndex + currentIndex ;
208
+ config . state . readingNextSetOfBlocks = false ;
210
209
}
211
210
} ;
212
211
216
215
function blockUploading ( block ) {
217
216
block . uploading = true ;
218
217
219
- var getReadAndUnprocessed = function ( ) {
220
- return state . blocks . filter ( function ( b ) {
218
+ var getReadAndUnprocessed = function ( ) {
219
+ return state . blocks . filter ( function ( b ) {
221
220
return b . read === true && b . uploading === false && b . resolved === false ;
222
221
} ) ;
223
222
} ;
252
251
'x-ms-blob-type' : 'BlockBlob' ,
253
252
'Content-Type' : state . file . type ,
254
253
'Content-MD5' : block . md5 . toString ( CryptoJS . enc . Base64 )
255
- } ) . success ( function ( result , req ) {
254
+ } ) . success ( function ( result , req ) {
256
255
$log . debug ( 'Put block successfully ' + block . blockId ) ;
257
256
258
257
// Clear data
259
258
block . data = null ;
260
259
block . uploading = false ;
261
260
262
261
deferred . resolve ( {
263
- requestLength : requestData . length ,
262
+ requestLength : block . size ,
264
263
data : result ,
265
264
} ) ;
266
- } ) . error ( function ( result , req ) {
265
+ } ) . error ( function ( result , req ) {
267
266
$log . error ( 'Put block error' ) ;
268
267
$log . error ( data ) ;
269
268
297
296
blockId : blockId ,
298
297
blockIdBase64 : btoa ( blockId ) ,
299
298
pointer : pointer ,
299
+ size : ( end - pointer ) ,
300
300
end : end ,
301
301
resolved : false ,
302
302
read : false ,
311
311
312
312
var currentlyProcessing = [ ] ;
313
313
314
- var addToCurrentlyProcessing = function ( block , action ) {
314
+ var addToCurrentlyProcessing = function ( block , action ) {
315
315
currentlyProcessing . push ( { action : action , block : block } ) ;
316
316
} ;
317
317
318
- var removeFromCurrentlyProcessing = function ( action ) {
318
+ var removeFromCurrentlyProcessing = function ( action ) {
319
319
currentlyProcessing . splice ( currentlyProcessing . indexOf ( _ . findWhere ( currentlyProcessing , { action : action } ) ) , 1 ) ;
320
320
} ;
321
321
322
- var getUnresolved = function ( ) {
323
- return state . blocks . filter ( function ( b ) {
322
+ var getUnresolved = function ( ) {
323
+ return state . blocks . filter ( function ( b ) {
324
324
return b . resolved === false && ! _ . findWhere ( currentlyProcessing , { block : b } ) ;
325
325
} ) ;
326
326
} ;
327
327
328
- var removeProcessedAction = function ( action , result ) {
328
+ var removeProcessedAction = function ( action , result ) {
329
329
action . resolved = true ;
330
330
331
331
state . bytesUploaded += result . requestLength ;
343
343
}
344
344
} ;
345
345
346
- var processRejectedAction = function ( block , action , rejectReason ) {
346
+ var processRejectedAction = function ( block , action , rejectReason ) {
347
347
// Remove from currently processing
348
348
removeFromCurrentlyProcessing ( action ) ;
349
349
356
356
//if (state.error) state.error(data, status, headers, config);
357
357
} ;
358
358
359
- var addNextAction = function ( ) {
359
+ var addNextAction = function ( ) {
360
360
var unresolved = getUnresolved ( ) ;
361
361
if ( _ . any ( unresolved ) ) {
362
362
var block = unresolved [ 0 ] ;
363
363
var action = uploadBlock ( block ) ;
364
364
365
- action . then ( function ( result ) {
365
+ action . then ( function ( result ) {
366
366
block . resolved = true ;
367
367
removeProcessedAction ( action , result ) ;
368
- } , function ( rejectReason ) {
368
+ } , function ( rejectReason ) {
369
369
block . resolved = false ;
370
370
processRejectedAction ( block , action , rejectReason ) ;
371
371
} ) ;
381
381
// Get first set of blocks and kick off the upload process.
382
382
readNextSetOfBlocks ( {
383
383
state : state ,
384
- done : function ( ) {
384
+ done : function ( ) {
385
385
for ( var j = 0 ; j < 8 ; j ++ ) {
386
- if ( state . blocks [ j ] ) {
386
+ if ( state . blocks [ j ] ) {
387
387
addNextAction ( state . blocks [ j ] ) ;
388
388
}
389
389
}
390
390
}
391
391
} ) ;
392
392
393
393
return {
394
- cancel : function ( ) {
394
+ cancel : function ( ) {
395
395
state . cancelled = true ;
396
396
}
397
397
} ;
401
401
var uri = state . blobUri + '&comp=blocklist' ;
402
402
403
403
var requestBody = '<?xml version="1.0" encoding="utf-8"?><BlockList>' ;
404
- state . blocks . forEach ( function ( block ) {
404
+ state . blocks . forEach ( function ( block ) {
405
405
requestBody += '<Latest>' + block . blockIdBase64 + '</Latest>' ;
406
406
} ) ;
407
407
requestBody += '</BlockList>' ;
430
430
}
431
431
432
432
function importAllScripts ( libPath ) {
433
- var addTrailingSlash = function ( str ) {
433
+ var addTrailingSlash = function ( str ) {
434
434
var lastChar = str . substr ( - 1 ) ;
435
435
if ( lastChar !== '/' ) {
436
- str = str + '/' ;
436
+ str = str + '/' ;
437
437
}
438
438
return str ;
439
439
} ;
440
440
441
- var addLib = function ( f ) {
441
+ var addLib = function ( f ) {
442
442
importScripts ( addTrailingSlash ( libPath ) + f ) ;
443
443
} ;
444
444
454
454
self . postMessage ( { type : 'ready' } ) ;
455
455
}
456
456
457
- self . onmessage = function ( e ) {
457
+ self . onmessage = function ( e ) {
458
458
459
459
switch ( e . data . type ) {
460
- case 'file' :
461
- setFile ( e . data . file ) ;
462
- break ;
463
- case 'config' :
464
- // Setup state
465
- setConfig ( e . data . config ) ;
466
-
467
- // Load scripts first
468
- importAllScripts ( e . data . config . libPath ) ;
469
-
470
- // Notify when ready for an upload
471
- notifyReady ( ) ;
472
- break ;
473
- case 'upload' :
474
- upload ( ) ;
475
- break ;
476
- default :
477
- throw new Error ( "Don't know what to do with message of type " + e . data . type ) ;
460
+ case 'file' :
461
+ setFile ( e . data . file ) ;
462
+ break ;
463
+ case 'config' :
464
+ // Setup state
465
+ setConfig ( e . data . config ) ;
466
+
467
+ // Load scripts first
468
+ importAllScripts ( e . data . config . libPath ) ;
469
+
470
+ // Notify when ready for an upload
471
+ notifyReady ( ) ;
472
+ break ;
473
+ case 'upload' :
474
+ upload ( ) ;
475
+ break ;
476
+ default :
477
+ throw new Error ( "Don't know what to do with message of type " + e . data . type ) ;
478
478
}
479
479
} ;
480
480
} ) ( ) ;
0 commit comments