react-native-blob-util 0.13.18 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +542 -401
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +89 -25
  4. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilBody.java +14 -15
  5. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +10 -6
  6. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +115 -289
  7. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +10 -0
  8. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java +314 -0
  9. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilPackage.java +6 -2
  10. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +46 -22
  11. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilStream.java +287 -0
  12. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java +57 -2
  13. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +9 -0
  14. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -0
  15. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -0
  16. package/class/ReactNativeBlobUtilBlobResponse.js +1 -1
  17. package/fs.js +44 -4
  18. package/index.d.ts +121 -1
  19. package/index.js +2 -0
  20. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.m +6 -4
  21. package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +6 -0
  22. package/ios/ReactNativeBlobUtilConst.h +1 -0
  23. package/ios/ReactNativeBlobUtilConst.m +1 -0
  24. package/ios/ReactNativeBlobUtilFS.h +3 -1
  25. package/ios/ReactNativeBlobUtilFS.m +33 -0
  26. package/ios/ReactNativeBlobUtilFileTransformer.h +24 -0
  27. package/ios/ReactNativeBlobUtilFileTransformer.m +21 -0
  28. package/ios/ReactNativeBlobUtilReqBuilder.m +2 -2
  29. package/ios/ReactNativeBlobUtilRequest.m +30 -1
  30. package/mediacollection.js +38 -0
  31. package/package.json +1 -1
  32. package/polyfill/Fetch.js +4 -2
  33. package/scripts/prelink.js +1 -1
  34. package/types.js +4 -0
package/index.d.ts CHANGED
@@ -6,17 +6,25 @@
6
6
  export const ReactNativeBlobUtil: ReactNativeBlobUtilStatic;
7
7
  export type ReactNativeBlobUtil = ReactNativeBlobUtilStatic;
8
8
  export default ReactNativeBlobUtil;
9
+ import {filedescriptor} from './types';
9
10
 
10
11
  interface ReactNativeBlobUtilStatic {
11
12
  fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: any
12
13
  | null): StatefulPromise<FetchBlobResponse>;
14
+
13
15
  base64: { encode(input: string): string; decode(input: string): string };
14
16
  android: AndroidApi;
15
17
  ios: IOSApi;
18
+
16
19
  config(options: ReactNativeBlobUtilConfig): ReactNativeBlobUtilStatic;
20
+
17
21
  session(name: string): ReactNativeBlobUtilSession;
22
+
18
23
  fs: FS;
24
+ MediaCollection: MediaCollection;
25
+
19
26
  wrap(path: string): string;
27
+
20
28
  net: Net;
21
29
  polyfill: Polyfill;
22
30
  // this require external module https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/oboe
