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

Commit 9c0e531

Browse files
committed
Fix cp and stat for assets #45
1 parent e267dc5 commit 9c0e531

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ static void writeChunk(String streamId, String data, Callback callback) {
344344
try {
345345
stream.write(chunk);
346346
callback.invoke();
347-
chunk = null;
348347
} catch (Exception e) {
349348
callback.invoke(e.getLocalizedMessage());
350349
}
@@ -431,8 +430,7 @@ static void cp(String path, String dest, Callback callback) {
431430

432431
try {
433432

434-
String destFolder = new File(dest).getPath();
435-
if(!new File(path).exists()) {
433+
if(!isPathExists(path)) {
436434
callback.invoke("cp error: source file at path`" + path + "` not exists");
437435
return;
438436
}
@@ -490,7 +488,6 @@ static void exists(String path, Callback callback) {
490488
try {
491489
String filename = path.replace(assetPrefix, "");
492490
AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
493-
// TODO : handle asset folder
494491
callback.invoke(true, false);
495492
} catch (IOException e) {
496493
callback.invoke(false, false);
@@ -512,22 +509,21 @@ static void exists(String path, Callback callback) {
512509
static void ls(String path, Callback callback) {
513510
path = normalizePath(path);
514511
File src = new File(path);
515-
// TODO : handle asset folder
516-
if(!src.exists() || !src.isDirectory()) {
512+
if (!src.exists() || !src.isDirectory()) {
517513
callback.invoke("ls error: failed to list path `" + path + "` for it is not exist or it is not a folder");
518514
return;
519515
}
520-
String [] files = new File(path).list();
516+
String[] files = new File(path).list();
521517
WritableArray arg = Arguments.createArray();
522-
for(String i : files) {
518+
for (String i : files) {
523519
arg.pushString(i);
524520
}
525521
callback.invoke(null, arg);
526522
}
527523

528524
static void lstat(String path, final Callback callback) {
529525
path = normalizePath(path);
530-
File src = new File(path);
526+
531527
new AsyncTask<String, Integer, Integer>() {
532528
@Override
533529
protected Integer doInBackground(String ...args) {

src/fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function session(name:string):RNFetchBlobSession {
4848
}
4949
}
5050

51-
function addAssetPrefix(path:string):string {
51+
function asset(path:string):string {
5252
if(Platform.OS === 'ios') {
5353
// path from camera roll
5454
if(/^assets-library\:\/\//.test(path))
@@ -335,5 +335,5 @@ export default {
335335
lstat,
336336
scanFile,
337337
dirs,
338-
addAssetPrefix
338+
asset
339339
}

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ - (NSDictionary *)constantsToExport
334334
BOOL exist = nil;
335335
BOOL isDir = nil;
336336
NSError * error = nil;
337+
338+
path = [RNFetchBlobFS getPathOfAsset:path];
339+
337340
exist = [fm fileExistsAtPath:path isDirectory:&isDir];
338341
if(exist == NO) {
339342
callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
@@ -352,6 +355,9 @@ - (NSDictionary *)constantsToExport
352355
NSFileManager* fm = [NSFileManager defaultManager];
353356
BOOL exist = nil;
354357
BOOL isDir = nil;
358+
359+
path = [RNFetchBlobFS getPathOfAsset:path];
360+
355361
exist = [fm fileExistsAtPath:path isDirectory:&isDir];
356362
if(exist == NO) {
357363
callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
@@ -380,6 +386,7 @@ - (NSDictionary *)constantsToExport
380386

381387
RCT_EXPORT_METHOD(cp:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
382388
NSError * error = nil;
389+
path = [RNFetchBlobFS getPathOfAsset:path];
383390
BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
384391

385392
if(error == nil)

0 commit comments

Comments
 (0)