1
+ import { ReactNativeBlobUtilResponseInfo , ReactNativeBlobUtilStream } from "types" ;
2
+ import fs from "fs" ;
3
+ import polyfill from "polyfill" ;
4
+
5
+ /**
6
+ * ReactNativeBlobUtil response object class.
7
+ */
8
+ export class FetchBlobResponse {
9
+
10
+ taskId : string ;
11
+ path : ( ) => string | null ;
12
+ type : 'base64' | 'path' | 'utf8' ;
13
+ data : any ;
14
+ blob : ( contentType : string , sliceSize : number ) => Promise < Blob > ;
15
+ text : ( ) => string | Promise < any > ;
16
+ json : ( ) => any ;
17
+ base64 : ( ) => any ;
18
+ flush : ( ) => void ;
19
+ respInfo : ReactNativeBlobUtilResponseInfo ;
20
+ session : ( name : string ) => ReactNativeBlobUtilSession | null ;
21
+ readFile : ( encode : 'base64' | 'utf8' | 'ascii' ) => ?Promise < any > ;
22
+ readStream : (
23
+ encode : 'utf8' | 'ascii' | 'base64' ,
24
+ ) => ReactNativeBlobUtilStream | null ;
25
+
26
+ constructor ( taskId : string , info : ReactNativeBlobUtilResponseInfo , data : any ) {
27
+ this . data = data ;
28
+ this . taskId = taskId ;
29
+ this . type = info . rnfbEncode ;
30
+ this . respInfo = info ;
31
+
32
+ this . info = ( ) : ReactNativeBlobUtilResponseInfo => {
33
+ return this . respInfo ;
34
+ } ;
35
+
36
+ this . array = ( ) : Promise < Array > => {
37
+ let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
38
+ return new Promise ( ( resolve , reject ) => {
39
+ switch ( this . type ) {
40
+ case 'base64' :
41
+ // TODO : base64 to array buffer
42
+ break ;
43
+ case 'path' :
44
+ fs . readFile ( this . data , 'ascii' ) . then ( resolve ) ;
45
+ break ;
46
+ default :
47
+ // TODO : text to array buffer
48
+ break ;
49
+ }
50
+ } ) ;
51
+ } ;
52
+
53
+ /**
54
+ * Convert result to javascript ReactNativeBlobUtil object.
55
+ * @return {Promise<Blob> } Return a promise resolves Blob object.
56
+ */
57
+ this . blob = ( ) : Promise < Blob > => {
58
+ let Blob = polyfill . Blob ;
59
+ let cType = info . headers [ 'Content-Type' ] || info . headers [ 'content-type' ] ;
60
+ return new Promise ( ( resolve , reject ) => {
61
+ switch ( this . type ) {
62
+ case 'base64' :
63
+ Blob . build ( this . data , { type : cType + ';BASE64' } ) . then ( resolve ) ;
64
+ break ;
65
+ case 'path' :
66
+ polyfill . Blob . build ( wrap ( this . data ) , { type : cType } ) . then ( resolve ) ;
67
+ break ;
68
+ default :
69
+ polyfill . Blob . build ( this . data , { type : 'text/plain' } ) . then ( resolve ) ;
70
+ break ;
71
+ }
72
+ } ) ;
73
+ } ;
74
+ /**
75
+ * Convert result to text.
76
+ * @return {string } Decoded base64 string.
77
+ */
78
+ this . text = ( ) : string | Promise < any > => {
79
+ let res = this . data ;
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 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 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 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 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
+ }
0 commit comments