react-native-blob-util 0.14.0 → 0.16.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 (29) hide show
  1. package/README.md +100 -0
  2. package/android/src/main/AndroidManifest.xml +0 -3
  3. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +11 -12
  4. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilBody.java +30 -6
  5. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +2 -0
  6. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +94 -3
  7. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +10 -0
  8. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java +123 -87
  9. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +99 -1
  10. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java +5 -17
  11. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +4 -0
  12. package/class/ReactNativeBlobUtilBlobResponse.js +1 -1
  13. package/fs.js +42 -3
  14. package/index.d.ts +30 -1
  15. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.m +4 -3
  16. package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +6 -2
  17. package/ios/ReactNativeBlobUtilConst.h +1 -0
  18. package/ios/ReactNativeBlobUtilConst.m +1 -0
  19. package/ios/ReactNativeBlobUtilFS.h +2 -1
  20. package/ios/ReactNativeBlobUtilFS.m +30 -2
  21. package/ios/ReactNativeBlobUtilFileTransformer.h +24 -0
  22. package/ios/ReactNativeBlobUtilFileTransformer.m +21 -0
  23. package/ios/ReactNativeBlobUtilReqBuilder.m +3 -4
  24. package/ios/ReactNativeBlobUtilRequest.m +35 -7
  25. package/mediacollection.js +6 -1
  26. package/package.json +1 -1
  27. package/polyfill/Fetch.js +4 -2
  28. package/types.js +1 -0
  29. package/ios/IOS7Polyfill.h +0 -27
@@ -252,9 +252,9 @@ RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callb
252
252
  }
253
253
 
254
254
  #pragma mark - fs.writeFile
255
- 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)
256
256
  {
257
- [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];
258
258
  }
259
259
 
260
260
  #pragma mark - fs.writeArray
