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
     public String appendExt;
14
     public String appendExt;
15
     public ReadableMap addAndroidDownloads;
15
     public ReadableMap addAndroidDownloads;
16
     public Boolean trusty;
16
     public Boolean trusty;
17
+    public String key;
17
 
18
 
18
     RNFetchBlobConfig(ReadableMap options) {
19
     RNFetchBlobConfig(ReadableMap options) {
19
         if(options == null)
20
         if(options == null)
25
         if(options.hasKey("addAndroidDownloads")) {
26
         if(options.hasKey("addAndroidDownloads")) {
26
             this.addAndroidDownloads = options.getMap("addAndroidDownloads");
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
     String mTaskId;
27
     String mTaskId;
28
     RNFetchBlobConfig mConfig;
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
         this.onResponse = onResponse;
32
         this.onResponse = onResponse;
33
         this.mTaskId = taskId;
33
         this.mTaskId = taskId;
34
         this.mConfig = config;
34
         this.mConfig = config;
35
         this.mCtx = ctx;
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
             this.isValid = false;
37
             this.isValid = false;
38
         }
38
         }
39
         this.isValid = true;
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
         if(config.path != null)
43
         if(config.path != null)
44
             return config.path;
44
             return config.path;
45
         else if(config.fileCache && config.appendExt != null)
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
         else
47
         else
48
-            return RNFetchBlobFS.getTmpPath(ctx, taskId);
48
+            return RNFetchBlobFS.getTmpPath(ctx, key);
49
     }
49
     }
50
 
50
 
51
     @Override
51
     @Override

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

22
 import java.io.IOException;
22
 import java.io.IOException;
23
 import java.io.InputStream;
23
 import java.io.InputStream;
24
 import java.security.KeyStore;
24
 import java.security.KeyStore;
25
+import java.security.MessageDigest;
25
 
26
 
26
 import cz.msebera.android.httpclient.HttpEntity;
27
 import cz.msebera.android.httpclient.HttpEntity;
27
 import cz.msebera.android.httpclient.entity.ByteArrayEntity;
28
 import cz.msebera.android.httpclient.entity.ByteArrayEntity;
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
     @Override
104
     @Override
82
     public void run() {
105
     public void run() {
83
 
106
 
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
         try {
149
         try {
113
 
150
 
114
             req = new AsyncHttpClient();
151
             req = new AsyncHttpClient();
145
 
182
 
146
             // create handler
183
             // create handler
147
             if(options.fileCache || options.path != null) {
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
                 // if path format invalid, throw error
186
                 // if path format invalid, throw error
150
                 if (!((RNFetchBlobFileHandler)handler).isValid) {
187
                 if (!((RNFetchBlobFileHandler)handler).isValid) {
151
                     callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path  +"` is not a valid path.");
188
                     callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path  +"` is not a valid path.");

+ 7
- 1
src/index.js View File

79
  *                   If this property has a valid string format, resonse data
79
  *                   If this property has a valid string format, resonse data
80
  *                   will be saved to specific file path. Default string format
80
  *                   will be saved to specific file path. Default string format
81
  *                   is : `RNFetchBlob-file://path-to-file`
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
  * @return {function} This method returns a `fetch` method instance.
89
  * @return {function} This method returns a `fetch` method instance.
84
  */
90
  */
125
       else {
131
       else {
126
         let respType = 'base64'
132
         let respType = 'base64'
127
         // response data is saved to storage
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
           respType = 'path'
135
           respType = 'path'
130
           if(options.session)
136
           if(options.session)
131
             session(options.session).add(data)
137
             session(options.session).add(data)