Browse Source

Add IOS assets library resource support to fs.cp #112

Ben Hsieh 8 years ago
parent
commit
e01dce4312

+ 2
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

467
                 }
467
                 }
468
                 break;
468
                 break;
469
         }
469
         }
470
-//        if(!resp.isSuccessful())
471
-//            resp.body().close();
472
-        resp.body().close();
470
+        if(!resp.isSuccessful())
471
+            resp.body().close();
473
         releaseTaskResource();
472
         releaseTaskResource();
474
     }
473
     }
475
 
474
 

+ 21
- 10
src/ios/RNFetchBlob/RNFetchBlob.m View File

289
 
289
 
290
 }
290
 }
291
 
291
 
292
-RCT_EXPORT_METHOD(cp:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
293
-    NSError * error = nil;
294
-    path = [RNFetchBlobFS getPathOfAsset:path];
295
-    BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
296
-
297
-    if(error == nil)
298
-        callback(@[[NSNull null], @YES]);
299
-    else
300
-        callback(@[[error localizedDescription], @NO]);
301
-
292
+RCT_EXPORT_METHOD(cp:(NSString*)src toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
293
+    
294
+//    path = [RNFetchBlobFS getPathOfAsset:path];
295
+    [RNFetchBlobFS getPathFromUri:src completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
296
+        NSError * error = nil;
297
+        if(path == nil)
298
+        {
299
+            [RNFetchBlobFS writeAssetToPath:asset dest:dest];
300
+            callback(@[[NSNull null], @YES]);
301
+        }
302
+        else
303
+        {
304
+            BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
305
+            
306
+            if(error == nil)
307
+                callback(@[[NSNull null], @YES]);
308
+            else
309
+                callback(@[[error localizedDescription], @NO]);
310
+        }
311
+    }];
312
+    
302
 }
313
 }
303
 
314
 
304
 RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
315
 RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {

+ 1
- 0
src/ios/RNFetchBlobFS.h View File

63
      resolver:(RCTPromiseResolveBlock)resolve
63
      resolver:(RCTPromiseResolveBlock)resolve
64
      rejecter:(RCTPromiseRejectBlock)reject;
64
      rejecter:(RCTPromiseRejectBlock)reject;
65
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
65
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
66
++ (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest;
66
 
67
 
67
 // constructor
68
 // constructor
68
 - (id) init;
69
 - (id) init;

+ 19
- 1
src/ios/RNFetchBlobFS.m View File

1
-//
1
+
2
 //  RNFetchBlobFS.m
2
 //  RNFetchBlobFS.m
3
 //  RNFetchBlob
3
 //  RNFetchBlob
4
 //
4
 //
193
 
193
 
194
 + (NSNumber *) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
194
 + (NSNumber *) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
195
 {
195
 {
196
+    
196
     NSInputStream * is = [[NSInputStream alloc] initWithFileAtPath:src];
197
     NSInputStream * is = [[NSInputStream alloc] initWithFileAtPath:src];
197
     NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
198
     NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
198
     [is open];
199
     [is open];
730
     }
731
     }
731
 }
732
 }
732
 
733
 
734
++ (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest
735
+{
736
+    int read = 0;
737
+    int cursor = 0;
738
+    Byte * buffer = (Byte *)malloc(10240);
739
+    NSOutputStream * ostream = [[NSOutputStream alloc] initToFileAtPath:dest append:NO];
740
+    [ostream open];
741
+    while((read = [rep getBytes:buffer fromOffset:cursor length:10240 error:nil]) > 0)
742
+    {
743
+        cursor+=10240;
744
+        [ostream write:buffer maxLength:read];
745
+    }
746
+    [ostream close];
747
+    free(buffer);
748
+    return;
749
+}
750
+
733
 @end
751
 @end