Browse Source

Add exception handling to MD5 code

Juan B. Rodriguez 8 years ago
parent
commit
47143d9317
1 changed files with 24 additions and 13 deletions
  1. 24
    13
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 24
- 13
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

78
     }
78
     }
79
 
79
 
80
     public static String getMD5(String input) {
80
     public static String getMD5(String input) {
81
-        MessageDigest md = MessageDigest.getInstance("MD5");
82
-        md.update(input.getBytes());
83
-        byte[] digest = md.digest();
84
-        
85
-        StringBuffer sb = new StringBuffer();
86
-        
87
-        for (byte b : digest) {
88
-            sb.append(String.format("%02x", b & 0xff))
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();
89
         }
97
         }
90
 
98
 
91
-        return sb.toString();
99
+        return result;
92
     }
100
     }
93
 
101
 
94
     @Override
102
     @Override
122
 
130
 
123
         }
131
         }
124
 
132
 
125
-        String key = this.taskId;
133
+        String cacheKey = this.taskId;
126
         if (this.options.key != null) {
134
         if (this.options.key != null) {
127
-            key = RNFetchBlobReq.getMD5(this.options.key);
135
+            cacheKey = RNFetchBlobReq.getMD5(this.options.key);
136
+            if (cacheKey == null) {
137
+                cacheKey = this.taskId;
138
+            }
128
 
139
 
129
-            File file = new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, key, this.options))
140
+            File file = new File(RNFetchBlobFileHandler.getFilePath(ctx, taskId, cacheKey, this.options));
130
             if (file.exists()) {
141
             if (file.exists()) {
131
                callback.invoke(null, file.getAbsolutePath());
142
                callback.invoke(null, file.getAbsolutePath());
132
                return;
143
                return;
167
 
178
 
168
             // create handler
179
             // create handler
169
             if(options.fileCache || options.path != null) {
180
             if(options.fileCache || options.path != null) {
170
-                handler = new RNFetchBlobFileHandler(ctx, taskId, key, options, callback);
181
+                handler = new RNFetchBlobFileHandler(ctx, taskId, cacheKey, options, callback);
171
                 // if path format invalid, throw error
182
                 // if path format invalid, throw error
172
                 if (!((RNFetchBlobFileHandler)handler).isValid) {
183
                 if (!((RNFetchBlobFileHandler)handler).isValid) {
173
                     callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path  +"` is not a valid path.");
184
                     callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path  +"` is not a valid path.");