Kaynağa Gözat

Merge pull request #40 from jbrodriguez/issue-35-NetworkOptimization

Issue 35 network optimization
wkh237 8 yıl önce
ebeveyn
işleme
8fb4883f31

+ 1
- 1
README.md Dosyayı Görüntüle

@@ -1,4 +1,4 @@
1
-# react-native-fetch-blob [![npm](https://img.shields.io/npm/v/react-native-fetch-blob.svg?style=flat-square)]() ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg?style=flat-square) [![npm](https://img.shields.io/npm/l/express.svg?maxAge=2592000&style=flat-square)]()
1
+# react-native-fetch-blob [![npm](https://img.shields.io/npm/v/react-native-fetch-blob.svg?style=flat-square)](https://www.npmjs.com/package/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg?style=flat-square) [![npm](https://img.shields.io/npm/l/express.svg?maxAge=2592000&style=flat-square)]()
2 2
 
3 3
 A module provides upload, download, and files access API. Supports file stream read/write for process large files.
4 4
 

+ 2
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

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

+ 7
- 1
src/index.js Dosyayı Görüntüle

@@ -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)