@@ -494,11 +494,12 @@ RCT_EXPORT_METHOD(mkdir:(NSString *)path resolver:(RCTPromiseResolveBlock)resolv
494
494
  #pragma mark - fs.readFile
495
495
  RCT_EXPORT_METHOD(readFile:(NSString *)path
496
496
  encoding:(NSString *)encoding
497
+ transformFile:(BOOL) transformFile
497
498
  resolver:(RCTPromiseResolveBlock)resolve
498
499
  rejecter:(RCTPromiseRejectBlock)reject)
499
500
  {
500
501
 
501
- [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) {
502
503
  if(err != nil) {
503
504
  reject(code, err, nil);
504
505
  return;
@@ -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>"; };
@@ -45,7 +48,6 @@
45
48
  A19B48241D98102400E6868A /* ReactNativeBlobUtilProgress.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativeBlobUtilProgress.m; sourceTree = "<group>"; };
46
49
  A1AAE2971D300E3E0051D11C /* ReactNativeBlobUtilReqBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ReactNativeBlobUtilReqBuilder.h; sourceTree = "<group>"; };
47
50
  A1AAE2981D300E4D0051D11C /* ReactNativeBlobUtilReqBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ReactNativeBlobUtilReqBuilder.m; sourceTree = "<group>"; };
48
- A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = IOS7Polyfill.h; sourceTree = "<group>"; };
49
51
  /* End PBXFileReference section */
50
52
 
51
53
  /* Begin PBXFrameworksBuildPhase section */
@@ -69,9 +71,10 @@
69
71
  A15C30051CD25C330074CB35 = {
70
72
  isa = PBXGroup;
71
73
  children = (
74
+ 9FD8D3C226F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m */,
75
+ 9FD8D3C126F1709A00009F35 /* ReactNativeBlobUtilFileTransformer.h */,
72
76
  A19B48241D98102400E6868A /* ReactNativeBlobUtilProgress.m */,
73
77
  A19B48231D98100800E6868A /* ReactNativeBlobUtilProgress.h */,
74
- A1F950181D7E9134002A95A6 /* IOS7Polyfill.h */,
75
78
  A1AAE2981D300E4D0051D11C /* ReactNativeBlobUtilReqBuilder.m */,
76
79
  A1AAE2971D300E3E0051D11C /* ReactNativeBlobUtilReqBuilder.h */,
77
80
  A158F42E1D0539CE006FFD38 /* ReactNativeBlobUtilNetwork.h */,
@@ -156,6 +159,7 @@
156
159
  A166D1AA1CE0647A00273590 /* ReactNativeBlobUtil.h in Sources */,
157
160
  8C4801A6200CF71700FED7ED /* ReactNativeBlobUtilRequest.m in Sources */,
158
161
  A158F42D1D0535BB006FFD38 /* ReactNativeBlobUtilConst.m in Sources */,
162
+ 9FD8D3C326F1736000009F35 /* ReactNativeBlobUtilFileTransformer.m in Sources */,
159
163
  A158F4271D052E49006FFD38 /* ReactNativeBlobUtilFS.m in Sources */,
160
164
  A158F4301D0539DB006FFD38 /* ReactNativeBlobUtilNetwork.m in Sources */,
161
165
  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";
@@ -72,7 +72,8 @@
72
72
  + (NSDictionary *) stat:(NSString *) path error:(NSError **) error;
73
73
  + (void) exists:(NSString *) path callback:(RCTResponseSenderBlock)callback;
74
74
  + (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
75
- + (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;
76
77
  + (void) readFile:(NSString *)path encoding:(NSString *)encoding onComplete:(void (^)(NSData * content, NSString* code, NSString * errMsg))onComplete;
77
78
  + (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
78
79
  + (void) slice:(NSString *)path
@@ -10,7 +10,7 @@
10
10
  #import "ReactNativeBlobUtil.h"
11
11
  #import "ReactNativeBlobUtilFS.h"
12
12
  #import "ReactNativeBlobUtilConst.h"
13
- #import "IOS7Polyfill.h"
13
+ #import "ReactNativeBlobUtilFileTransformer.h"
14
14
  @import AssetsLibrary;
15
15
 
16
16
  #import <CommonCrypto/CommonDigest.h>
@@ -347,6 +347,7 @@ NSMutableDictionary *fileStreams = nil;
347
347
  + (void) writeFile:(NSString *)path
348
348
  encoding:(NSString *)encoding
349
349
  data:(NSString *)data
350
+ transformFile:(BOOL)transformFile
350
351
  append:(BOOL)append
351
352
  resolver:(RCTPromiseResolveBlock)resolve
352
353
  rejecter:(RCTPromiseRejectBlock)reject
@@ -379,7 +380,7 @@ NSMutableDictionary *fileStreams = nil;
379
380
 
380
381
  NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
381
382
  NSData * content = nil;
382
- if([encoding RNFBContainsString:@"base64"]) {
383
+ if([encoding containsString:@"base64"]) {
383
384
  content = [[NSData alloc] initWithBase64EncodedString:data options:0];
384
385
  }
385
386
  else if([encoding isEqualToString:@"uri"]) {
@@ -394,6 +395,16 @@ NSMutableDictionary *fileStreams = nil;
394
395
  else {
395
396
  content = [data dataUsingEncoding:NSUTF8StringEncoding];
396
397
  }
398
+
399
+ if (transformFile) {
400
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
401
+ if (fileTransformer) {
402
+ content = [fileTransformer onWriteFile:content];
403
+ } else {
404
+ return reject(@"EUNSPECIFIED",@"Transform specified but transformer not set", nil);
405
+ }
406
+ }
407
+
397
408
  if(append == YES) {
398
409
  [fileHandle seekToEndOfFile];
399
410
  [fileHandle writeData:content];
@@ -488,6 +499,7 @@ NSMutableDictionary *fileStreams = nil;
488
499
 
489
500
  + (void) readFile:(NSString *)path
490
501
  encoding:(NSString *)encoding
502
+ transformFile:(BOOL) transformFile
491
503
  onComplete:(void (^)(NSData * content, NSString * codeStr, NSString * errMsg))onComplete
492
504
  {
493
505
  [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
@@ -522,6 +534,22 @@ NSMutableDictionary *fileStreams = nil;
522
534
 
523
535
  }
524
536
 
537
+ if (transformFile) {
538
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
539
+ if (fileTransformer) {
540
+ @try{
541
+ fileContent = [fileTransformer onReadFile:fileContent];
542
+ } @catch (NSException * ex)
543
+ {
544
+ onComplete(nil, @"EUNSPECIFIED", [NSString stringWithFormat:@"Exception on File Transformer: '%@' ", [ex description]]);
545
+ return;
546
+ }
547
+ } else {
548
+ onComplete(nil, @"EUNSPECIFIED", @"Transform specified but transformer not set");
549
+ return;
550
+ }
551
+ }
552
+
525
553
  if(encoding != nil)
526
554
  {
527
555
  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
@@ -11,7 +11,6 @@
11
11
  #import "ReactNativeBlobUtilNetwork.h"
12
12
  #import "ReactNativeBlobUtilConst.h"
13
13
  #import "ReactNativeBlobUtilFS.h"
14
- #import "IOS7Polyfill.h"
15
14
 
16
15
  #if __has_include(<React/RCTAssert.h>)
17
16
  #import <React/RCTLog.h>
@@ -119,7 +118,7 @@
119
118
  if([orgPath hasPrefix:AL_PREFIX])
120
119
  {
121
120
 
122
- [ReactNativeBlobUtilFS readFile:orgPath encoding:nil onComplete:^(id content, NSString* code, NSString * err) {
121
+ [ReactNativeBlobUtilFS readFile:orgPath encoding:nil transformFile:false onComplete:^(id content, NSString* code, NSString * err) {
123
122
  if(err != nil)
124
123
  {
125
124
  onComplete(nil, nil);
@@ -151,7 +150,7 @@
151
150
 
152
151
  __block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
153
152
  // when content-type is application/octet* decode body string using BASE64 decoder
154
- if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] RNFBContainsString:@";base64"])
153
+ if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] containsString:@";base64"])
155
154
  {
156
155
  __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
157
156
  if([mheaders valueForKey:@"content-type"] != nil)
@@ -223,7 +222,7 @@
223
222
  NSString * orgPath = [content substringFromIndex:[FILE_PREFIX length]];
224
223
  orgPath = [ReactNativeBlobUtilFS getPathOfAsset:orgPath];
225
224
 
226
- [ReactNativeBlobUtilFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString* code, NSString * err) {
225
+ [ReactNativeBlobUtilFS readFile:orgPath encoding:nil transformFile:false onComplete:^(NSData *content, NSString* code, NSString * err) {
227
226
  if(err != nil)
228
227
  {
229
228
  onComplete(formData, YES);
@@ -10,9 +10,9 @@
10
10
 
11
11
  #import "ReactNativeBlobUtilFS.h"
12
12
  #import "ReactNativeBlobUtilConst.h"
13
+ #import "ReactNativeBlobUtilFileTransformer.h"
13
14
  #import "ReactNativeBlobUtilReqBuilder.h"
14
15
 
15
- #import "IOS7Polyfill.h"
16
16
  #import <CommonCrypto/CommonDigest.h>
17
17
 
18
18
 
@@ -156,6 +156,12 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
156
156
  } else {
157
157
  destPath = [ReactNativeBlobUtilFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
158
158
  }
159
+
160
+ // We still need to initialize this as a placeholder in memory before we perform
161
+ // the conversion
162
+ if ([self ShouldTransformFile]) {
163
+ respData = [[NSMutableData alloc] init];
164
+ }
159
165
  } else {
160
166
  respData = [[NSMutableData alloc] init];
161
167
  respFile = NO;
@@ -215,20 +221,20 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
215
221
 
216
222
  return;
217
223
  } else {
218
- self.isServerPush = [[respCType lowercaseString] RNFBContainsString:@"multipart/x-mixed-replace;"];
224
+ self.isServerPush = [[respCType lowercaseString] containsString:@"multipart/x-mixed-replace;"];
219
225
  }
220
226
 
221
227
  if(respCType)
222
228
  {
223
229
  NSArray * extraBlobCTypes = [options objectForKey:CONFIG_EXTRA_BLOB_CTYPE];
224
230
 
225
- if ([respCType RNFBContainsString:@"text/"]) {
231
+ if ([respCType containsString:@"text/"]) {
226
232
  respType = @"text";
227
- } else if ([respCType RNFBContainsString:@"application/json"]) {
233
+ } else if ([respCType containsString:@"application/json"]) {
228
234
  respType = @"json";
229
235
  } else if(extraBlobCTypes) { // If extra blob content type is not empty, check if response type matches
230
236
  for (NSString * substr in extraBlobCTypes) {
231
- if ([respCType RNFBContainsString:[substr lowercaseString]]) {
237
+ if ([respCType containsString:[substr lowercaseString]]) {
232
238
  respType = @"blob";
233
239
  respFile = YES;
234
240
  destPath = [ReactNativeBlobUtilFS getTempPath:taskId withExtension:nil];
@@ -286,7 +292,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
286
292
 
287
293
  // if not set overwrite in options, defaults to TRUE
288
294
  BOOL overwrite = [options valueForKey:@"overwrite"] == nil ? YES : [[options valueForKey:@"overwrite"] boolValue];
289
- BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
295
+ BOOL appendToExistingFile = [destPath containsString:@"?append=true"];
290
296
 
291
297
  appendToExistingFile = !overwrite;
292
298
 
@@ -333,7 +339,9 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
333
339
  chunkString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
334
340
  }
335
341
 
336
- if (respFile) {
342
+ // If we need to process the data, we defer writing into the file until the we have all the data, at which point
343
+ // we can perform the processing and then write into the file
344
+ if (respFile && ![self ShouldTransformFile]) {
337
345
  [writeStream write:[data bytes] maxLength:[data length]];
338
346
  } else {
339
347
  [respData appendData:data];
@@ -390,6 +398,22 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
390
398
  }
391
399
 
392
400
  if (respFile) {
401
+ if ([self ShouldTransformFile]) {
402
+ // At this point, we have the data that we deferred to write into a file. We can now
403
+ // perform the conversion and write the converted data into the file.
404
+ NSObject<FileTransformer>* fileTransformer = [ReactNativeBlobUtilFileTransformer getFileTransformer];
405
+ if (!fileTransformer) {
406
+ errMsg = @"Transform file specified but file transfomer not set";
407
+ } else {
408
+ @try{
409
+ NSData* transformedData = [fileTransformer onWriteFile:respData];
410
+ [writeStream write:[transformedData bytes] maxLength:[transformedData length]];
411
+ } @catch(NSException * ex)
412
+ {
413
+ errMsg = [NSString stringWithFormat:@"Exception on File Transformer: '%@' ", [ex description]];
414
+ }
415
+ }
416
+ }
393
417
  [writeStream close];
394
418
  rnfbRespType = RESP_TYPE_PATH;
395
419
  respStr = destPath;
@@ -430,6 +454,10 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
430
454
 
431
455
  }
432
456
 
457
+ - (BOOL) ShouldTransformFile{
458
+ return [[options valueForKey:CONFIG_TRANSFORM_FILE] boolValue];
459
+ }
460
+
433
461
  // upload progress handler
434
462
  - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
435
463
  {
@@ -9,7 +9,11 @@ function createMediafile(fd: filedescriptor, mediatype: string): Promise {
9
9
  }
10
10
 
11
11
  function writeToMediafile(uri: string, path: string) {
12
- return ReactNativeBlobUtil.writeToMediaFile(uri, path);
12
+ return ReactNativeBlobUtil.writeToMediaFile(uri, path, false);
13
+ }
14
+
15
+ function writeToMediafileWithTransform(uri: string, path: string) {
16
+ return ReactNativeBlobUtil.writeToMediaFile(uri, path, true);
13
17
  }
14
18
 
15
19
  function copyToInternal(contenturi: string, destpath: string) {
@@ -27,6 +31,7 @@ function copyToMediaStore(fd: filedescriptor, mediatype: string, path: string) {
27
31
  export default {
28
32
  createMediafile,
29
33
  writeToMediafile,
34
+ writeToMediafileWithTransform,
30
35
  copyToInternal,
31
36
  getBlob,
32
37
  copyToMediaStore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-blob-util",
3
- "version": "0.14.0",
3
+ "version": "0.16.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": {
package/polyfill/Fetch.js CHANGED
@@ -59,10 +59,12 @@ class ReactNativeBlobUtilFetchPolyfill {
59
59
  // task.then is not, so we have to extend task.then with progress and
60
60
  // cancel function
61
61
  let progressHandler, uploadHandler, cancelHandler;
62
+ let scopedTask = null;
62
63
  let statefulPromise = promise
63
64
  .then((body) => {
64
65
  let task = RNconfig(config)
65
66
  .fetch(options.method, url, options.headers, body);
67
+ scopedTask = task;
66
68
  if (progressHandler)
67
69
  task.progress(progressHandler);
68
70
  if (uploadHandler)
@@ -87,8 +89,8 @@ class ReactNativeBlobUtilFetchPolyfill {
87
89
  };
88
90
  statefulPromise.cancel = () => {
89
91
  cancelHandler = true;
90
- if (task.cancel)
91
- task.cancel();
92
+ if (scopedTask && scopedTask.cancel)
93
+ scopedTask.cancel();
92
94
  };
93
95
 
94
96
  return statefulPromise;
package/types.js CHANGED
@@ -3,6 +3,7 @@ export type ReactNativeBlobUtilConfig = {
3
3
  Progress: any,
4
4
  UploadProgress: any,
5
5
  fileCache : bool,
6
+ transformFile: boolean;
6
7
  path : string,
7
8
  appendExt : string,
8
9
  session : string,
@@ -1,27 +0,0 @@
1
- //
2
- // IOS7Polyfill.h
3
- // ReactNativeBlobUtil
4
- //
5
- // Created by Ben Hsieh on 2016/9/6.
6
- // Copyright © 2016年 wkh237.github.io. All rights reserved.
7
- //
8
-
9
- #ifndef IOS7Polyfill_h
10
- #define IOS7Polyfill_h
11
-
12
- @interface NSString (Contains)
13
-
14
- - (BOOL)RNFBContainsString:(NSString*)other;
15
-
16
- @end
17
-
18
- @implementation NSString (Contains)
19
-
20
- - (BOOL)RNFBContainsString:(NSString*)other {
21
- NSRange range = [self rangeOfString:other];
22
- return range.length != 0;
23
- }
24
-
25
-
26
- @end
27
- #endif /* IOS7Polyfill_h */