浏览代码

Added android implementation close #19

Ben Hsieh 8 年前
父节点
当前提交
1d3dcfae72

+ 19
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java 查看文件

@@ -45,10 +45,13 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
45 45
 
46 46
     @ReactMethod
47 47
     public void getEnvironmentDirs(Callback callback) {
48
-
49 48
         ReactApplicationContext ctx = this.getReactApplicationContext();
50 49
         RNFetchBlobFS.getSystemfolders(ctx, callback);
50
+    }
51 51
 
52
+    @ReactMethod
53
+    public void createFile(String path, String content, String encode, Callback callback) {
54
+        RNFetchBlobFS.createFile(path, content, encode, callback);
52 55
     }
53 56
 
54 57
     @ReactMethod
@@ -56,6 +59,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
56 59
         RNFetchBlobFS.unlink(path, callback);
57 60
     }
58 61
 
62
+    @ReactMethod
63
+    public void mkdir(String path, Callback callback) {
64
+        RNFetchBlobFS.mkdir(path, callback);
65
+    }
66
+
59 67
     @ReactMethod
60 68
     public void exists(String path, Callback callback) {
61 69
         RNFetchBlobFS.exists(path, callback);
@@ -86,6 +94,16 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
86 94
         RNFetchBlobFS.writeChunk(streamId, data, callback);
87 95
     }
88 96
 
97
+    @ReactMethod
98
+    public void closeStream(String streamId, Callback callback) {
99
+        RNFetchBlobFS.closeStream(streamId, callback);
100
+    }
101
+
102
+    @ReactMethod
103
+    public void removeSession(ReadableArray paths, Callback callback) {
104
+        RNFetchBlobFS.removeSession(paths, callback);
105
+    }
106
+
89 107
     @ReactMethod
90 108
     /**
91 109
      * @param path Stream file path

+ 68
- 30
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

@@ -6,6 +6,8 @@ import android.os.Environment;
6 6
 import com.facebook.react.bridge.Arguments;
7 7
 import com.facebook.react.bridge.Callback;
8 8
 import com.facebook.react.bridge.ReactApplicationContext;
9
+import com.facebook.react.bridge.ReadableArray;
10
+import com.facebook.react.bridge.WritableArray;
9 11
 import com.facebook.react.bridge.WritableMap;
10 12
 import com.facebook.react.modules.core.DeviceEventManagerModule;
11 13
 import com.loopj.android.http.Base64;
@@ -34,7 +36,7 @@ public class RNFetchBlobFS {
34 36
     DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
35 37
     String encoding = "base64";
36 38
     boolean append = false;
37
-    FileOutputStream writeStreamInstance = null;
39
+    OutputStream writeStreamInstance = null;
38 40
     static HashMap<String, RNFetchBlobFS> fileStreams = new HashMap<>();
39 41
 
40 42
     RNFetchBlobFS(ReactApplicationContext ctx) {
@@ -50,13 +52,13 @@ public class RNFetchBlobFS {
50 52
     static public void getSystemfolders(ReactApplicationContext ctx, Callback callback) {
51 53
         callback.invoke(
52 54
                 // document folder
53
-                String.valueOf(ctx.getFilesDir()),
55
+                String.valueOf(ctx.getFilesDir().getAbsolutePath()),
54 56
                 // cache folder
55
-                String.valueOf(ctx.getCacheDir()),
57
+                String.valueOf(ctx.getCacheDir().getAbsolutePath()),
56 58
                 // SD card folder
57
-                String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)),
59
+                String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()),
58 60
                 // Download folder
59
-                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
61
+                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
60 62
         );
61 63
     }
62 64
 
@@ -106,7 +108,15 @@ public class RNFetchBlobFS {
106 108
                         }
107 109
                     } else if (encoding.equalsIgnoreCase("base64")) {
108 110
                         while ((cursor = fs.read(buffer)) != -1) {
109
-                            emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
111
+                            if(cursor < chunkSize) {
112
+                                byte [] copy = new byte[cursor];
113
+                                for(int i =0;i<cursor;i++) {
114
+                                    copy[i] = buffer[i];
115
+                                }
116
+                                emitStreamEvent(eventName, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
117
+                            }
118
+                            else
119
+                                emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
110 120
                         }
111 121
                     } else {
112 122
                         String msg = "unrecognized encoding `" + encoding + "`";
@@ -147,6 +157,7 @@ public class RNFetchBlobFS {
147 157
             this.append = append;
148 158
             String streamId = UUID.randomUUID().toString();
149 159
             RNFetchBlobFS.fileStreams.put(streamId, this);
160
+            this.writeStreamInstance = fs;
150 161
             callback.invoke(null, streamId);
151 162
         } catch(Exception err) {
152 163
             callback.invoke("failed to create write stream at path `"+path+"` "+ err.getLocalizedMessage());
@@ -163,13 +174,13 @@ public class RNFetchBlobFS {
163 174
     static void writeChunk(String streamId, String data, Callback callback) {
164 175
 
165 176
         RNFetchBlobFS fs = fileStreams.get(streamId);
166
-        FileOutputStream stream = fs.writeStreamInstance;
177
+        OutputStream stream = fs.writeStreamInstance;
167 178
         byte [] chunk = RNFetchBlobFS.stringToBytes(data, fs.encoding);
168 179
 
169 180
         try {
170 181
             stream.write(chunk);
171
-            callback.invoke(null);
172
-        } catch (IOException e) {
182
+            callback.invoke();
183
+        } catch (Exception e) {
173 184
             callback.invoke(e.getLocalizedMessage());
174 185
         }
175 186
     }
@@ -182,10 +193,10 @@ public class RNFetchBlobFS {
182 193
     static void closeStream(String streamId, Callback callback) {
183 194
         try {
184 195
             RNFetchBlobFS fs = fileStreams.get(streamId);
185
-            FileOutputStream stream = fs.writeStreamInstance;
186
-            stream.close();
187
-            stream = null;
196
+            OutputStream stream = fs.writeStreamInstance;
188 197
             fileStreams.remove(streamId);
198
+            stream.close();
199
+            callback.invoke();
189 200
         } catch(Exception err) {
190 201
             callback.invoke(err.getLocalizedMessage());
191 202
         }
@@ -199,13 +210,26 @@ public class RNFetchBlobFS {
199 210
     static void unlink(String path, Callback callback) {
200 211
         try {
201 212
             boolean success = new File(path).delete();
202
-            callback.invoke(success);
213
+            callback.invoke( null, success);
203 214
         } catch(Exception err) {
204 215
             if(err != null)
205 216
             callback.invoke(err.getLocalizedMessage());
206 217
         }
207 218
     }
208
-
219
+    /**
220
+     * Make a folder
221
+     * @param path Source path
222
+     * @param callback  JS context callback
223
+     */
224
+    static void mkdir(String path, Callback callback) {
225
+        File dest = new File(path);
226
+        if(dest.exists()) {
227
+            callback.invoke("failed to create folder at `" + path + "` folder already exists");
228
+            return;
229
+        }
230
+        dest.mkdirs();
231
+        callback.invoke();
232
+    }
209 233
     /**
210 234
      * Copy file to destination path
211 235
      * @param path Source path
@@ -223,8 +247,7 @@ public class RNFetchBlobFS {
223 247
                 callback.invoke("source file at path`" + path + "` not exists");
224 248
                 return;
225 249
             }
226
-            if(!new File(destFolder).exists())
227
-                new File(destFolder).mkdir();
250
+
228 251
             if(!new File(dest).exists())
229 252
                 new File(dest).createNewFile();
230 253
 
@@ -236,20 +259,15 @@ public class RNFetchBlobFS {
236 259
             while ((len = in.read(buf)) > 0) {
237 260
                 out.write(buf, 0, len);
238 261
             }
239
-            in.close();
240
-            out.close();
241
-            callback.invoke(null);
262
+
242 263
         } catch (Exception err) {
243 264
             if(err != null)
244 265
                 callback.invoke(err.getLocalizedMessage());
245 266
         } finally {
246 267
             try {
247 268
                 in.close();
248
-            } catch (IOException e) {
249
-                callback.invoke(e.getLocalizedMessage());
250
-            }
251
-            try {
252 269
                 out.close();
270
+                callback.invoke();
253 271
             } catch (IOException e) {
254 272
                 callback.invoke(e.getLocalizedMessage());
255 273
             }
@@ -269,7 +287,7 @@ public class RNFetchBlobFS {
269 287
             return;
270 288
         }
271 289
         src.renameTo(new File(dest));
272
-        callback.invoke(null);
290
+        callback.invoke();
273 291
     }
274 292
 
275 293
     /**
@@ -279,9 +297,7 @@ public class RNFetchBlobFS {
279 297
      */
