Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit e01dce4

Browse files
committed
Add IOS assets library resource support to fs.cp #112
1 parent 5f9a0b6 commit e01dce4

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,8 @@ private void done(Response resp) {
467467
}
468468
break;
469469
}
470-
// if(!resp.isSuccessful())
471-
// resp.body().close();
472-
resp.body().close();
470+
if(!resp.isSuccessful())
471+
resp.body().close();
473472
releaseTaskResource();
474473
}
475474

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,27 @@ - (NSDictionary *)constantsToExport
289289

290290
}
291291

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+
302313
}
303314

304315
RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {

src/ios/RNFetchBlobFS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
resolver:(RCTPromiseResolveBlock)resolve
6464
rejecter:(RCTPromiseRejectBlock)reject;
6565
//+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
66+
+ (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest;
6667

6768
// constructor
6869
- (id) init;

src/ios/RNFetchBlobFS.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
22
// RNFetchBlobFS.m
33
// RNFetchBlob
44
//
@@ -193,6 +193,7 @@ - (void) startAssetReadStream:(NSString *)assetUrl
193193

194194
+ (NSNumber *) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
195195
{
196+
196197
NSInputStream * is = [[NSInputStream alloc] initWithFileAtPath:src];
197198
NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
198199
[is open];
@@ -730,4 +731,21 @@ + (void) getPathFromUri:(NSString *)uri completionHandler:(void(^)(NSString * pa
730731
}
731732
}
732733

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+
733751
@end

0 commit comments

Comments
 (0)