Browse Source

Fix android stat and lstat method

Ben Hsieh 8 years ago
parent
commit
2224a469e2

+ 1
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -116,7 +116,7 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
116 116
 
117 117
     @ReactMethod
118 118
     public void lstat(String path, Callback callback) {
119
-        RNFetchBlobFS.ls(path, callback);
119
+        RNFetchBlobFS.lstat(path, callback);
120 120
     }
121 121
 
122 122
     @ReactMethod

+ 21
- 12
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -376,7 +376,7 @@ public class RNFetchBlobFS {
376 376
                 if(src.isDirectory()) {
377 377
                     String [] files = src.list();
378 378
                     for(String p : files) {
379
-                        res.pushMap(statFile ( src.getPath() + p));
379
+                        res.pushMap(statFile ( src.getPath() + "/" + p));
380 380
                     }
381 381
                 }
382 382
                 else {
@@ -394,18 +394,23 @@ public class RNFetchBlobFS {
394 394
      * @param callback
395 395
      */
396 396
     static void stat(String path, Callback callback) {
397
-        File target = new File(path);
398
-        if(!target.exists()) {
399
-            callback.invoke("stat error: file "+path+" does not exists");
400
-            return;
397
+        try {
398
+            File target = new File(path);
399
+            if (!target.exists()) {
400
+                callback.invoke("stat error: file " + path + " does not exists");
401
+                return;
402
+            }
403
+            WritableMap stat = Arguments.createMap();
404
+            stat.putString("filename", target.getName());
405
+            stat.putString("path", target.getPath());
406
+            stat.putString("type", target.isDirectory() ? "directory" : "file");
407
+            stat.putString("size", String.valueOf(target.length()));
408
+            String lastModified = String.valueOf(target.lastModified());
409
+            stat.putString("lastModified", lastModified);
410
+            callback.invoke(null, stat);
411
+        } catch(Exception err) {
412
+            callback.invoke(err.getLocalizedMessage());
401 413
         }
402
-        WritableMap stat = Arguments.createMap();
403
-        stat.putString("filename", target.getName());
404
-        stat.putString("path", target.getPath());
405
-        stat.putString("type", target.isDirectory() ? "directory" : "file");
406
-        stat.putInt("size", (int)target.length());
407
-        stat.putInt("lastModified", (int)target.lastModified());
408
-        callback.invoke(null, stat);
409 414
     }
410 415
 
411 416
     void scanFile(String [] path, String[] mimes, final Callback callback) {
@@ -453,6 +458,10 @@ public class RNFetchBlobFS {
453 458
     static void createFileASCII(String path, ReadableArray data, Callback callback) {
454 459
         try {
455 460
             File dest = new File(path);
461
+            if(dest.exists()) {
462
+                callback.invoke("create file error: failed to create file at path `" + path + "`, file already exists.");
463
+                return;
464
+            }
456 465
             boolean created = dest.createNewFile();
457 466
             if(!created) {
458 467
                 callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");