280 298
     static void exists(String path, Callback callback) {
281 299
         boolean exist = new File(path).exists();
282
-        boolean isDir = false;
283
-        if(exist)
284
-         isDir = new File(path).isDirectory();
300
+        boolean isDir = new File(path).isDirectory();;
285 301
         callback.invoke(exist, isDir);
286 302
     }
287 303
 
@@ -292,10 +308,16 @@ public class RNFetchBlobFS {
292 308
      */
293 309
     static void ls(String path, Callback callback) {
294 310
         File src = new File(path);
295
-        if(!src.exists() || !src.isDirectory())
296
-            callback.invoke("failed to list path `"+path+"` for it is not exist or it is not a folder");
311
+        if(!src.exists() || !src.isDirectory()) {
312
+            callback.invoke("failed to list path `" + path + "` for it is not exist or it is not a folder");
313
+            return;
314
+        }
297 315
         String [] files = new File(path).list();
298
-        callback.invoke(null, files);
316
+        WritableArray arg = Arguments.createArray();
317
+        for(String i : files) {
318
+            arg.pushString(i);
319
+        }
320
+        callback.invoke(null, arg);
299 321
     }
300 322
 
301 323
     /**
@@ -321,6 +343,22 @@ public class RNFetchBlobFS {
321 343
         }
322 344
     }
323 345
 
346
+    static void removeSession(ReadableArray paths, Callback callback) {
347
+
348
+        AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
349
+            @Override
350
+            protected Integer doInBackground(ReadableArray ...paths) {
351
+                for(int i =0; i< paths[0].size(); i++) {
352
+                    File f = new File(paths[0].getString(i));
353
+                    if(f.exists())
354
+                        f.delete();
355
+                }
356
+                return paths[0].size();
357
+            }
358
+        };
359
+        task.execute(paths);
360
+    }
361
+
324 362
     /**
325 363
      * String to byte converter method
326 364
      * @param data  Raw data in string format