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
     }
85
     }
86
 
86
 
87
     @ReactMethod
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
         threadPool.execute(new Runnable() {
89
         threadPool.execute(new Runnable() {
90
             @Override
90
             @Override
91
             public void run() {
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
     }
128
     }
129
 
129
 
130
     @ReactMethod
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
         threadPool.execute(new Runnable() {
132
         threadPool.execute(new Runnable() {
133
             @Override
133
             @Override
134
             public void run() {
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
      * @param encoding Encoding of initial data.
727
      * @param encoding Encoding of initial data.
728
      * @param callback RCT bridge callback.
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
         try {
731
         try {
732
             File dest = new File(path);
732
             File dest = new File(path);
733
             boolean created = dest.createNewFile();
733
             boolean created = dest.createNewFile();
735
                 String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
735
                 String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
736
                 File src = new File(orgPath);
736
                 File src = new File(orgPath);
737
                 if(!src.exists()) {
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
                     return ;
739
                     return ;
740
                 }
740
                 }
741
                 FileInputStream fin = new FileInputStream(src);
741
                 FileInputStream fin = new FileInputStream(src);
751
             }
751
             }
752
             else {
752
             else {
753
                 if (!created) {
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
                     return;
755
                     return;
756
                 }
756
                 }
757
                 OutputStream ostream = new FileOutputStream(dest);
757
                 OutputStream ostream = new FileOutputStream(dest);
758
                 ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
758
                 ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
759
             }
759
             }
760
-            callback.invoke(null, path);
760
+            promise.resolve(path);
761
         } catch(Exception err) {
761
         } catch(Exception err) {
762
-            callback.invoke(err.getLocalizedMessage());
762
+            promise.reject(err.getLocalizedMessage());
763
         }
763
         }
764
     }
764
     }
765
 
765
 
769
      * @param data  Content of new file
769
      * @param data  Content of new file
770
      * @param callback  JS context callback
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
         try {
773
         try {
774
             File dest = new File(path);
774
             File dest = new File(path);
775
             if(dest.exists()) {
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
                 return;
777
                 return;
778
             }
778
             }
779
             boolean created = dest.createNewFile();
779
             boolean created = dest.createNewFile();
780
             if(!created) {
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
                 return;
782
                 return;
783
             }
783
             }
784
             OutputStream ostream = new FileOutputStream(dest);
784
             OutputStream ostream = new FileOutputStream(dest);
788
             }
788
             }
789
             ostream.write(chunk);
789
             ostream.write(chunk);
790
             chunk = null;
790
             chunk = null;
791
-            callback.invoke(null, path);
791
+            promise.resolve(path);
792
         } catch(Exception err) {
792
         } catch(Exception err) {
793
-            callback.invoke(err.getLocalizedMessage());
793
+            promise.reject(err.getLocalizedMessage());
794
         }
794
         }
795
     }
795
     }
796
 
796