Browse Source

createFile methods with promises

Artur Chrusciel 6 years ago
parent
commit
fb63db99e0

+ 4
- 4
android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -85,11 +85,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
85 85
     }
86 86
 
87 87
     @ReactMethod
88
-    public void createFile(final String path, final String content, final String encode, final Callback callback) {
88
+    public void createFile(final String path, final String content, final String encode, final Promise promise) {
89 89
         threadPool.execute(new Runnable() {
90 90
             @Override
91 91
             public void run() {
92
-                RNFetchBlobFS.createFile(path, content, encode, callback);
92
+                RNFetchBlobFS.createFile(path, content, encode, promise);
93 93
             }
94 94
         });
95 95
     }
@@ -128,11 +128,11 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
128 128
     }
129 129
 
130 130
     @ReactMethod
131
-    public void createFileASCII(final String path, final ReadableArray dataArray, final Callback callback) {
131
+    public void createFileASCII(final String path, final ReadableArray dataArray, final Promise promise) {
132 132
         threadPool.execute(new Runnable() {
133 133
             @Override
134 134
             public void run() {
135
-                RNFetchBlobFS.createFileASCII(path, dataArray, callback);
135
+                RNFetchBlobFS.createFileASCII(path, dataArray, promise);
136 136
             }
137 137
         });
138 138
     }

+ 10
- 10
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -727,7 +727,7 @@ public class RNFetchBlobFS {
727 727
      * @param encoding Encoding of initial data.
728 728
      * @param callback RCT bridge callback.
729 729
      */
730
-    static void createFile(String path, String data, String encoding, Callback callback) {
730
+    static void createFile(String path, String data, String encoding, Promise promise) {
731 731
         try {
732 732
             File dest = new File(path);
733 733
             boolean created = dest.createNewFile();
@@ -735,7 +735,7 @@ public class RNFetchBlobFS {
735 735
                 String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
736 736
                 File src = new File(orgPath);
737 737
                 if(!src.exists()) {
738
-                    callback.invoke("RNfetchBlob writeFileError", "source file : " + data + "not exists");
738
+                    promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
739 739
                     return ;
740 740
                 }
741 741
                 FileInputStream fin = new FileInputStream(src);
@@ -751,15 +751,15 @@ public class RNFetchBlobFS {
751 751
             }
752 752
             else {
753 753
                 if (!created) {
754
-                    callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists, or the file already exists. If you intended to overwrite the existing file use fs.writeFile instead.");
754
+                    Promise.reject("create file error: failed to create file at path `" + path + "` for its parent path may not exists, or the file already exists. If you intended to overwrite the existing file use fs.writeFile instead.");
755 755
                     return;
756 756
                 }
757 757
                 OutputStream ostream = new FileOutputStream(dest);
758 758
                 ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
759 759
             }
760
-            callback.invoke(null, path);
760
+            promise.resolve(path);
761 761
         } catch(Exception err) {
762
-            callback.invoke(err.getLocalizedMessage());
762
+            promise.reject(err.getLocalizedMessage());
763 763
         }
764 764
     }
765 765
 
@@ -769,16 +769,16 @@ public class RNFetchBlobFS {
769 769
      * @param data  Content of new file
770 770
      * @param callback  JS context callback
771 771
      */
772
-    static void createFileASCII(String path, ReadableArray data, Callback callback) {
772
+    static void createFileASCII(String path, ReadableArray data, Promise promise) {
773 773
         try {
774 774
             File dest = new File(path);
775 775
             if(dest.exists()) {
776
-                callback.invoke("create file error: failed to create file at path `" + path + "`, file already exists.");
776
+                promise.reject("create file error: failed to create file at path `" + path + "`, file already exists.");
777 777
                 return;
778 778
             }
779 779
             boolean created = dest.createNewFile();
780 780
             if(!created) {
781
-                callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
781
+                promise.reject("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
782 782
                 return;
783 783
             }
784 784
             OutputStream ostream = new FileOutputStream(dest);
@@ -788,9 +788,9 @@ public class RNFetchBlobFS {
788 788
             }
789 789
             ostream.write(chunk);
790 790
             chunk = null;
791
-            callback.invoke(null, path);
791
+            promise.resolve(path);
792 792
         } catch(Exception err) {
793
-            callback.invoke(err.getLocalizedMessage());
793
+            promise.reject(err.getLocalizedMessage());
794 794
         }
795 795
     }
796 796