@@ -160,7 +160,20 @@ function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
160
160
if ( typeof path !== 'string' ) {
161
161
return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) ) ;
162
162
}
163
- return ReactNativeBlobUtil . readFile ( path , encoding ) ;
163
+ return ReactNativeBlobUtil . readFile ( path , encoding, false) ;
164
+ }
165
+
166
+ /**
167
+ * Reads the file, then transforms it before returning the content
168
+ * @param {string } path Path of the file.
169
+ * @param {'base64' | 'utf8' | 'ascii' } encoding Encoding of read stream.
170
+ * @return {Promise<Array<number> | string> }
171
+ */
172
+ function readFileWithTransform ( path : string , encoding : string = 'utf8' ) : Promise < any > {
173
+ if ( typeof path !== 'string' ) {
174
+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) )
175
+ }
176
+ return ReactNativeBlobUtil . readFile ( path , encoding, true) ;
164
177
}
165
178
166
179
/**
@@ -186,7 +199,31 @@ function writeFile(path: string, data: string | Array<number>, encoding: ?string
186
199
return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( `"data" must be a String when encoding is "utf8" or "base64", but it is "${ typeof data } "` ) ) ) ;
187
200
}
188
201
else
189
- return ReactNativeBlobUtil . writeFile ( path , encoding , data , false ) ;
202
+ return ReactNativeBlobUtil . writeFile ( path , encoding , data , false , false ) ;
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Transforms the data and then writes to the file.
208
+ * @param {string } path Path of the file.
209
+ * @param {string | number[] } data Data to write to the file.
210
+ * @param {string } encoding Encoding of data (Optional).
211
+ * @return {Promise }
212
+ */
213
+ function writeFileWithTransform ( path : string , data : string | Array < number > , encoding : ?string = 'utf8' ) : Promise {
214
+ if ( typeof path !== 'string' ) {
215
+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'Missing argument "path" ' ) ) )
216
+ }
217
+ if ( encoding . toLocaleLowerCase ( ) === 'ascii' ) {
218
+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( 'ascii is not supported for converted files' ) ) )
219
+ }
220
+ else {
221
+ if ( typeof data !== 'string' ) {
222
+ return Promise . reject ( addCode ( 'EINVAL' , new TypeError ( `"data" must be a String when encoding is "utf8" or "base64", but it is "${ typeof data } "` ) ) )
223
+ }
224
+
225
+ else
226
+ return ReactNativeBlobUtil . writeFile ( path , encoding , data , true , false )
190
227
}
191
228
}
192
229
@@ -415,6 +452,8 @@ export default {
415
452
cp,
416
453
writeStream,
417
454
writeFile,
455
+ writeFileWithTransform,
456
+ readFileWithTransform,
418
457
appendFile,
419
458
pathForAppGroup,
420
459
syncPathAppGroup,
0 commit comments