@@ -15,16 +15,19 @@ const upload = async function(items, dirPath, options = {}){
15
15
}
16
16
}
17
17
18
+ const error = ( e ) => {
19
+ // if error callback is provided, call it
20
+ if ( options . error && typeof options . error === 'function' )
21
+ options . error ( e ) ;
22
+ return reject ( e ) ;
23
+ } ;
24
+
18
25
// xhr object to be used for the upload
19
26
let xhr = new XMLHttpRequest ( ) ;
20
27
21
28
// Can not write to root
22
- if ( dirPath === '/' ) {
23
- // if error callback is provided, call it
24
- if ( options . error && typeof options . error === 'function' )
25
- options . error ( 'Can not upload to root directory.' ) ;
26
- return reject ( 'Can not upload to root directory.' ) ;
27
- }
29
+ if ( dirPath === '/' )
30
+ return error ( 'Can not upload to root directory.' ) ;
28
31
29
32
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
30
33
// in that case, we need to prepend the app's root directory to it
@@ -119,7 +122,11 @@ const upload = async function(items, dirPath, options = {}){
119
122
entries [ i ] . filepath = entries [ i ] . name ;
120
123
entries [ i ] . fullPath = entries [ i ] . name ;
121
124
}
122
- }
125
+ }
126
+ // Anything else is invalid
127
+ else {
128
+ return error ( { code : 'field_invalid' , message : 'upload() items parameter is an invalid type' } ) ;
129
+ }
123
130
124
131
// Will hold directories and files to be uploaded
125
132
let dirs = [ ] ;
@@ -145,10 +152,7 @@ const upload = async function(items, dirPath, options = {}){
145
152
146
153
// Continue only if there are actually any files/directories to upload
147
154
if ( dirs . length === 0 && files . length === 0 ) {
148
- if ( options . error && typeof options . error === 'function' ) {
149
- options . error ( { code : 'EMPTY_UPLOAD' , message : 'No files or directories to upload.' } ) ;
150
- }
151
- return reject ( { code : 'EMPTY_UPLOAD' , message : 'No files or directories to upload.' } ) ;
155
+ return error ( { code : 'EMPTY_UPLOAD' , message : 'No files or directories to upload.' } ) ;
152
156
}
153
157
154
158
// Check storage capacity.
@@ -163,10 +167,7 @@ const upload = async function(items, dirPath, options = {}){
163
167
try {
164
168
storage = await this . space ( ) ;
165
169
if ( storage . capacity - storage . used < total_size ) {
166
- if ( options . error && typeof options . error === 'function' ) {
167
- options . error ( { code : 'NOT_ENOUGH_SPACE' , message : 'Not enough storage space available.' } ) ;
168
- }
169
- return reject ( { code : 'NOT_ENOUGH_SPACE' , message : 'Not enough storage space available.' } ) ;
170
+ return error ( { code : 'NOT_ENOUGH_SPACE' , message : 'Not enough storage space available.' } ) ;
170
171
}
171
172
} catch ( e ) {
172
173
// Ignored
@@ -368,18 +369,10 @@ const upload = async function(items, dirPath, options = {}){
368
369
break ;
369
370
}
370
371
}
371
- // if error callback is provided, call it
372
- if ( options . error && typeof options . error === 'function' ) {
373
- options . error ( failed_operation ) ;
374
- }
375
- return reject ( failed_operation ) ;
372
+ return error ( failed_operation ) ;
376
373
}
377
374
378
- // if error callback is provided, call it
379
- if ( options . error && typeof options . error === 'function' ) {
380
- options . error ( resp ) ;
381
- }
382
- return reject ( resp ) ;
375
+ return error ( resp ) ;
383
376
}
384
377
// Success
385
378
else {
0 commit comments