소스 검색

Change Download Manager complete handler #75

Ben Hsieh 8 년 전
부모
커밋
130d9f3db3
2개의 변경된 파일21개의 추가작업 그리고 17개의 파일을 삭제
  1. 21
    16
      src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
  2. 0
    1
      src/ios/RNFetchBlobFS.h

+ 21
- 16
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java 파일 보기

@@ -558,6 +558,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
558 558
                 DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
559 559
                 dm.query(query);
560 560
                 Cursor c = dm.query(query);
561
+                String filePath = null;
562
+                // the file exists in media content database
561 563
                 if (c.moveToFirst()) {
562 564
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
563 565
                     Uri uri = Uri.parse(contentUri);
@@ -565,26 +567,29 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
565 567
                     // use default destination of DownloadManager
566 568
                     if (cursor != null) {
567 569
                         cursor.moveToFirst();
568
-                        String filePath = cursor.getString(0);
570
+                        filePath = cursor.getString(0);
569 571
                         cursor.close();
570
-                        this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
571
-                    }
572
-                    // custom destination
573
-                    else {
574
-                        if(options.addAndroidDownloads.hasKey("path")) {
575
-                            try {
576
-                                String customDest = options.addAndroidDownloads.getString("path");
577
-                                boolean exists = new File(customDest).exists();
578
-                                if(!exists)
579
-                                    throw new Exception("Download manager download failed, the file does not downloaded to destination.");
580
-                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
581
-
582
-                            } catch(Exception ex) {
583
-                                this.callback.invoke(ex.getLocalizedMessage(), null, null);
584
-                            }
572
+                        if(filePath != null) {
573
+                            this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
574
+                            return;
585 575
                         }
586 576
                     }
587 577
                 }
578
+                // When the file is not found in media content database, check if custom path exists
579
+                if (options.addAndroidDownloads.hasKey("path")) {
580
+                    try {
581
+                        String customDest = options.addAndroidDownloads.getString("path");
582
+                        boolean exists = new File(customDest).exists();
583
+                        if(!exists)
584
+                            throw new Exception("Download manager download failed, the file does not downloaded to destination.");
585
+                        callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
586
+
587
+                    } catch(Exception ex) {
588
+                        this.callback.invoke(ex.getLocalizedMessage(), null, null);
589
+                    }
590
+                }
591
+                else
592
+                    this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
588 593
             }
589 594
         }
590 595
     }

+ 0
- 1
src/ios/RNFetchBlobFS.h 파일 보기

@@ -65,7 +65,6 @@
65 65
 
66 66
 // file stream
67 67
 - (void) openWithDestination;
68
-- (void) openWithId;
69 68
 - (NSString *)openWithPath:(NSString *)destPath encode:(nullable NSString *)encode appendData:(BOOL)append;
70 69
 - (void) startAssetReadStream:(NSData *)assetUrl;
71 70