@@ -45,10 +53,15 @@ export declare class ReactNativeBlobUtilFetchPolyfill {
45
53
 
46
54
  export interface ReactNativeBlobUtilFetchRepsonse {
47
55
  arrayBuffer(): Promise<any[]>;
56
+
48
57
  blob(): Promise<PolyfillBlob>;
58
+
49
59
  json(): Promise<any>;
60
+
50
61
  rawResp(): Promise<FetchBlobResponse>;
62
+
51
63
  text(): Promise<string>;
64
+
52
65
  bodyUsed: boolean;
53
66
  headers: any;
54
67
  ok: boolean;
@@ -64,46 +77,58 @@ export interface ReactNativeBlobUtilFetchRepsonse {
64
77
  */
65
78
  export interface FetchBlobResponse {
66
79
  taskId: string;
80
+
67
81
  /**
68
82
  * get path of response temp file
69
83
  * @return File path of temp file.
70
84
  */
71
85
  path(): string;
86
+
72
87
  type: "base64" | "path" | "utf8";
73
88
  data: any;
89
+
74
90
  /**
75
91
  * Convert result to javascript ReactNativeBlobUtil object.
76
92
  * @return Return a promise resolves Blob object.
77
93
  */
78
94
  blob(contentType: string, sliceSize: number): Promise<PolyfillBlob>;
95
+
79
96
  /**
80
97
  * Convert result to text.
81
98
  * @return Decoded base64 string.
82
99
  */
83
100
  text(): string | Promise<any>;
101
+
84
102
  /**
85
103
  * Convert result to JSON object.
86
104
  * @return Parsed javascript object.
87
105
  */
88
106
  json(): any;
107
+
89
108
  /**
90
109
  * Return BASE64 string directly.
91
110
  * @return BASE64 string of response body.
92
111
  */
93
112
  base64(): any;
113
+
94
114
  /**
95
115
  * Remove cahced file
96
116
  */
97
117
  flush(): void;
118
+
98
119
  respInfo: ReactNativeBlobUtilResponseInfo;
120
+
99
121
  info(): ReactNativeBlobUtilResponseInfo;
122
+
100
123
  session(name: string): ReactNativeBlobUtilSession | null;
124
+
101
125
  /**
102
126
  * Read file content with given encoding, if the response does not contains
103
127
  * a file path, show warning message
104
128
  * @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
105
129
  */
106
130
  readFile(encode: Encoding): Promise<any> | null;
131
+
107
132
  /**
108
133
  * Start read stream from cached file
109
134
  * @param encode Encode type, should be one of `base64`, `ascrii`, `utf8`.
@@ -113,17 +138,27 @@ export interface FetchBlobResponse {
113
138
 
114
139
  export interface PolyfillFileReader extends EventTarget {
115
140
  isRNFBPolyFill: boolean;
141
+
116
142
  onloadstart(e: Event): void;
143
+
117
144
  onprogress(e: Event): void;
145
+
118
146
  onload(e: Event): void;
147
+
119
148
  onabort(e: Event): void;
149
+
120
150
  onerror(e: Event): void;
151
+
121
152
  onloadend(e: Event): void;
122
153
 
123
154
  abort(): void;
155
+
124
156
  readAsArrayBuffer(b: PolyfillBlob): void;
157
+
125
158
  readAsBinaryString(b: PolyfillBlob): void;
159
+
126
160
  readAsText(b: PolyfillBlob, label?: string): void;
161
+
127
162
  readAsDataURL(b: PolyfillBlob): void;
128
163
 
129
164
  readyState: number;
@@ -242,6 +277,7 @@ export interface PolyfillXMLHttpRequest extends PolyfillXMLHttpRequestEventTarge
242
277
  getAllResponseHeaders(): string | null;
243
278
 
244
279
  onreadystatechange(e: Event): void;
280
+
245
281
  readyState: number;
246
282
  status: number;
247
283
  statusText: string;
@@ -270,11 +306,17 @@ export declare namespace PolyfillXMLHttpRequest {
270
306
 
271
307
  export interface PolyfillXMLHttpRequestEventTarget extends EventTarget {
272
308
  onabort(e: Event): void;
309
+
273
310
  onerror(e: Event): void;
311
+
274
312
  onload(e: Event): void;
313
+
275
314
  onloadstart(e: Event): void;
315
+
276
316
  onprogress(e: Event): void;
317
+
277
318
  ontimeout(e: Event): void;
319
+
278
320
  onloadend(e: Event): void;
279
321
  }
280
322
 
@@ -295,6 +337,7 @@ export interface Net {
295
337
  }
296
338
 
297
339
  type HashAlgorithm = "md5" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512";
340
+
298
341
  export interface FS {
299
342
  ReactNativeBlobUtilSession: ReactNativeBlobUtilSession;
300
343
 
@@ -356,6 +399,14 @@ export interface FS {
356
399
  */
357
400
  writeFile(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
358
401
 
402
+ /**
403
+ * Processes the data and then writes to the file.
404
+ * @param path Path of the file.
405
+ * @param data Data to write to the file.
406
+ * @param encoding Encoding of data (Optional).
407
+ */
408
+ writeFileWithTransform(path: string, data: string | number[], encoding?: Encoding): Promise<void>;
409
+
359
410
  appendFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>;
360
411
 
361
412
  /**
@@ -364,6 +415,14 @@ export interface FS {
364
415
  * @param encoding Encoding of read stream.
365
416
  */
366
417
  readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<any>;
418
+
419
+ /**
420
+ * Reads from a file and then processes the data before returning
421
+ * @param path Path of the file.
422
+ * @param encoding Encoding of read stream.
423
+ */
424
+ readFileWithTransform(path: string, encoding: Encoding, bufferSize?: number): Promise<any>;
425
+
367
426
  /**
368
427
  * Check if file exists and if it is a folder.
369
428
  * @param path Path to check
@@ -391,7 +450,9 @@ export interface FS {
391
450
  dirs: Dirs;
392
451
 
393
452
  slice(src: string, dest: string, start: number, end: number): Promise<void>;
453
+
394
454
  asset(path: string): string;
455
+
395
456
  df(): Promise<RNFetchBlobDf>;
396
457
  }
397
458
 
@@ -428,6 +489,7 @@ export interface ReactNativeBlobUtilWriteStream {
428
489
  append: boolean;
429
490
 
430
491
  write(data: string): Promise<void>;
492
+
431
493
  close(): void;
432
494
  }
433
495
 
@@ -526,7 +588,7 @@ export interface AndroidApi {
526
588
  actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<boolean | null>;
527
589
 
528
590
  /**
529
- *
591
+ *
530
592
  * This method brings up OS default file picker and resolves a file URI when the user selected a file.
531
593
  * However, it does not resolve or reject when user dismiss the file picker via pressing hardware back button,
532
594
  * but you can still handle this behavior via AppState.
@@ -651,6 +713,13 @@ export interface ReactNativeBlobUtilConfig {
651
713
  */
652
714
  fileCache?: boolean;
653
715
 
716
+ /**
717
+ * Set this property to true if you want the data to be processed before it gets written onto disk.
718
+ * This only has effect if the FileTransformer has been registered and the library is configured to write
719
+ * response onto disk.
720
+ */
721
+ transformFile?: boolean;
722
+
654
723
  /**
655
724
  * Set this property to change temp file extension that created by fetch response data.
656
725
  */
@@ -717,7 +786,9 @@ export interface ReactNativeBlobUtilResponseInfo {
717
786
 
718
787
  export interface ReactNativeBlobUtilStream {
719
788
  onData(): void;
789
+
720
790
  onError(): void;
791
+
721
792
  onEnd(): void;
722
793
  }
723
794
 
@@ -731,3 +802,52 @@ export declare class ReactNativeBlobUtilStat {
731
802
  path: string;
732
803
  filename: string;
733
804
  }
805
+
806
+ export type Mediatype = "Audio" | "Image" | "Video" | "Download";
807
+
808
+ export interface MediaCollection {
809
+ /**
810
+ * Creates a new File in the collection.
811
+ * Promise will resolve to content UIR or error message
812
+ * @param filedata descriptor for the media store entry
813
+ * @param mediatype
814
+ * @param path path of the file being copied
815
+ */
816
+ copyToMediaStore(filedata: filedescriptor, mediatype: Mediatype, path: string): Promise<string>;
817
+
818
+
819
+ /**
820
+ * Creates a new File in the collection.
821
+ * @param filedata
822
+ * @param mediatype
823
+ */
824
+ createMediafile(filedata: filedescriptor, mediatype: Mediatype): Promise<string>;
825
+
826
+ /**
827
+ * Copies an existing file to a mediastore file
828
+ * @param uri URI of the destination mediastore file
829
+ * @param path Path to the existing file which should be copied
830
+ */
831
+ writeToMediafile(uri: string, path: string): Promise<string>
832
+
833
+ /**
834
+ * Copies and transforms an existing file to a mediastore file. Make sure FileTransformer is set
835
+ * @param uri URI of the destination mediastore file
836
+ * @param path Path to the existing file which should be copied
837
+ */
838
+ writeToMediafileWithTransform(uri: string, path: string): Promise<string>
839
+
840
+ /**
841
+ * Copies a file from the mediastore to the apps internal storage
842
+ * @param contenturi URI of the mediastore file
843
+ * @param destpath Path for the file in the internal storage
844
+ */
845
+ copyToInternal(contenturi: string, destpath: string): Promise<string>
846
+
847
+ /**
848
+ * Gets the blob data for a given URI in the mediastore
849
+ * @param contenturi
850
+ * @param encoding
851
+ */
852
+ getBlob(contenturi: string, encoding: string): Promise<string>
853
+ }
package/index.js CHANGED
@@ -6,6 +6,7 @@ import {AppState, DeviceEventEmitter, NativeModules, Platform,} from 'react-nati
6
6
 
7
7
  //import StatefulPromise from './class/StatefulPromise.js'
8
8
  import fs from './fs';
9
+ import MediaCollection from './mediacollection';
9
10
  import base64 from 'base-64';
10
11
  import polyfill from './polyfill';
11
12
  import android from './android';
@@ -69,4 +70,5 @@ export default {
69
70
  wrap,
70
71
  polyfill,
71
72
  JSONStream,
73
+ MediaCollection
72
74
  };
@@ -76,6 +76,7 @@ RCT_EXPORT_MODULE();
76
76
  @"MovieDir" : [ReactNativeBlobUtilFS getMovieDir],
77
77
  @"MusicDir" : [ReactNativeBlobUtilFS getMusicDir],
78
78
  @"PictureDir" : [ReactNativeBlobUtilFS getPictureDir],
79
+ @"ApplicationSupportDir" : [ReactNativeBlobUtilFS getApplicationSupportDir],
79
80
  };
80
81
  }
81
82
 
@@ -251,9 +252,9 @@ RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callb
251
252
  }
252
253
 
253
254
  #pragma mark - fs.writeFile
254
- RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
255
+ RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data transformFile:(BOOL)transformFile append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
255
256
  {
256
- [ReactNativeBlobUtilFS writeFile:path encoding:[NSString stringWithString:encoding] data:data append:append resolver:resolve rejecter:reject];
257
+ [ReactNativeBlobUtilFS writeFile:path encoding:[NSString stringWithString:encoding] data:data transformFile:transformFile append:append resolver:resolve rejecter:reject];
257
258
  }
258
259
 
259
260
  #pragma mark - fs.writeArray
@@ -493,11 +494,12 @@ RCT_EXPORT_METHOD(mkdir:(NSString *)path resolver:(RCTPromiseResolveBlock)resolv
493
494
  #pragma mark - fs.readFile
494
495
  RCT_EXPORT_METHOD(readFile:(NSString *)path
495
496
  encoding:(NSString *)encoding
497
+ transformFile:(BOOL) transformFile
496
498
  resolver:(RCTPromiseResolveBlock)resolve
497
499
  rejecter:(RCTPromiseRejectBlock)reject)
498
500
  {
499
501
 
500
- [ReactNativeBlobUtilFS readFile:path encoding:encoding onComplete:^(NSData * content, NSString * code, NSString * err) {
502
+ [ReactNativeBlobUtilFS readFile:path encoding:encoding transformFile:transformFile onComplete:^(NSData * content, NSString * code, NSString * err) {
501
503
  if(err != nil) {
502
504
  reject(code, err, nil);
503
505
  return;
@@ -535,7 +537,7 @@ RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding
535
537
  });
536
538
  }
537
539
 
538
- #pragma mark - fs.getEnvionmentDirs
540
+ #pragma mark - fs.getEnvironmentDirs
539
541
  RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback)
540
542
  {
541
543
 
@@ -8,6 +8,7 @@
8
8
 
9
9
  /* Begin PBXBuildFile section */
10
10
  8C4801A6200CF71700FED7ED /* ReactNativeBlobUtilRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C4801A5200CF71700FED7ED /* ReactNativeBlobUtilRequest.m */; };
11
+ 9FD8D3C326F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 9FD8D3C226F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m */; };
11
12
  A158F4271D052E49006FFD38 /* ReactNativeBlobUtilFS.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F4261D052E49006FFD38 /* ReactNativeBlobUtilFS.m */; };
12
13
  A158F42D1D0535BB006FFD38 /* ReactNativeBlobUtilConst.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42C1D0535BB006FFD38 /* ReactNativeBlobUtilConst.m */; };
13
14
  A158F4301D0539DB006FFD38 /* ReactNativeBlobUtilNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* ReactNativeBlobUtilNetwork.m */; };
@@ -32,6 +33,8 @@
32
33
  /* Begin PBXFileReference section */
33
34
  8C4801A4200CF71700FED7ED /* ReactNativeBlobUtilRequest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativeBlobUtilRequest.h; sourceTree = "<group>"; };
34
35
  8C4801A5200CF71700FED7ED /* ReactNativeBlobUtilRequest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ReactNativeBlobUtilRequest.m; sourceTree = "<group>"; };
36
+ 9FD8D3C126F1709A00009F35 /* ReactNativeBlobUtilFileTransformer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativeBlobUtilFileTransformer.h; sourceTree = "<group>"; };
37
+ 9FD8D3C226F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ReactNativeBlobUtilFileTransformer.m; sourceTree = "<group>"; };
35
38
  A158F4261D052E49006FFD38 /* ReactNativeBlobUtilFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativeBlobUtilFS.m; sourceTree = "<group>"; };
36
39
  A158F4281D052E57006FFD38 /* ReactNativeBlobUtilFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativeBlobUtilFS.h; sourceTree = "<group>"; };
37
40
  A158F4291D0534A9006FFD38 /* ReactNativeBlobUtilConst.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativeBlobUtilConst.h; sourceTree = "<group>"; };
@@ -69,6 +72,8 @@
69
72
  A15C30051CD25C330074CB35 = {
70
73
  isa = PBXGroup;
71
74
  children = (
75
+ 9FD8D3C226F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m */,
76
+ 9FD8D3C126F1709A00009F35 /* ReactNativeBlobUtilFileTransformer.h */,
72
77
  A19B48241D98102400E6868A /* ReactNativeBlobUtilProgress.m */,
73
78
  A19B48231D98100800E6868A /* ReactNativeBlobUtilProgress.h */,
74
79
  A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
@@ -156,6 +161,7 @@
156
161
  A166D1AA1CE0647A00273590 /* ReactNativeBlobUtil.h in Sources */,
157
162
  8C4801A6200CF71700FED7ED /* ReactNativeBlobUtilRequest.m in Sources */,
158
163
  A158F42D1D0535BB006FFD38 /* ReactNativeBlobUtilConst.m in Sources */,
164
+ 9FD8D3C326F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m in Sources */,
159
165
  A158F4271D052E49006FFD38 /* ReactNativeBlobUtilFS.m in Sources */,
160
166
  A158F4301D0539DB006FFD38 /* ReactNativeBlobUtilNetwork.m in Sources */,
161
167
  A19B48251D98102400E6868A /* ReactNativeBlobUtilProgress.m in Sources */,
@@ -29,6 +29,7 @@ extern NSString *const AL_PREFIX;
29
29
 
30
30
  // config
31
31
  extern NSString *const CONFIG_USE_TEMP;
32
+ extern NSString *const CONFIG_TRANSFORM_FILE;
32
33
  extern NSString *const CONFIG_FILE_PATH;
33
34
  extern NSString *const CONFIG_FILE_EXT;
34
35
  extern NSString *const CONFIG_TRUSTY;
@@ -13,6 +13,7 @@ NSString *const AL_PREFIX = @"assets-library://";
13
13
 
14
14
  // fetch configs
15
15
  NSString *const CONFIG_USE_TEMP = @"fileCache";
16
+ NSString *const CONFIG_TRANSFORM_FILE = @"transformFile";
16
17
  NSString *const CONFIG_FILE_PATH = @"path";
17
18
  NSString *const CONFIG_FILE_EXT = @"appendExt";
18
19
  NSString *const CONFIG_TRUSTY = @"trusty";
@@ -54,6 +54,7 @@
54
54
  + (NSString *) getMovieDir;
55
55
  + (NSString *) getMusicDir;
56
56
  + (NSString *) getPictureDir;
57
+ + (NSString *) getApplicationSupportDir;
57
58
  + (NSString *) getTempPath;
58
59
  + (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext;
59
60
  + (NSString *) getPathOfAsset:(NSString *)assetURI;
@@ -71,7 +72,8 @@
71
72
  + (NSDictionary *) stat:(NSString *) path error:(NSError **) error;
72
73
  + (void) exists:(NSString *) path callback:(RCTResponseSenderBlock)callback;
73
74
  + (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
74
- + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
75
+ + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data transformFile:(BOOL)transformFile append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
76
+ + (void) readFile:(NSString *)path encoding:(NSString *)encoding transformFile:(BOOL)transformFile onComplete:(void (^)(NSData * content, NSString* code, NSString * errMsg))onComplete;
75
77
  + (void) readFile:(NSString *)path encoding:(NSString *)encoding onComplete:(void (^)(NSData * content, NSString* code, NSString * errMsg))onComplete;
76
78
  + (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
77
79
  + (void) slice:(NSString *)path
@@ -10,6 +10,7 @@
10
10
  #import "ReactNativeBlobUtil.h"
11
11
  #import "ReactNativeBlobUtilFS.h"
12
12
  #import "ReactNativeBlobUtilConst.h"
13
+ #import "ReactNativeBlobUtilFileTransformer.h"
13
14
  #import "IOS7Polyfill.h"
14
15
  @import AssetsLibrary;
15
16
 
@@ -124,6 +125,10 @@ NSMutableDictionary *fileStreams = nil;
124
125
  return [NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES) firstObject];
125
126
  }
126
127
 
128
+ + (NSString *) getApplicationSupportDir {
129
+ return [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
130
+ }
131
+
127
132
  + (NSString *) getTempPath {
128
133
 
129
134
  return NSTemporaryDirectory();
@@ -343,6 +348,7 @@ NSMutableDictionary *fileStreams = nil;
343
348
  + (void) writeFile:(NSString *)path
344
349
  encoding:(NSString *)encoding
345
350
  data:(NSString *)data
351
+ transformFile:(BOOL)transformFile
346
352
  append:(BOOL)append
347
353
  resolver:(RCTPromiseResolveBlock)resolve
348
354
  rejecter:(RCTPromiseRejectBlock)reject
@@ -390,6 +396,16 @@ NSMutableDictionary *fileStreams = nil;
390
396
  else {
391
397
  content = [data dataUsingEncoding:NSUTF8StringEncoding];
392
398
  }
399
+
400
+ if (transformFile) {
401
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
402
+ if (fileTransformer) {
403
+ content = [fileTransformer onWriteFile:content];
404
+ } else {
405
+ return reject(@"EUNSPECIFIED",@"Transform specified but transformer not set", nil);
406
+ }
407
+ }
408
+
393
409
  if(append == YES) {
394
410
  [fileHandle seekToEndOfFile];
395
411
  [fileHandle writeData:content];
@@ -484,6 +500,7 @@ NSMutableDictionary *fileStreams = nil;
484
500
 
485
501
  + (void) readFile:(NSString *)path
486
502
  encoding:(NSString *)encoding
503
+ transformFile:(BOOL) transformFile
487
504
  onComplete:(void (^)(NSData * content, NSString * codeStr, NSString * errMsg))onComplete
488
505
  {
489
506
  [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
@@ -518,6 +535,22 @@ NSMutableDictionary *fileStreams = nil;
518
535
 
519
536
  }
520
537
 
538
+ if (transformFile) {
539
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
540
+ if (fileTransformer) {
541
+ @try{
542
+ fileContent = [fileTransformer onReadFile:fileContent];
543
+ } @catch (NSException * ex)
544
+ {
545
+ onComplete(nil, @"EUNSPECIFIED", [NSString stringWithFormat:@"Exception on File Transformer: '%@' ", [ex description]]);
546
+ return;
547
+ }
548
+ } else {
549
+ onComplete(nil, @"EUNSPECIFIED", @"Transform specified but transformer not set");
550
+ return;
551
+ }
552
+ }
553
+
521
554
  if(encoding != nil)
522
555
  {
523
556
  if([[encoding lowercaseString] isEqualToString:@"utf8"])
@@ -0,0 +1,24 @@
1
+ // ReactNativeBlobUtilFileTransformer.h
2
+ // ReactNativeBlobUtil
3
+ //
4
+
5
+ #ifndef ReactNativeBlobUtilDataConverter_h
6
+ #define ReactNativeBlobUtilDataConverter_h
7
+
8
+ #import <UIKit/UIKit.h>
9
+
10
+
11
+ @protocol FileTransformer <NSObject>
12
+ - (NSData*) onWriteFile:(NSData*)data;
13
+ - (NSData*) onReadFile:(NSData*)data;
14
+ @end
15
+
16
+ @interface ReactNativeBlobUtilFileTransformer : NSObject
17
+
18
+ + (void) setFileTransformer:(NSObject<FileTransformer>*) fileTransformer;
19
+ + (NSObject<FileTransformer>*) getFileTransformer;
20
+
21
+ @end
22
+
23
+
24
+ #endif /* ReactNativeBlobUtilFileTransformer_h */
@@ -0,0 +1,21 @@
1
+ // ReactNativeBlobUtilFileTransformer.m
2
+ // ReactNativeBlobUtil
3
+ //
4
+
5
+ #import "ReactNativeBlobUtilFileTransformer.h"
6
+
7
+ static NSObject<FileTransformer> *sharedFileTransformer;
8
+
9
+ @implementation ReactNativeBlobUtilFileTransformer
10
+
11
+ + (void) setFileTransformer:(NSObject<FileTransformer> *)fileTransformer {
12
+ sharedFileTransformer = fileTransformer;
13
+ }
14
+
15
+ + (NSObject<FileTransformer>*) getFileTransformer {
16
+ return sharedFileTransformer;
17
+ }
18
+
19
+ @end
20
+
21
+ // PHOENIX:PATCH-PACKAGE END
@@ -119,7 +119,7 @@
119
119
  if([orgPath hasPrefix:AL_PREFIX])
120
120
  {
121
121
 
122
- [ReactNativeBlobUtilFS readFile:orgPath encoding:nil onComplete:^(id content, NSString* code, NSString * err) {
122
+ [ReactNativeBlobUtilFS readFile:orgPath encoding:nil transformFile:false onComplete:^(id content, NSString* code, NSString * err) {
123
123
  if(err != nil)
124
124
  {
125
125
  onComplete(nil, nil);
@@ -223,7 +223,7 @@
223
223
  NSString * orgPath = [content substringFromIndex:[FILE_PREFIX length]];
224
224
  orgPath = [ReactNativeBlobUtilFS getPathOfAsset:orgPath];
225
225
 
226
- [ReactNativeBlobUtilFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString* code, NSString * err) {
226
+ [ReactNativeBlobUtilFS readFile:orgPath encoding:nil transformFile:false onComplete:^(NSData *content, NSString* code, NSString * err) {
227
227
  if(err != nil)
228
228
  {
229
229
  onComplete(formData, YES);
@@ -10,6 +10,7 @@
10
10
 
11
11
  #import "ReactNativeBlobUtilFS.h"
12
12
  #import "ReactNativeBlobUtilConst.h"
13
+ #import "ReactNativeBlobUtilFileTransformer.h"
13
14
  #import "ReactNativeBlobUtilReqBuilder.h"
14
15
 
15
16
  #import "IOS7Polyfill.h"
@@ -156,6 +157,12 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
156
157
  } else {
157
158
  destPath = [ReactNativeBlobUtilFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
158
159
  }
160
+
161
+ // We still need to initialize this as a placeholder in memory before we perform
162
+ // the conversion
163
+ if ([self ShouldTransformFile]) {
164
+ respData = [[NSMutableData alloc] init];
165
+ }
159
166
  } else {
160
167
  respData = [[NSMutableData alloc] init];
161
168
  respFile = NO;
@@ -333,7 +340,9 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
333
340
  chunkString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
334
341
  }
335
342
 
336
- if (respFile) {
343
+ // If we need to process the data, we defer writing into the file until the we have all the data, at which point
344
+ // we can perform the processing and then write into the file
345
+ if (respFile && ![self ShouldTransformFile]) {
337
346
  [writeStream write:[data bytes] maxLength:[data length]];
338
347
  } else {
339
348
  [respData appendData:data];
@@ -390,6 +399,22 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
390
399
  }
391
400
 
392
401
  if (respFile) {
402
+ if ([self ShouldTransformFile]) {
403
+ // At this point, we have the data that we deferred to write into a file. We can now
404
+ // perform the conversion and write the converted data into the file.
405
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
406
+ if (!fileTransformer) {
407
+ errMsg = @"Transform file specified but file transfomer not set";
408
+ } else {
409
+ @try{
410
+ NSData* transformedData = [fileTransformer onWriteFile:respData];
411
+ [writeStream write:[transformedData bytes] maxLength:[transformedData length]];
412
+ } @catch(NSException * ex)
413
+ {
414
+ errMsg = [NSString stringWithFormat:@"Exception on File Transformer: '%@' ", [ex description]];
415
+ }
416
+ }
417
+ }
393
418
  [writeStream close];
394
419
  rnfbRespType = RESP_TYPE_PATH;
395
420
  respStr = destPath;
@@ -430,6 +455,10 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
430
455
 
431
456
  }
432
457
 
458
+ - (BOOL) ShouldTransformFile{
459
+ return [[options valueForKey:CONFIG_TRANSFORM_FILE] boolValue];
460
+ }
461
+
433
462
  // upload progress handler
434
463
  - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
435
464
  {
@@ -0,0 +1,38 @@
1
+ import {NativeModules} from 'react-native';
2
+ import type {ReactNativeBlobUtilNative, filedescriptor} from "types";
3
+
4
+ const ReactNativeBlobUtil: ReactNativeBlobUtilNative = NativeModules.ReactNativeBlobUtil;
5
+
6
+ function createMediafile(fd: filedescriptor, mediatype: string): Promise {
7
+ if ((!'parentFolder' in fd)) fd['parentFolder'] = '';
8
+ return ReactNativeBlobUtil.createMediaFile(fd, mediatype);
9
+ }
10
+
11
+ function writeToMediafile(uri: string, path: string) {
12
+ return ReactNativeBlobUtil.writeToMediaFile(uri, path, false);
13
+ }
14
+
15
+ function writeToMediafileWithTransform(uri: string, path: string) {
16
+ return ReactNativeBlobUtil.writeToMediaFile(uri, path, true);
17
+ }
18
+
19
+ function copyToInternal(contenturi: string, destpath: string) {
20
+ return ReactNativeBlobUtil.copyToInternal(contenturi, destpath);
21
+ }
22
+
23
+ function getBlob(contenturi: string, encoding: string) {
24
+ return ReactNativeBlobUtil.getBlob(contenturi, encoding);
25
+ }
26
+
27
+ function copyToMediaStore(fd: filedescriptor, mediatype: string, path: string) {
28
+ return ReactNativeBlobUtil.copyToMediaStore(fd, mediatype, path);
29
+ }
30
+
31
+ export default {
32
+ createMediafile,
33
+ writeToMediafile,
34
+ writeToMediafileWithTransform,
35
+ copyToInternal,
36
+ getBlob,
37
+ copyToMediaStore
38
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-blob-util",
3
- "version": "0.13.18",
3
+ "version": "0.15.0",
4
4
  "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
5
5
  "main": "index.js",
6
6
  "scripts": {