Quellcode durchsuchen

Fix cp and stat for assets #45

Ben Hsieh vor 8 Jahren
Ursprung
Commit
9c0e531e91

+ 5
- 9
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Datei anzeigen

344
         try {
344
         try {
345
             stream.write(chunk);
345
             stream.write(chunk);
346
             callback.invoke();
346
             callback.invoke();
347
-            chunk = null;
348
         } catch (Exception e) {
347
         } catch (Exception e) {
349
             callback.invoke(e.getLocalizedMessage());
348
             callback.invoke(e.getLocalizedMessage());
350
         }
349
         }
431
 
430
 
432
         try {
431
         try {
433
 
432
 
434
-            String destFolder = new File(dest).getPath();
435
-            if(!new File(path).exists()) {
433
+            if(!isPathExists(path)) {
436
                 callback.invoke("cp error: source file at path`" + path + "` not exists");
434
                 callback.invoke("cp error: source file at path`" + path + "` not exists");
437
                 return;
435
                 return;
438
             }
436
             }
490
             try {
488
             try {
491
                 String filename = path.replace(assetPrefix, "");
489
                 String filename = path.replace(assetPrefix, "");
492
                 AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
490
                 AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
493
-                // TODO : handle asset folder
494
                 callback.invoke(true, false);
491
                 callback.invoke(true, false);
495
             } catch (IOException e) {
492
             } catch (IOException e) {
496
                 callback.invoke(false, false);
493
                 callback.invoke(false, false);
512
     static void ls(String path, Callback callback) {
509
     static void ls(String path, Callback callback) {
513
         path = normalizePath(path);
510
         path = normalizePath(path);
514
         File src = new File(path);
511
         File src = new File(path);
515
-        // TODO : handle asset folder
516
-        if(!src.exists() || !src.isDirectory()) {
512
+        if (!src.exists() || !src.isDirectory()) {
517
             callback.invoke("ls error: failed to list path `" + path + "` for it is not exist or it is not a folder");
513
             callback.invoke("ls error: failed to list path `" + path + "` for it is not exist or it is not a folder");
518
             return;
514
             return;
519
         }
515
         }
520
-        String [] files = new File(path).list();
516
+        String[] files = new File(path).list();
521
         WritableArray arg = Arguments.createArray();
517
         WritableArray arg = Arguments.createArray();
522
-        for(String i : files) {
518
+        for (String i : files) {
523
             arg.pushString(i);
519
             arg.pushString(i);
524
         }
520
         }
525
         callback.invoke(null, arg);
521
         callback.invoke(null, arg);
527
 
523
 
528
     static void lstat(String path, final Callback callback) {
524
     static void lstat(String path, final Callback callback) {
529
         path = normalizePath(path);
525
         path = normalizePath(path);
530
-        File src = new File(path);
526
+
531
         new AsyncTask<String, Integer, Integer>() {
527
         new AsyncTask<String, Integer, Integer>() {
532
             @Override
528
             @Override
533
             protected Integer doInBackground(String ...args) {
529
             protected Integer doInBackground(String ...args) {

+ 2
- 2
src/fs.js Datei anzeigen

48
   }
48
   }
49
 }
49
 }
50
 
50
 
51
-function addAssetPrefix(path:string):string {
51
+function asset(path:string):string {
52
   if(Platform.OS === 'ios') {
52
   if(Platform.OS === 'ios') {
53
     // path from camera roll
53
     // path from camera roll
54
     if(/^assets-library\:\/\//.test(path))
54
     if(/^assets-library\:\/\//.test(path))
335
   lstat,
335
   lstat,
336
   scanFile,
336
   scanFile,
337
   dirs,
337
   dirs,
338
-  addAssetPrefix
338
+  asset
339
 }
339
 }

+ 7
- 0
src/ios/RNFetchBlob/RNFetchBlob.m Datei anzeigen

334
     BOOL exist = nil;
334
     BOOL exist = nil;
335
     BOOL isDir = nil;
335
     BOOL isDir = nil;
336
     NSError * error = nil;
336
     NSError * error = nil;
337
+    
338
+    path = [RNFetchBlobFS getPathOfAsset:path];
339
+    
337
     exist = [fm fileExistsAtPath:path isDirectory:&isDir];
340
     exist = [fm fileExistsAtPath:path isDirectory:&isDir];
338
     if(exist == NO) {
341
     if(exist == NO) {
339
         callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
342
         callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
352
     NSFileManager* fm = [NSFileManager defaultManager];
355
     NSFileManager* fm = [NSFileManager defaultManager];
353
     BOOL exist = nil;
356
     BOOL exist = nil;
354
     BOOL isDir = nil;
357
     BOOL isDir = nil;
358
+    
359
+    path = [RNFetchBlobFS getPathOfAsset:path];
360
+    
355
     exist = [fm fileExistsAtPath:path isDirectory:&isDir];
361
     exist = [fm fileExistsAtPath:path isDirectory:&isDir];
356
     if(exist == NO) {
362
     if(exist == NO) {
357
         callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
363
         callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
380
 
386
 
381
 RCT_EXPORT_METHOD(cp:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
387
 RCT_EXPORT_METHOD(cp:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
382
     NSError * error = nil;
388
     NSError * error = nil;
389
+    path = [RNFetchBlobFS getPathOfAsset:path];
383
     BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
390
     BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
384
     
391
     
385
     if(error == nil)
392
     if(error == nil)