1
1
import {
2
2
fileNameFromPath ,
3
3
Headers ,
4
+ HttpDownloadRequestOptions ,
4
5
HttpError ,
5
6
HttpRequestOptions ,
6
7
isImageUrl ,
7
8
SaveImageStorageKey ,
8
9
TNSHttpSettings
9
10
} from './http-request-common' ;
10
11
import * as types from 'tns-core-modules/utils/types' ;
12
+ import { isString } from 'tns-core-modules/utils/types' ;
11
13
import { NetworkAgent } from 'tns-core-modules/debugger' ;
12
14
import { getString , setString } from 'tns-core-modules/application-settings' ;
13
- import { File , knownFolders , path } from 'tns-core-modules/file-system' ;
15
+ import { File , Folder , knownFolders , path } from 'tns-core-modules/file-system' ;
14
16
import { FileManager } from '..' ;
15
17
16
18
export type CancellablePromise = Promise < any > & { cancel : ( ) => void } ;
@@ -102,7 +104,7 @@ export class Http {
102
104
constructor ( ) {
103
105
}
104
106
105
- buildJavaOptions ( options : HttpRequestOptions ) {
107
+ private static buildJavaOptions ( options : HttpRequestOptions ) {
106
108
if ( ! types . isString ( options . url ) ) {
107
109
throw new Error ( 'Http request must provide a valid url.' ) ;
108
110
}
@@ -148,6 +150,45 @@ export class Http {
148
150
return javaOptions ;
149
151
}
150
152
153
+ private static buildJavaDownloadOptions ( options : HttpDownloadRequestOptions ) {
154
+ if ( ! types . isString ( options . url ) ) {
155
+ throw new Error ( 'Http request must provide a valid url.' ) ;
156
+ }
157
+
158
+ const javaOptions = new com . github . triniwiz . async . Async . Http . DownloadRequestOptions ( ) ;
159
+ javaOptions . url = options . url ;
160
+
161
+ if ( typeof options . timeout === 'number' ) {
162
+ javaOptions . timeout = options . timeout ;
163
+ }
164
+
165
+ if ( typeof options . filePath === 'string' ) {
166
+ javaOptions . filePath = options . filePath ;
167
+ } else {
168
+ // creates directory
169
+ Folder . fromPath ( path . join ( knownFolders . temp ( ) . path , 'async_http' ) ) ;
170
+ javaOptions . filePath = path . join ( knownFolders . temp ( ) . path , 'async_http' , java . util . UUID . randomUUID ( ) . toString ( ) ) ;
171
+ }
172
+
173
+ if ( options . headers ) {
174
+ const arrayList = new java . util . ArrayList < any > ( ) ;
175
+ const pair = com . github . triniwiz . async . Async . Http . KeyValuePair ;
176
+
177
+ if ( options . headers instanceof Map ) {
178
+ options . headers . forEach ( ( value , key ) => {
179
+ arrayList . add ( new pair ( key , value + '' ) ) ;
180
+ } ) ;
181
+ } else {
182
+ for ( let key in options . headers ) {
183
+ arrayList . add ( new pair ( key , options . headers [ key ] + '' ) ) ;
184
+ }
185
+ }
186
+
187
+ javaOptions . headers = arrayList ;
188
+ }
189
+ return javaOptions ;
190
+ }
191
+
151
192
request ( options : HttpRequestOptions ) : CancellablePromise {
152
193
const headers : Headers = { } ;
153
194
let statusCode = 0 ;
@@ -157,7 +198,7 @@ export class Http {
157
198
try {
158
199
159
200
// initialize the options
160
- const javaOptions = this . buildJavaOptions ( options ) ;
201
+ const javaOptions = Http . buildJavaOptions ( options ) ;
161
202
162
203
if ( TNSHttpSettings . debug ) {
163
204
// @ts -ignore
@@ -396,6 +437,160 @@ export class Http {
396
437
return request ;
397
438
}
398
439
440
+ public static getFile ( options : HttpDownloadRequestOptions ) : CancellablePromise {
441
+ const headers : Headers = { } ;
442
+ let statusCode = 0 ;
443
+ let id ;
444
+ const counter = requestIdCounter ;
445
+ const request = < CancellablePromise > new Promise < any > ( ( resolve , reject ) => {
446
+ try {
447
+
448
+ // initialize the options
449
+ const javaOptions = Http . buildJavaDownloadOptions ( options ) ;
450
+
451
+ if ( TNSHttpSettings . debug ) {
452
+ // @ts -ignore
453
+ if ( global . __inspector && global . __inspector . isConnected ) {
454
+ NetworkAgent . requestWillBeSent ( requestIdCounter , options ) ;
455
+ }
456
+ }
457
+
458
+ const makeRemoteRequest = ( ) => {
459
+ const callback = new com . github . triniwiz . async . Async . Http . Callback ( {
460
+ onCancel ( param : any ) : void {
461
+ reject ( {
462
+ type : HttpError . Cancelled ,
463
+ result : param
464
+ } ) ;
465
+ requestCallbacks . delete ( id ) ;
466
+ } ,
467
+ onComplete ( result : any ) : void {
468
+ if ( result && result . headers ) {
469
+ const length = result . headers . size ( ) ;
470
+ let pair ;
471
+ for ( let i = 0 ; i < length ; i ++ ) {
472
+ pair = result . headers . get ( i ) ;
473
+ addHeader ( headers , pair . key , pair . value ) ;
474
+ }
475
+ }
476
+ // send response data (for requestId) to network debugger
477
+
478
+
479
+ let contentType = headers [ 'Content-Type' ] ;
480
+ if ( types . isNullOrUndefined ( contentType ) ) {
481
+ contentType = headers [ 'content-type' ] ;
482
+ }
483
+ let acceptHeader ;
484
+
485
+ if ( types . isNullOrUndefined ( contentType ) ) {
486
+ acceptHeader = headers [ 'Accept' ] ;
487
+ if ( types . isNullOrUndefined ( acceptHeader ) ) {
488
+ acceptHeader = headers [ 'accept' ] ;
489
+ }
490
+ } else {
491
+ acceptHeader = contentType ;
492
+ }
493
+
494
+ let returnType = 'text/plain' ;
495
+ if ( ! types . isNullOrUndefined ( acceptHeader ) && types . isString ( acceptHeader ) ) {
496
+ let acceptValues = acceptHeader . split ( ',' ) ;
497
+ let quality = [ ] ;
498
+ let defaultQuality = [ ] ;
499
+ let customQuality = [ ] ;
500
+ for ( let value of acceptValues ) {
501
+ if ( value . indexOf ( ';q=' ) > - 1 ) {
502
+ customQuality . push ( value ) ;
503
+ } else {
504
+ defaultQuality . push ( value ) ;
505
+ }
506
+ }
507
+ customQuality = customQuality . sort ( ( a , b ) => {
508
+ const a_quality = parseFloat ( a . substring ( a . indexOf ( ';q=' ) ) . replace ( ';q=' , '' ) ) ;
509
+ const b_quality = parseFloat ( b . substring ( b . indexOf ( ';q=' ) ) . replace ( ';q=' , '' ) ) ;
510
+ return ( b_quality - a_quality ) ;
511
+ } ) ;
512
+ quality . push ( ...defaultQuality ) ;
513
+ quality . push ( ...customQuality ) ;
514
+ returnType = quality [ 0 ] ;
515
+ }
516
+
517
+ result [ 'statusCode' ] = statusCode ;
518
+
519
+ if ( TNSHttpSettings . debug ) {
520
+ // send response data (for requestId) to network debugger
521
+ // @ts -ignore
522
+ if ( global . __inspector && global . __inspector . isConnected ) {
523
+ NetworkAgent . responseReceived ( counter , {
524
+ url : result . url ,
525
+ statusCode,
526
+ headers,
527
+ responseAsString : isString ? ( result . contentText ? result . contentText : result . content . toString ( ) ) : null ,
528
+ responseAsImage : null // TODO needs base64 Image
529
+ } , headers ) ;
530
+ }
531
+
532
+ }
533
+
534
+
535
+ resolve ( result . filePath ) ;
536
+ requestCallbacks . delete ( id ) ;
537
+ } ,
538
+ onError ( param0 : string , param1 : java . lang . Exception ) : void {
539
+ reject ( {
540
+ type : HttpError . Error ,
541
+ message : param0
542
+ } ) ;
543
+ requestCallbacks . delete ( id ) ;
544
+ } ,
545
+ onHeaders ( jHeaders : any , status : number ) : void {
546
+ statusCode = status ;
547
+ const length = jHeaders . size ( ) ;
548
+ let pair ;
549
+ for ( let i = 0 ; i < length ; i ++ ) {
550
+ pair = jHeaders . get ( i ) ;
551
+ addHeader ( headers , pair . key , pair . value ) ;
552
+ }
553
+ if ( options . onHeaders ) {
554
+ options . onHeaders ( headers , statusCode ) ;
555
+ }
556
+ requestCallbacks . delete ( id ) ;
557
+ } , onLoading ( ) : void {
558
+ options . onLoading ( ) ;
559
+ requestCallbacks . delete ( id ) ;
560
+ } , onProgress ( lengthComputable : boolean , loaded : number , total : number ) : void {
561
+ if ( options . onProgress ) {
562
+ options . onProgress ( {
563
+ lengthComputable,
564
+ loaded,
565
+ total
566
+ } ) ;
567
+ }
568
+ requestCallbacks . delete ( id ) ;
569
+ } ,
570
+ onTimeout ( ) : void {
571
+ reject ( {
572
+ type : HttpError . Timeout
573
+ } ) ;
574
+ requestCallbacks . delete ( id ) ;
575
+ }
576
+ } ) ;
577
+ id = com . github . triniwiz . async . Async . Http . getFileRequest ( javaOptions , callback ) ;
578
+ requestCallbacks . set ( id , callback ) ;
579
+ } ;
580
+ makeRemoteRequest ( ) ;
581
+ requestIdCounter ++ ;
582
+ } catch ( ex ) {
583
+ reject ( {
584
+ type : HttpError . Error ,
585
+ message : ex . message
586
+ } ) ;
587
+ }
588
+ } ) ;
589
+ request [ 'cancel' ] = function ( ) {
590
+ com . github . triniwiz . async . Async . Http . cancelRequest ( id ) ;
591
+ } ;
592
+ return request ;
593
+ }
399
594
}
400
595
401
596
function serialize ( data : any ) : any {
0 commit comments