1
1
/*!
2
2
* Uploader - Uploader library implements html5 file upload and provides multiple simultaneous, stable, fault tolerant and resumable uploads
3
- * @version v0.2.2
3
+ * @version v0.3.0
4
4
* @author dolymood <[email protected] >
5
5
* @link https://github.com/simple-uploader/Uploader
6
6
* @license MIT
@@ -167,31 +167,45 @@ utils.extend(Chunk.prototype, {
167
167
}
168
168
169
169
function doneHandler ( event ) {
170
- var status = $ . status ( )
171
- if ( status === STATUS . SUCCESS || status === STATUS . ERROR ) {
172
- delete this . data
173
- $ . _event ( status , $ . message ( ) )
174
- status === STATUS . ERROR && $ . uploader . uploadNextChunk ( )
175
- } else {
176
- $ . _event ( STATUS . RETRY , $ . message ( ) )
177
- $ . pendingRetry = true
178
- $ . abort ( )
179
- $ . retries ++
180
- var retryInterval = $ . uploader . opts . chunkRetryInterval
181
- if ( retryInterval !== null ) {
182
- setTimeout ( function ( ) {
183
- $ . send ( )
184
- } , retryInterval )
170
+ var msg = $ . message ( )
171
+ $ . processingResponse = true
172
+ $ . uploader . opts . processResponse ( msg , function ( err , res ) {
173
+ $ . processingResponse = false
174
+ if ( ! $ . xhr ) {
175
+ return
176
+ }
177
+ $ . processedState = {
178
+ err : err ,
179
+ res : res
180
+ }
181
+ var status = $ . status ( )
182
+ if ( status === STATUS . SUCCESS || status === STATUS . ERROR ) {
183
+ delete this . data
184
+ $ . _event ( status , res )
185
+ status === STATUS . ERROR && $ . uploader . uploadNextChunk ( )
185
186
} else {
186
- $ . send ( )
187
+ $ . _event ( STATUS . RETRY , res )
188
+ $ . pendingRetry = true
189
+ $ . abort ( )
190
+ $ . retries ++
191
+ var retryInterval = $ . uploader . opts . chunkRetryInterval
192
+ if ( retryInterval !== null ) {
193
+ setTimeout ( function ( ) {
194
+ $ . send ( )
195
+ } , retryInterval )
196
+ } else {
197
+ $ . send ( )
198
+ }
187
199
}
188
- }
200
+ } )
189
201
}
190
202
} ,
191
203
192
204
abort : function ( ) {
193
205
var xhr = this . xhr
194
206
this . xhr = null
207
+ this . processingResponse = false
208
+ this . processedState = null
195
209
if ( xhr ) {
196
210
xhr . abort ( )
197
211
}
@@ -206,25 +220,31 @@ utils.extend(Chunk.prototype, {
206
220
return STATUS . UPLOADING
207
221
} else if ( ! this . xhr ) {
208
222
return STATUS . PENDING
209
- } else if ( this . xhr . readyState < 4 ) {
223
+ } else if ( this . xhr . readyState < 4 || this . processingResponse ) {
210
224
// Status is really 'OPENED', 'HEADERS_RECEIVED'
211
225
// or 'LOADING' - meaning that stuff is happening
212
226
return STATUS . UPLOADING
213
227
} else {
228
+ var _status
214
229
if ( this . uploader . opts . successStatuses . indexOf ( this . xhr . status ) > - 1 ) {
215
230
// HTTP 200, perfect
216
231
// HTTP 202 Accepted - The request has been accepted for processing, but the processing has not been completed.
217
- return STATUS . SUCCESS
232
+ _status = STATUS . SUCCESS
218
233
} else if ( this . uploader . opts . permanentErrors . indexOf ( this . xhr . status ) > - 1 ||
219
234
! isTest && this . retries >= this . uploader . opts . maxChunkRetries ) {
220
235
// HTTP 415/500/501, permanent error
221
- return STATUS . ERROR
236
+ _status = STATUS . ERROR
222
237
} else {
223
238
// this should never happen, but we'll reset and queue a retry
224
239
// a likely case for this would be 503 service unavailable
225
240
this . abort ( )
226
- return STATUS . PENDING
241
+ _status = STATUS . PENDING
242
+ }
243
+ var processedState = this . processedState
244
+ if ( processedState && processedState . err ) {
245
+ _status = STATUS . ERROR
227
246
}
247
+ return _status
228
248
}
229
249
} ,
230
250
@@ -351,11 +371,16 @@ var event = _dereq_('./event')
351
371
var File = _dereq_ ( './file' )
352
372
var Chunk = _dereq_ ( './chunk' )
353
373
354
- var version = '0.2.2'
374
+ var version = '0.3.0'
375
+
376
+ var isServer = typeof window === 'undefined'
355
377
356
378
// ie10+
357
- var ie10plus = window . navigator . msPointerEnabled
379
+ var ie10plus = isServer ? false : window . navigator . msPointerEnabled
358
380
var support = ( function ( ) {
381
+ if ( isServer ) {
382
+ return false
383
+ }
359
384
var sliceName = 'slice'
360
385
var _support = utils . isDefined ( window . File ) && utils . isDefined ( window . Blob ) &&
361
386
utils . isDefined ( window . FileList )
@@ -376,6 +401,9 @@ var support = (function () {
376
401
} ) ( )
377
402
378
403
var supportDirectory = ( function ( ) {
404
+ if ( isServer ) {
405
+ return false
406
+ }
379
407
var input = window . document . createElement ( 'input' )
380
408
input . type = 'file'
381
409
var sd = 'webkitdirectory' in input || 'directory' in input
@@ -393,6 +421,8 @@ function Uploader (opts) {
393
421
utils . defineNonEnumerable ( this , 'filePaths' , { } )
394
422
this . opts = utils . extend ( { } , Uploader . defaults , opts || { } )
395
423
424
+ this . preventEvent = utils . bind ( this . _preventEvent , this )
425
+
396
426
File . call ( this , this )
397
427
}
398
428
@@ -435,7 +465,11 @@ Uploader.defaults = {
435
465
onDropStopPropagation : false ,
436
466
initFileFn : null ,
437
467
readFileFn : webAPIFileRead ,
438
- checkChunkUploadedByResponse : null
468
+ checkChunkUploadedByResponse : null ,
469
+ initialPaused : false ,
470
+ processResponse : function ( response , cb ) {
471
+ cb ( null , response )
472
+ }
439
473
}
440
474
441
475
Uploader . utils = utils
@@ -708,6 +742,7 @@ utils.extend(Uploader.prototype, {
708
742
// When new files are added, simply append them to the overall list
709
743
var that = this
710
744
input . addEventListener ( 'change' , function ( e ) {
745
+ that . _trigger ( e . type , e )
711
746
if ( e . target . value ) {
712
747
that . addFiles ( e . target . files , e )
713
748
e . target . value = ''
@@ -717,6 +752,7 @@ utils.extend(Uploader.prototype, {
717
752
} ,
718
753
719
754
onDrop : function ( evt ) {
755
+ this . _trigger ( evt . type , evt )
720
756
if ( this . opts . onDropStopPropagation ) {
721
757
evt . stopPropagation ( )
722
758
}
@@ -798,6 +834,11 @@ utils.extend(Uploader.prototype, {
798
834
} , this )
799
835
} ,
800
836
837
+ _preventEvent : function ( e ) {
838
+ utils . preventEvent ( e )
839
+ this . _trigger ( e . type , e )
840
+ } ,
841
+
801
842
/**
802
843
* Assign one or more DOM nodes as a drop target.
803
844
* @function
@@ -806,8 +847,9 @@ utils.extend(Uploader.prototype, {
806
847
assignDrop : function ( domNodes ) {
807
848
this . _onDrop = utils . bind ( this . onDrop , this )
808
849
this . _assignHelper ( domNodes , {
809
- dragover : utils . preventEvent ,
810
- dragenter : utils . preventEvent ,
850
+ dragover : this . preventEvent ,
851
+ dragenter : this . preventEvent ,
852
+ dragleave : this . preventEvent ,
811
853
drop : this . _onDrop
812
854
} )
813
855
} ,
@@ -819,8 +861,9 @@ utils.extend(Uploader.prototype, {
819
861
*/
820
862
unAssignDrop : function ( domNodes ) {
821
863
this . _assignHelper ( domNodes , {
822
- dragover : utils . preventEvent ,
823
- dragenter : utils . preventEvent ,
864
+ dragover : this . preventEvent ,
865
+ dragenter : this . preventEvent ,
866
+ dragleave : this . preventEvent ,
824
867
drop : this . _onDrop
825
868
} , true )
826
869
this . _onDrop = null
@@ -866,7 +909,7 @@ function File (uploader, file, parent) {
866
909
}
867
910
}
868
911
869
- this . paused = false
912
+ this . paused = uploader . opts . initialPaused
870
913
this . error = false
871
914
this . allError = false
872
915
this . aborted = false
0 commit comments