Kaynağa Gözat

Apply possible fix to fs.readFile and fs.readStream for #287

Ben Hsieh 8 yıl önce
ebeveyn
işleme
8121dbf66d

+ 20
- 4
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Dosyayı Görüntüle

138
      * @param promise
138
      * @param promise
139
      */
139
      */
140
     static public void readFile(String path, String encoding, final Promise promise ) {
140
     static public void readFile(String path, String encoding, final Promise promise ) {
141
-        path = normalizePath(path);
141
+        String resolved = normalizePath(path);
142
+        if(resolved != null)
143
+            path = resolved;
142
         try {
144
         try {
143
             byte[] bytes;
145
             byte[] bytes;
144
 
146
 
145
-            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
147
+            if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
146
                 String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
148
                 String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
147
                 long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
149
                 long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
148
                 bytes = new byte[(int) length];
150
                 bytes = new byte[(int) length];
150
                 in.read(bytes, 0, (int) length);
152
                 in.read(bytes, 0, (int) length);
151
                 in.close();
153
                 in.close();
152
             }
154
             }
155
+            // issue 287
156
+            else if(resolved == null) {
157
+                InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
158
+                int length = (int) in.available();
159
+                bytes = new byte[length];
160
+                in.read(bytes);
161
+                in.close();
162
+            }
153
             else {
163
             else {
154
                 File f = new File(path);
164
                 File f = new File(path);
155
                 int length = (int) f.length();
165
                 int length = (int) f.length();
225
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
235
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
226
      */
236
      */
227
     public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
237
     public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
228
-        path = normalizePath(path);
238
+        String resolved = normalizePath(path);
239
+        if(resolved != null)
240
+            path = resolved;
229
         try {
241
         try {
230
 
242
 
231
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
243
             int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
233
                 chunkSize = bufferSize;
245
                 chunkSize = bufferSize;
234
 
246
 
235
             InputStream fs;
247
             InputStream fs;
236
-            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
248
+            if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
237
                 fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
249
                 fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238
             }
250
             }
251
+            // fix issue 287
252
+            else if(resolved == null) {
253
+                fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
254
+            }
239
             else {
255
             else {
240
                 fs = new FileInputStream(new File(path));
256
                 fs = new FileInputStream(new File(path));
241
             }
257
             }