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
 
116
 
117
     @ReactMethod
117
     @ReactMethod
118
     public void lstat(String path, Callback callback) {
118
     public void lstat(String path, Callback callback) {
119
-        RNFetchBlobFS.ls(path, callback);
119
+        RNFetchBlobFS.lstat(path, callback);
120
     }
120
     }
121
 
121
 
122
     @ReactMethod
122
     @ReactMethod

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

376
                 if(src.isDirectory()) {
376
                 if(src.isDirectory()) {
377
                     String [] files = src.list();
377
                     String [] files = src.list();
378
                     for(String p : files) {
378
                     for(String p : files) {
379
-                        res.pushMap(statFile ( src.getPath() + p));
379
+                        res.pushMap(statFile ( src.getPath() + "/" + p));
380
                     }
380
                     }
381
                 }
381
                 }
382
                 else {
382
                 else {
394
      * @param callback
394
      * @param callback
395
      */
395
      */
396
     static void stat(String path, Callback callback) {
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
     void scanFile(String [] path, String[] mimes, final Callback callback) {
416
     void scanFile(String [] path, String[] mimes, final Callback callback) {
453
     static void createFileASCII(String path, ReadableArray data, Callback callback) {
458
     static void createFileASCII(String path, ReadableArray data, Callback callback) {
454
         try {
459
         try {
455
             File dest = new File(path);
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
             boolean created = dest.createNewFile();
465
             boolean created = dest.createNewFile();
457
             if(!created) {
466
             if(!created) {
458
                 callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
467
                 callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");