Browse Source

#2 Change android download-to-file API consistency

Ben Hsieh 8 years ago
parent
commit
1cc5d4b0a4

+ 22
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java View File

@@ -0,0 +1,22 @@
1
+package com.RNFetchBlob;
2
+
3
+import com.facebook.react.bridge.ReadableMap;
4
+
5
+/**
6
+ * Created by wkh237 on 2016/5/29.
7
+ */
8
+public class RNFetchBlobConfig {
9
+
10
+    public Boolean fileCache;
11
+    public String path;
12
+    public String appendExt;
13
+
14
+    RNFetchBlobConfig(ReadableMap options) {
15
+
16
+        this.fileCache = options.getBoolean("fileCache");
17
+        this.path = options.getString("path");
18
+        this.appendExt = options.getString("appendExt");
19
+
20
+    }
21
+
22
+}

+ 42
- 33
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -1,5 +1,6 @@
1 1
 package com.RNFetchBlob;
2 2
 
3
+import android.os.AsyncTask;
3 4
 import android.os.Environment;
4 5
 
5 6
 import com.facebook.react.bridge.Arguments;
@@ -31,41 +32,49 @@ public class RNFetchBlobFS {
31 32
     }
32 33
     
33 34
     // TODO : make it an AsyncTask
34
-    public void readStream(String taskId, String encoding) {
35
-        try {
36
-
37
-            FileInputStream fs = mCtx.openFileInput(mCtx.getFilesDir() + "/fetchblobtmp_"+ taskId);
38
-            byte[] buffer = new byte[1024];
39
-            int cursor = 0;
40
-            boolean error = false;
41
-
42
-            if (encoding.toLowerCase() == "utf8") {
43
-                while ((cursor = fs.read(buffer)) != -1) {
44
-                    String chunk = new String(buffer, 0, cursor, "UTF-8");
45
-                    emitFSData(taskId, "data", chunk);
46
-                }
47
-            } else if (encoding.toLowerCase() == "ascii") {
48
-                while ((cursor = fs.read(buffer)) != -1) {
49
-                    String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
50
-                    emitFSData(taskId, "data", chunk);
51
-                }
52
-            } else if (encoding.toLowerCase() == "base64") {
53
-                while ((cursor = fs.read(buffer)) != -1) {
54
-                    emitFSData(taskId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
35
+    public void readStream(final String path, String encoding) {
36
+        AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
37
+            @Override
38
+            protected Integer doInBackground(String ... args) {
39
+                String path = args[0];
40
+                String encoding = args[1];
41
+                String eventName = "RNFetchBlobStream+" + path;
42
+                try {
43
+                    FileInputStream fs = mCtx.openFileInput(mCtx.getFilesDir() + "/"+ path);
44
+                    byte[] buffer = new byte[1024];
45
+                    int cursor = 0;
46
+                    boolean error = false;
47
+
48
+                    if (encoding.toLowerCase() == "utf8") {
49
+                        while ((cursor = fs.read(buffer)) != -1) {
50
+                            String chunk = new String(buffer, 0, cursor, "UTF-8");
51
+                            emitFSData(eventName, "data", chunk);
52
+                        }
53
+                    } else if (encoding.toLowerCase() == "ascii") {
54
+                        while ((cursor = fs.read(buffer)) != -1) {
55
+                            String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
56
+                            emitFSData(eventName, "data", chunk);
57
+                        }
58
+                    } else if (encoding.toLowerCase() == "base64") {
59
+                        while ((cursor = fs.read(buffer)) != -1) {
60
+                            emitFSData(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
61
+                        }
62
+                    } else {
63
+                        String msg = "unrecognized encoding `" + encoding + "`";
64
+                        emitFSData(eventName, "error", msg);
65
+                        error = true;
66
+                    }
67
+
68
+                    if(!error)
69
+                        emitFSData(eventName, "end", "");
70
+
71
+                } catch (Exception err) {
72
+                    emitFSData(eventName, "error", err.getLocalizedMessage());
55 73
                 }
56
-            } else {
57
-                String msg = "unrecognized encoding `" + encoding + "`";
58
-                emitFSData(taskId, "error", msg);
59
-                error = true;
74
+                return null;
60 75
             }
61
-
62
-            if(!error)
63
-                emitFSData(taskId, "end", "");
64
-
65
-        } catch (Exception err) {
66
-            emitFSData(taskId, "error", err.getLocalizedMessage());
67
-        }
68
-
76
+        };
77
+        task.execute(path, encoding);
69 78
     }
70 79
 
71 80
     void emitFSData(String taskId, String event, String data) {

+ 13
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFileHandler.java View File

@@ -20,15 +20,25 @@ public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
20 20
     Callback onResponse;
21 21
     ReactContext mCtx;
22 22
     String mTaskId;
23
+    RNFetchBlobConfig mConfig;
23 24
 
24
-    RNFetchBlobFileHandler(ReactApplicationContext ctx, String taskId, Callback onResponse) {
25
-        // save temp file to application storage
26
-        super(new File(RNFetchBlobFS.getTmpPath(ctx, taskId)), false, false);
25
+    RNFetchBlobFileHandler(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config, Callback onResponse) {
26
+        super(new File( RNFetchBlobFileHandler.getFilePath(ctx, taskId, config)), false, false);
27 27
         this.onResponse = onResponse;
28 28
         this.mTaskId = taskId;
29
+        this.mConfig = config;
29 30
         this.mCtx = ctx;
30 31
     }
31 32
 
33
+    static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
34
+        if(config.path != null)
35
+            return config.path;
36
+        else if(config.fileCache && config.appendExt != null)
37
+            return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
38
+        else
39
+            return RNFetchBlobFS.getTmpPath(ctx, taskId);
40
+    }
41
+
32 42
     @Override
33 43
     public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
34 44
         this.onResponse.invoke(statusCode, throwable.getMessage()+ ", "+ throwable.getCause());