Browse Source

Merge branch '0.7.0' of github.com:wkh237/react-native-fetch-blob into 0.7.0

Ben Hsieh 8 years ago
parent
commit
81f112b7b9

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

@@ -14,6 +14,7 @@ public class RNFetchBlobConfig {
14 14
     public String appendExt;
15 15
     public ReadableMap addAndroidDownloads;
16 16
     public Boolean trusty;
17
+    public String key;
17 18
 
18 19
     RNFetchBlobConfig(ReadableMap options) {
19 20
         if(options == null)
@@ -25,6 +26,7 @@ public class RNFetchBlobConfig {
25 26
         if(options.hasKey("addAndroidDownloads")) {
26 27
             this.addAndroidDownloads = options.getMap("addAndroidDownloads");
27 28
         }
29
+        this.key = options.hasKey("key") ? options.getString("key") : null;
28 30
     }
29 31
 
30 32
 }

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

@@ -27,25 +27,25 @@ public class RNFetchBlobFileHandler extends FileAsyncHttpResponseHandler {
27 27
     String mTaskId;
28 28
     RNFetchBlobConfig mConfig;
29 29
 
30
-    RNFetchBlobFileHandler(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config, Callback onResponse) {
31
-        super(new File( RNFetchBlobFileHandler.getFilePath(ctx, taskId, config)), false, false);
30
+    RNFetchBlobFileHandler(ReactApplicationContext ctx, String taskId, String key, RNFetchBlobConfig config, Callback onResponse) {
31
+        super(new File( RNFetchBlobFileHandler.getFilePath(ctx, taskId, key, config)), false, false);
32 32
         this.onResponse = onResponse;
33 33
         this.mTaskId = taskId;
34 34
         this.mConfig = config;
35 35
         this.mCtx = ctx;
36
-        if(!new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, config)).isFile()) {
36
+        if(!new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, key, config)).isFile()) {
37 37
             this.isValid = false;
38 38
         }
39 39
         this.isValid = true;
40 40
     }
41 41
 
42
-    static String getFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
42
+    static String getFilePath(ReactApplicationContext ctx, String taskId, String key, RNFetchBlobConfig config) {
43 43
         if(config.path != null)
44 44
             return config.path;
45 45
         else if(config.fileCache && config.appendExt != null)
46
-            return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
46
+            return RNFetchBlobFS.getTmpPath(ctx, key) + "." + config.appendExt;
47 47
         else
48
-            return RNFetchBlobFS.getTmpPath(ctx, taskId);
48
+            return RNFetchBlobFS.getTmpPath(ctx, key);
49 49
     }
50 50
 
51 51
     @Override

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

@@ -22,6 +22,7 @@ import java.io.File;
22 22
 import java.io.IOException;
23 23
 import java.io.InputStream;
24 24
 import java.security.KeyStore;
25
+import java.security.MessageDigest;
25 26
 
26 27
 import cz.msebera.android.httpclient.HttpEntity;
27 28
 import cz.msebera.android.httpclient.entity.ByteArrayEntity;
@@ -78,6 +79,28 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
78 79
         }
79 80
     }
80 81
 
82
+    public static String getMD5(String input) {
83
+        String result = null;
84
+
85
+        try {
86
+            MessageDigest md = MessageDigest.getInstance("MD5");
87
+            md.update(input.getBytes());
88
+            byte[] digest = md.digest();
89
+            
90
+            StringBuffer sb = new StringBuffer();
91
+            
92
+            for (byte b : digest) {
93
+                sb.append(String.format("%02x", b & 0xff));
94
+            }
95
+
96
+            result = sb.toString();
97
+        } catch(Exception ex) {
98
+            ex.printStackTrace();
99
+        }
100
+
101
+        return result;
102
+    }
103
+
81 104
     @Override
82 105
     public void run() {
83 106
 
@@ -109,6 +132,20 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
109 132
 
110 133
         }
111 134
 
135
+        String cacheKey = this.taskId;
136
+        if (this.options.key != null) {
137
+            cacheKey = RNFetchBlobReq.getMD5(this.options.key);
138
+            if (cacheKey == null) {
139
+                cacheKey = this.taskId;
140
+            }
141
+
142
+            File file = new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, cacheKey, this.options));
143
+            if (file.exists()) {
144
+               callback.invoke(null, file.getAbsolutePath());
145
+               return;
146
+            }
147
+        }
148
+
112 149
         try {
113 150
 
114 151
             req = new AsyncHttpClient();
@@ -145,7 +182,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
145 182
 
146 183
             // create handler
147 184
             if(options.fileCache || options.path != null) {
148
-                handler = new RNFetchBlobFileHandler(ctx, taskId, options, callback);
185
+                handler = new RNFetchBlobFileHandler(ctx, taskId, cacheKey, options, callback);
149 186
                 // if path format invalid, throw error
150 187
                 if (!((RNFetchBlobFileHandler)handler).isValid) {
151 188
                     callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path  +"` is not a valid path.");

+ 7
- 1
src/index.js View File

@@ -79,6 +79,12 @@ function wrap(path:string):string {
79 79
  *                   If this property has a valid string format, resonse data
80 80
  *                   will be saved to specific file path. Default string format
81 81
  *                   is : `RNFetchBlob-file://path-to-file`
82
+ *         @property {string} key
83
+ *                   If this property is set, it will be converted to md5, to
84
+ *                   check if a file with this name exists.
85
+ *                   If it exists, the absolute path is returned (no network 
86
+ *                   activity takes place )
87
+ *                   If it doesn't exist, the file is downloaded as usual
82 88
  *
83 89
  * @return {function} This method returns a `fetch` method instance.
84 90
  */
@@ -125,7 +131,7 @@ function fetch(...args:any):Promise {
125 131
       else {
126 132
         let respType = 'base64'
127 133
         // response data is saved to storage
128
-        if(options.path || options.fileCache || options.addAndroidDownloads) {
134
+        if(options.path || options.fileCache || options.addAndroidDownloads || options.key) {
129 135
           respType = 'path'
130 136
           if(options.session)
131 137
             session(options.session).add(data)