@@ -20,10 +20,10 @@ function onProgressReceiverProgress(context: Context, uploadInfo: UploadInfo) {
20
20
task . setUpload ( currentBytes ) ;
21
21
task . setStatus ( "uploading" ) ;
22
22
task . notify ( < common . ProgressEventData > {
23
- eventName : "progress" ,
24
- object : task ,
25
- currentBytes : currentBytes ,
26
- totalBytes : totalBytes
23
+ eventName : "progress" ,
24
+ object : task ,
25
+ currentBytes : currentBytes ,
26
+ totalBytes : totalBytes
27
27
} ) ;
28
28
}
29
29
@@ -39,11 +39,11 @@ function onProgressReceiverError(context: Context, uploadInfo: UploadInfo, respo
39
39
const task = Task . fromId ( uploadId ) ;
40
40
task . setStatus ( "error" ) ;
41
41
task . notify ( < common . ErrorEventData > {
42
- eventName : "error" ,
43
- object : task ,
44
- error,
45
- responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1 ,
46
- response
42
+ eventName : "error" ,
43
+ object : task ,
44
+ error,
45
+ responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1 ,
46
+ response
47
47
} ) ;
48
48
}
49
49
@@ -60,22 +60,22 @@ function onProgressReceiverCompleted(context: Context, uploadInfo: UploadInfo, r
60
60
task . setStatus ( "complete" ) ;
61
61
62
62
task . notify ( < common . ProgressEventData > {
63
- eventName : "progress" ,
64
- object : task ,
65
- currentBytes : totalUpload ,
66
- totalBytes : totalUpload
63
+ eventName : "progress" ,
64
+ object : task ,
65
+ currentBytes : totalUpload ,
66
+ totalBytes : totalUpload
67
67
} ) ;
68
68
task . notify ( < common . ResultEventData > {
69
- eventName : "responded" ,
70
- object : task ,
71
- data : response . getBodyAsString ( ) ,
72
- responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1
69
+ eventName : "responded" ,
70
+ object : task ,
71
+ data : response . getBodyAsString ( ) ,
72
+ responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1
73
73
} ) ;
74
74
task . notify ( < common . CompleteEventData > {
75
- eventName : "complete" ,
76
- object : task ,
77
- responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1 ,
78
- response
75
+ eventName : "complete" ,
76
+ object : task ,
77
+ responseCode : response && typeof response . getHttpCode === 'function' ? response . getHttpCode ( ) : - 1 ,
78
+ response
79
79
} ) ;
80
80
}
81
81
@@ -90,21 +90,21 @@ function initializeProgressReceiver() {
90
90
const zonedOnCompleted = global . zonedCallback ( onProgressReceiverCompleted ) ;
91
91
92
92
const temp : Partial < UploadServiceBroadcastReceiver > = {
93
- onProgress ( context : Context , uploadInfo : UploadInfo ) {
94
- zonedOnProgress ( context , uploadInfo ) ;
95
- } ,
93
+ onProgress ( context : Context , uploadInfo : UploadInfo ) {
94
+ zonedOnProgress ( context , uploadInfo ) ;
95
+ } ,
96
96
97
- onCancelled ( context : Context , uploadInfo : UploadInfo ) {
98
- zonedOnCancelled ( context , uploadInfo ) ;
99
- } ,
97
+ onCancelled ( context : Context , uploadInfo : UploadInfo ) {
98
+ zonedOnCancelled ( context , uploadInfo ) ;
99
+ } ,
100
100
101
- onError ( context : Context , uploadInfo : UploadInfo , response : ServerResponse , error : java . lang . Exception ) {
102
- zonedOnError ( context , uploadInfo , response , error ) ;
103
- } ,
101
+ onError ( context : Context , uploadInfo : UploadInfo , response : ServerResponse , error : java . lang . Exception ) {
102
+ zonedOnError ( context , uploadInfo , response , error ) ;
103
+ } ,
104
104
105
- onCompleted ( context : Context , uploadInfo : UploadInfo , serverResponse : ServerResponse ) {
106
- zonedOnCompleted ( context , uploadInfo , serverResponse ) ;
107
- }
105
+ onCompleted ( context : Context , uploadInfo : UploadInfo , serverResponse : ServerResponse ) {
106
+ zonedOnCompleted ( context , uploadInfo , serverResponse ) ;
107
+ }
108
108
} ;
109
109
110
110
const ProgressReceiverImpl = ( < any > net . gotev . uploadservice . UploadServiceBroadcastReceiver ) . extend ( temp ) ;
@@ -177,50 +177,10 @@ class Task extends ObservableBase {
177
177
static create ( session : Session , file : string , options : common . Request ) : Task {
178
178
ensureUploadServiceNamespace ( ) ;
179
179
ensureReceiver ( ) ;
180
- const task = new Task ( ) ;
181
- task . _session = session ;
182
- task . _id = session . id + "{" + ++ Task . taskCount + "}" ;
183
-
184
- const context = application . android . context ;
185
-
186
- const request = new net . gotev . uploadservice . BinaryUploadRequest ( context , task . _id , options . url ) ;
187
-
188
- request . setFileToUpload ( file ) ;
189
-
190
- const displayNotificationProgress = typeof options . androidDisplayNotificationProgress === "boolean" ? options . androidDisplayNotificationProgress : true ;
191
- if ( displayNotificationProgress ) {
192
- const uploadNotificationConfig = new net . gotev . uploadservice . UploadNotificationConfig ( ) ;
193
- const notificationTitle = typeof options . androidNotificationTitle === "string" ? options . androidNotificationTitle : 'File Upload' ;
194
- uploadNotificationConfig . setTitleForAllStatuses ( notificationTitle ) ;
195
- request . setNotificationConfig ( uploadNotificationConfig ) ;
196
- }
197
- const autoDeleteAfterUpload = typeof options . androidAutoDeleteAfterUpload === "boolean" ? options . androidAutoDeleteAfterUpload : false ;
198
- if ( autoDeleteAfterUpload ) {
199
- request . setAutoDeleteFilesAfterSuccessfulUpload ( true ) ;
200
- }
201
- const maxRetryCount = typeof options . androidMaxRetries === "number" ? options . androidMaxRetries : undefined ;
202
- if ( maxRetryCount ) {
203
- request . setMaxRetries ( maxRetryCount ) ;
204
- }
205
-
206
- const headers = options . headers ;
207
- if ( headers ) {
208
- for ( const header in headers ) {
209
- const value = headers [ header ] ;
210
- if ( value !== null && value !== void 0 ) {
211
- request . addHeader ( header , value . toString ( ) ) ;
212
- }
213
- }
214
- }
215
-
216
- task . setDescription ( options . description ) ;
217
-
218
- request . setMethod ( options . method ? options . method : "GET" ) ;
219
-
220
- task . setUpload ( 0 ) ;
221
- task . setTotalUpload ( 1 ) ;
222
- task . setStatus ( "pending" ) ;
180
+ const taskId = session . id + "{" + ++ Task . taskCount + "}" ;
181
+ const request = getBinaryRequest ( taskId , options , file ) ;
223
182
183
+ const task = Task . initTask ( taskId , session , options ) ;
224
184
request . startUpload ( ) ;
225
185
226
186
Task . cache [ task . _id ] = task ;
@@ -231,79 +191,25 @@ class Task extends ObservableBase {
231
191
static createMultiPart ( session : Session , params : Array < any > , options : common . Request ) : Task {
232
192
ensureUploadServiceNamespace ( ) ;
233
193
ensureReceiver ( ) ;
234
- const task = new Task ( ) ;
235
- task . _session = session ;
236
- task . _id = session . id + "{" + ( ++ Task . taskCount ) + "}" ;
237
-
238
- const context = application . android . context ;
239
-
240
- const request = new net . gotev . uploadservice . MultipartUploadRequest ( context , task . _id , options . url ) ;
241
-
242
-
243
- for ( let i = 0 ; i < params . length ; i ++ ) {
244
- const curParam = params [ i ] ;
245
- if ( typeof curParam . name === 'undefined' ) {
246
- throw new Error ( "You must have a `name` value" ) ;
247
- }
248
-
249
- if ( curParam . filename ) {
250
- let fileName = curParam . filename ;
251
- if ( fileName . startsWith ( "~/" ) ) {
252
- fileName = fileName . replace ( "~/" , fileSystemModule . knownFolders . currentApp ( ) . path + "/" ) ;
253
- }
194
+ const taskId = session . id + "{" + ++ Task . taskCount + "}" ;
195
+ const request = getMultipartRequest ( taskId , options , params ) ;
196
+ const task = Task . initTask ( taskId , session , options ) ;
254
197
255
- const destFileName = curParam . destFilename || fileName . substring ( fileName . lastIndexOf ( '/' ) + 1 , fileName . length ) ;
256
- request . addFileToUpload ( fileName , curParam . name , destFileName , curParam . mimeType ) ;
257
- } else {
258
- request . addParameter ( params [ i ] . name , params [ i ] . value ) ;
259
-
260
- }
261
- }
262
-
263
- const utf8 = options . utf8 ;
264
-
265
- if ( utf8 ) {
266
- request . setUtf8Charset ( ) ;
267
- }
268
-
269
- const displayNotificationProgress = typeof options . androidDisplayNotificationProgress === "boolean" ? options . androidDisplayNotificationProgress : true ;
270
- if ( displayNotificationProgress ) {
271
- const uploadNotificationConfig = new net . gotev . uploadservice . UploadNotificationConfig ( ) ;
272
- const notificationTitle = typeof options . androidNotificationTitle === "string" ? options . androidNotificationTitle : 'File Upload' ;
273
- uploadNotificationConfig . setTitleForAllStatuses ( notificationTitle ) ;
274
- request . setNotificationConfig ( uploadNotificationConfig ) ;
275
- }
276
- const autoDeleteAfterUpload = typeof options . androidAutoDeleteAfterUpload === "boolean" ? options . androidAutoDeleteAfterUpload : false ;
277
- if ( autoDeleteAfterUpload ) {
278
- request . setAutoDeleteFilesAfterSuccessfulUpload ( true ) ;
279
- }
280
- const maxRetryCount = typeof options . androidMaxRetries === "number" ? options . androidMaxRetries : undefined ;
281
- if ( maxRetryCount ) {
282
- request . setMaxRetries ( maxRetryCount ) ;
283
- }
198
+ request . startUpload ( ) ;
199
+ Task . cache [ task . _id ] = task ;
200
+ return task ;
201
+ }
284
202
285
- const headers = options . headers ;
286
- if ( headers ) {
287
- for ( const header in headers ) {
288
- const value = headers [ header ] ;
289
- if ( value !== null && value !== void 0 ) {
290
- request . addHeader ( header , value . toString ( ) ) ;
291
- }
292
- }
293
- }
203
+ private static initTask ( taskId : string , session : Session , options : common . Request ) {
204
+ const task = new Task ( ) ;
205
+ task . _session = session ;
206
+ task . _id = taskId ;
294
207
295
208
task . setDescription ( options . description ) ;
296
-
297
- request . setMethod ( options . method ? options . method : "GET" ) ;
298
-
299
209
task . setUpload ( 0 ) ;
300
210
task . setTotalUpload ( 1 ) ;
301
211
task . setStatus ( "pending" ) ;
302
212
303
- request . startUpload ( ) ;
304
-
305
- Task . cache [ task . _id ] = task ;
306
-
307
213
return task ;
308
214
}
309
215
@@ -354,3 +260,73 @@ class Task extends ObservableBase {
354
260
( < any > net ) . gotev . uploadservice . UploadService . stopUpload ( this . _id ) ;
355
261
}
356
262
}
263
+
264
+ function getBinaryRequest ( taskId : string , options : common . Request , file : string ) {
265
+ const request = new net . gotev . uploadservice . BinaryUploadRequest ( application . android . context , taskId , options . url ) ;
266
+
267
+ request . setFileToUpload ( file ) ;
268
+ setRequestOptions ( request , options ) ;
269
+
270
+ return request ;
271
+ }
272
+
273
+ function getMultipartRequest ( taskId : string , options : common . Request , params : any [ ] ) {
274
+ const request = new net . gotev . uploadservice . MultipartUploadRequest ( application . android . context , taskId , options . url ) ;
275
+
276
+ for ( let i = 0 ; i < params . length ; i ++ ) {
277
+ const curParam = params [ i ] ;
278
+ if ( typeof curParam . name === 'undefined' ) {
279
+ throw new Error ( "You must have a `name` value" ) ;
280
+ }
281
+ if ( curParam . filename ) {
282
+ let fileName = curParam . filename ;
283
+ if ( fileName . startsWith ( "~/" ) ) {
284
+ fileName = fileName . replace ( "~/" , fileSystemModule . knownFolders . currentApp ( ) . path + "/" ) ;
285
+ }
286
+ const destFileName = curParam . destFilename || fileName . substring ( fileName . lastIndexOf ( '/' ) + 1 , fileName . length ) ;
287
+ request . addFileToUpload ( fileName , curParam . name , destFileName , curParam . mimeType ) ;
288
+ }
289
+ else {
290
+ request . addParameter ( params [ i ] . name , params [ i ] . value ) ;
291
+ }
292
+ }
293
+
294
+ const utf8 = options . utf8 ;
295
+
296
+ if ( utf8 ) {
297
+ request . setUtf8Charset ( ) ;
298
+ }
299
+ setRequestOptions ( request , options ) ;
300
+
301
+ return request ;
302
+ }
303
+
304
+ function setRequestOptions ( request : any , options : common . Request ) {
305
+ const displayNotificationProgress = typeof options . androidDisplayNotificationProgress === "boolean" ? options . androidDisplayNotificationProgress : true ;
306
+
307
+ if ( displayNotificationProgress ) {
308
+ const uploadNotificationConfig = new net . gotev . uploadservice . UploadNotificationConfig ( ) ;
309
+ const notificationTitle = typeof options . androidNotificationTitle === "string" ? options . androidNotificationTitle : 'File Upload' ;
310
+ uploadNotificationConfig . setTitleForAllStatuses ( notificationTitle ) ;
311
+ request . setNotificationConfig ( uploadNotificationConfig ) ;
312
+ }
313
+ const autoDeleteAfterUpload = typeof options . androidAutoDeleteAfterUpload === "boolean" ? options . androidAutoDeleteAfterUpload : false ;
314
+ if ( autoDeleteAfterUpload ) {
315
+ request . setAutoDeleteFilesAfterSuccessfulUpload ( true ) ;
316
+ }
317
+ const maxRetryCount = typeof options . androidMaxRetries === "number" ? options . androidMaxRetries : undefined ;
318
+ if ( maxRetryCount ) {
319
+ request . setMaxRetries ( maxRetryCount ) ;
320
+ }
321
+ const headers = options . headers ;
322
+ if ( headers ) {
323
+ for ( const header in headers ) {
324
+ const value = headers [ header ] ;
325
+ if ( value !== null && value !== void 0 ) {
326
+ request . addHeader ( header , value . toString ( ) ) ;
327
+ }
328
+ }
329
+ }
330
+
331
+ request . setMethod ( options . method ? options . method : "GET" ) ;
332
+ }
0 commit comments