1
- import { ReactNativeBlobUtilResponseInfo , ReactNativeBlobUtilStream } from "../types" ;
2
- import fs from "../fs" ;
3
- import Blob from "../polyfill/Blob" ;
4
- import ReactNativeBlobUtilSession from "./ReactNativeBlobUtilSession" ;
5
- import URIUtil from "../utils/uri" ;
6
-
7
- /**
8
- * ReactNativeBlobUtil response object class.
9
- */
10
- export class FetchBlobResponse {
11
-
12
- taskId : string ;
13
- path : ( ) => string | null ;
14
- type : 'base64' | 'path' | 'utf8' ;
15
- data : any ;
16
- blob : ( contentType : string , sliceSize : number ) => Promise < Blob > ;
17
- text : ( ) => string | Promise < any > ;
18
- json : ( ) => any ;
19
- base64 : ( ) => any ;
20
- flush : ( ) => void ;
21
- respInfo : ReactNativeBlobUtilResponseInfo ;
22
- session : ( name : string ) => ReactNativeBlobUtilSession | null ;
23
- readFile : ( encode : 'base64' | 'utf8' | 'ascii' ) => ?Promise < any > ;
24
- readStream : (
25
- encode : 'utf8' | 'ascii' | 'base64' ,
26
- ) => ReactNativeBlobUtilStream | null ;
27
-
28
- constructor ( taskId : string , info : ReactNativeBlobUtilResponseInfo , data : any ) {
29
- this . data = data ;
30
- this . taskId = taskId ;
31
- this . type = info . rnfbEncode ;
32
- this . respInfo = info ;
33
-
34
- this . info = ( ) : ReactNativeBlobUtilResponseInfo => {
35
- return this . respInfo ;
36
- } ;
37
-
38
- this . array = ( ) : Promise < Array > => {
39
- let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
40
- return new Promise ( ( resolve , reject ) => {
41
- switch ( this . type ) {
42
- case 'base64' :
43
- // TODO : base64 to array buffer
44
- break ;
45
- case 'path' :
46
- fs . readFile ( this . data , 'ascii' ) . then ( resolve ) ;
47
- break ;
48
- default :
49
- // TODO : text to array buffer
50
- break ;
51
- }
52
- } ) ;
53
- } ;
54
-
55
- /**
56
- * Convert result to javascript ReactNativeBlobUtil object.
57
- * @return {Promise<Blob> } Return a promise resolves Blob object.
58
- */
59
- this . blob = ( ) : Promise < Blob > => {
60
- let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
61
- return new Promise ( ( resolve , reject ) => {
62
- switch ( this . type ) {
63
- case 'base64' :
64
- Blob . build ( this . data , { type : cType + ';BASE64' } ) . then ( resolve ) ;
65
- break ;
66
- case 'path' :
67
- Blob . build ( URIUtil . wrap ( this . data ) , { type : cType } ) . then ( resolve ) ;
68
- break ;
69
- default :
70
- Blob . build ( this . data , { type : 'text/plain' } ) . then ( resolve ) ;
71
- break ;
72
- }
73
- } ) ;
74
- } ;
75
- /**
76
- * Convert result to text.
77
- * @return {string } Decoded base64 string.
78
- */
79
- this . text = ( ) : string | Promise < any > => {
80
- switch ( this . type ) {
81
- case 'base64' :
82
- return base64 . decode ( this . data ) ;
83
- case 'path' :
84
- return fs . readFile ( this . data , 'base64' ) . then ( ( b64 ) => Promise . resolve ( base64 . decode ( b64 ) ) ) ;
85
- default:
86
- return this . data ;
87
- }
88
- } ;
89
- /**
90
- * Convert result to JSON object.
91
- * @return {object } Parsed javascript object.
92
- */
93
- this . json = ( ) : any => {
94
- switch ( this . type ) {
95
- case 'base64' :
96
- return JSON . parse ( base64 . decode ( this . data ) ) ;
97
- case 'path' :
98
- return fs . readFile ( this . data , 'utf8' )
99
- . then ( ( text ) => Promise . resolve ( JSON . parse ( text ) ) ) ;
100
- default :
101
- return JSON . parse ( this . data ) ;
102
- }
103
- } ;
104
- /**
105
- * Return BASE64 string directly.
106
- * @return {string } BASE64 string of response body.
107
- */
108
- this . base64 = ( ) : string | Promise < any > => {
109
- switch ( this . type ) {
110
- case 'base64' :
111
- return this . data ;
112
- case 'path' :
113
- return fs . readFile ( this . data , 'base64' ) ;
114
- default:
115
- return base64 . encode ( this . data ) ;
116
- }
117
- } ;
118
- /**
119
- * Remove cahced file
120
- * @return {Promise }
121
- */
122
- this . flush = ( ) => {
123
- let path = this . path ( ) ;
124
- if ( ! path || this . type !== 'path' )
125
- return ;
126
- return fs . unlink ( path ) ;
127
- } ;
128
- /**
129
- * get path of response temp file
130
- * @return {string } File path of temp file.
131
- */
132
- this . path = ( ) => {
133
- if ( this . type === 'path' )
134
- return this . data ;
135
- return null ;
136
- } ;
137
-
138
- this . session = ( name : string ) : ReactNativeBlobUtilSession | null = > {
139
- if ( this . type === 'path' )
140
- return fs . session ( name ) . add ( this . data ) ;
141
- else {
142
- console . warn ( 'only file paths can be add into session.' ) ;
143
- return null ;
144
- }
145
- } ;
146
- /**
147
- * Start read stream from cached file
148
- * @param {String } encoding Encode type, should be one of `base64`, `ascii`, `utf8`.
149
- * @return {void }
150
- */
151
- this . readStream = ( encoding : 'base64' | 'utf8' | 'ascii' ) : ReactNativeBlobUtilStream | null = > {
152
- if ( this . type === 'path' ) {
153
- return fs . readStream ( this . data , encoding ) ;
154
- }
155
- else {
156
- console . warn ( 'ReactNativeBlobUtil' , 'this response data does not contains any available stream' ) ;
157
- return null ;
158
- }
159
- } ;
160
- /**
161
- * Read file content with given encoding, if the response does not contains
162
- * a file path, show warning message
163
- * @param {String } encoding Encode type, should be one of `base64`, `ascrii`, `utf8`.
164
- * @return {String }
165
- */
166
- this . readFile = ( encoding : 'base64' | 'utf8' | 'ascii' ) => {
167
- if ( this . type === 'path' ) {
168
- return fs . readFile ( this . data , encoding ) ;
169
- }
170
- else {
171
- console . warn ( 'ReactNativeBlobUtil' , 'this response does not contains a readable file' ) ;
172
- return null ;
173
- }
174
- } ;
175
- }
176
-
1
+ import { ReactNativeBlobUtilResponseInfo , ReactNativeBlobUtilStream } from "../types" ;
2
+ import fs from "../fs" ;
3
+ import Blob from "../polyfill/Blob" ;
4
+ import ReactNativeBlobUtilSession from "./ReactNativeBlobUtilSession" ;
5
+ import URIUtil from "../utils/uri" ;
6
+ import base64 from "base-64" ;
7
+ /**
8
+ * ReactNativeBlobUtil response object class.
9
+ */
10
+ export class FetchBlobResponse {
11
+
12
+ taskId : string ;
13
+ path : ( ) => string | null ;
14
+ type : 'base64' | 'path' | 'utf8' ;
15
+ data : any ;
16
+ blob : ( contentType : string , sliceSize : number ) => Promise < Blob > ;
17
+ text : ( ) => string | Promise < any > ;
18
+ json : ( ) => any ;
19
+ base64 : ( ) => any ;
20
+ flush : ( ) => void ;
21
+ respInfo : ReactNativeBlobUtilResponseInfo ;
22
+ session : ( name : string ) => ReactNativeBlobUtilSession | null ;
23
+ readFile : ( encode : 'base64' | 'utf8' | 'ascii' ) => ?Promise < any > ;
24
+ readStream : (
25
+ encode : 'utf8' | 'ascii' | 'base64' ,
26
+ ) => ReactNativeBlobUtilStream | null ;
27
+
28
+ constructor ( taskId : string , info : ReactNativeBlobUtilResponseInfo , data : any ) {
29
+ this . data = data ;
30
+ this . taskId = taskId ;
31
+ this . type = info . rnfbEncode ;
32
+ this . respInfo = info ;
33
+
34
+ this . info = ( ) : ReactNativeBlobUtilResponseInfo => {
35
+ return this . respInfo ;
36
+ } ;
37
+
38
+ this . array = ( ) : Promise < Array > => {
39
+ let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
40
+ return new Promise ( ( resolve , reject ) => {
41
+ switch ( this . type ) {
42
+ case 'base64' :
43
+ // TODO : base64 to array buffer
44
+ break ;
45
+ case 'path' :
46
+ fs . readFile ( this . data , 'ascii' ) . then ( resolve ) ;
47
+ break ;
48
+ default :
49
+ // TODO : text to array buffer
50
+ break ;
51
+ }
52
+ } ) ;
53
+ } ;
54
+
55
+ /**
56
+ * Convert result to javascript ReactNativeBlobUtil object.
57
+ * @return {Promise<Blob> } Return a promise resolves Blob object.
58
+ */
59
+ this . blob = ( ) : Promise < Blob > => {
60
+ let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
61
+ return new Promise ( ( resolve , reject ) => {
62
+ switch ( this . type ) {
63
+ case 'base64' :
64
+ Blob . build ( this . data , { type : cType + ';BASE64' } ) . then ( resolve ) ;
65
+ break ;
66
+ case 'path' :
67
+ Blob . build ( URIUtil . wrap ( this . data ) , { type : cType } ) . then ( resolve ) ;
68
+ break ;
69
+ default :
70
+ Blob . build ( this . data , { type : 'text/plain' } ) . then ( resolve ) ;
71
+ break ;
72
+ }
73
+ } ) ;
74
+ } ;
75
+ /**
76
+ * Convert result to text.
77
+ * @return {string } Decoded base64 string.
78
+ */
79
+ this . text = ( ) : string | Promise < any > => {
80
+ switch ( this . type ) {
81
+ case 'base64' :
82
+ return base64 . decode ( this . data ) ;
83
+ case 'path' :
84
+ return fs . readFile ( this . data , 'base64' ) . then ( ( b64 ) => Promise . resolve ( base64 . decode ( b64 ) ) ) ;
85
+ default:
86
+ return this . data ;
87
+ }
88
+ } ;
89
+ /**
90
+ * Convert result to JSON object.
91
+ * @return {object } Parsed javascript object.
92
+ */
93
+ this . json = ( ) : any => {
94
+ switch ( this . type ) {
95
+ case 'base64' :
96
+ return JSON . parse ( base64 . decode ( this . data ) ) ;
97
+ case 'path' :
98
+ return fs . readFile ( this . data , 'utf8' )
99
+ . then ( ( text ) => Promise . resolve ( JSON . parse ( text ) ) ) ;
100
+ default :
101
+ return JSON . parse ( this . data ) ;
102
+ }
103
+ } ;
104
+ /**
105
+ * Return BASE64 string directly.
106
+ * @return {string } BASE64 string of response body.
107
+ */
108
+ this . base64 = ( ) : string | Promise < any > => {
109
+ switch ( this . type ) {
110
+ case 'base64' :
111
+ return this . data ;
112
+ case 'path' :
113
+ return fs . readFile ( this . data , 'base64' ) ;
114
+ default:
115
+ return base64 . encode ( this . data ) ;
116
+ }
117
+ } ;
118
+ /**
119
+ * Remove cahced file
120
+ * @return {Promise }
121
+ */
122
+ this . flush = ( ) => {
123
+ let path = this . path ( ) ;
124
+ if ( ! path || this . type !== 'path' )
125
+ return ;
126
+ return fs . unlink ( path ) ;
127
+ } ;
128
+ /**
129
+ * get path of response temp file
130
+ * @return {string } File path of temp file.
131
+ */
132
+ this . path = ( ) => {
133
+ if ( this . type === 'path' )
134
+ return this . data ;
135
+ return null ;
136
+ } ;
137
+
138
+ this . session = ( name : string ) : ReactNativeBlobUtilSession | null = > {
139
+ if ( this . type === 'path' )
140
+ return fs . session ( name ) . add ( this . data ) ;
141
+ else {
142
+ console . warn ( 'only file paths can be add into session.' ) ;
143
+ return null ;
144
+ }
145
+ } ;
146
+ /**
147
+ * Start read stream from cached file
148
+ * @param {String } encoding Encode type, should be one of `base64`, `ascii`, `utf8`.
149
+ * @return {void }
150
+ */
151
+ this . readStream = ( encoding : 'base64' | 'utf8' | 'ascii' ) : ReactNativeBlobUtilStream | null = > {
152
+ if ( this . type === 'path' ) {
153
+ return fs . readStream ( this . data , encoding ) ;
154
+ }
155
+ else {
156
+ console . warn ( 'ReactNativeBlobUtil' , 'this response data does not contains any available stream' ) ;
157
+ return null ;
158
+ }
159
+ } ;
160
+ /**
161
+ * Read file content with given encoding, if the response does not contains
162
+ * a file path, show warning message
163
+ * @param {String } encoding Encode type, should be one of `base64`, `ascrii`, `utf8`.
164
+ * @return {String }
165
+ */
166
+ this . readFile = ( encoding : 'base64' | 'utf8' | 'ascii' ) => {
167
+ if ( this . type === 'path' ) {
168
+ return fs . readFile ( this . data , encoding ) ;
169
+ }
170
+ else {
171
+ console . warn ( 'ReactNativeBlobUtil' , 'this response does not contains a readable file' ) ;
172
+ return null ;
173
+ }
174
+ } ;
175
+ }
176
+
177
177
}
0 commit comments