1616import android .support .v4 .util .LongSparseArray ;
1717import android .util .Log ;
1818import com .facebook .react .bridge .*;
19+ import org .hstar .reactnative .esayupgrade .IORejectionException ;
1920
20- import java .io .File ;
21- import java .io .FileNotFoundException ;
21+ import java .io .*;
2222import java .util .ArrayList ;
2323import java .util .Arrays ;
2424import java .util .HashMap ;
@@ -63,7 +63,6 @@ public String getName() {
6363 return "RNEasyUpgrade" ;
6464 }
6565
66-
6766 BroadcastReceiver downloadReceiver = new BroadcastReceiver () {
6867 @ Override
6968 public void onReceive (Context context , Intent intent ) {
@@ -87,6 +86,19 @@ public void onReceive(Context context, Intent intent) {
8786 }
8887 };
8988
89+ private Uri getFileUri (String filepath ) throws IORejectionException {
90+ Uri uri = Uri .parse (filepath );
91+ if (uri .getScheme () == null ) {
92+ // No prefix, assuming that provided path is absolute path to file
93+ File file = new File (filepath );
94+ if (file .isDirectory ()) {
95+ throw new IORejectionException ("EISDIR" , "EISDIR: illegal operation on a directory, read '" + filepath + "'" );
96+ }
97+ uri = Uri .parse ("file://" + filepath );
98+ }
99+ return uri ;
100+ }
101+
90102
91103 @ ReactMethod
92104 public void download (String url , ReadableMap headers , ReadableMap config , Callback onDone ) {
@@ -163,6 +175,108 @@ public void isFileExists(String filepath, Promise promise) {
163175 }
164176 }
165177
178+ @ ReactMethod
179+ public void moveFile (String filepath , String destPath , Promise promise ) {
180+ try {
181+ File externalStorageDirectory = Environment .getExternalStorageDirectory ();
182+ if (externalStorageDirectory != null && !filepath .contains (externalStorageDirectory .getAbsolutePath ())) {
183+ filepath = externalStorageDirectory .getAbsolutePath () + filepath ;
184+ destPath = externalStorageDirectory .getAbsolutePath () + destPath ;
185+ }
186+
187+ File inFile = new File (filepath );
188+
189+ if (!inFile .renameTo (new File (destPath ))) {
190+ copyFile (filepath , destPath );
191+ inFile .delete ();
192+ }
193+
194+ promise .resolve (true );
195+ } catch (Exception ex ) {
196+ ex .printStackTrace ();
197+ reject (promise , filepath , ex );
198+ }
199+ }
200+
201+ @ ReactMethod
202+ public void copyFile (String filepath , String destPath , Promise promise ) {
203+ try {
204+ copyFile (filepath , destPath );
205+
206+ promise .resolve (null );
207+ } catch (Exception ex ) {
208+ ex .printStackTrace ();
209+ reject (promise , filepath , ex );
210+ }
211+ }
212+
213+ @ ReactMethod
214+ public void unlink (String filepath , Promise promise ) {
215+ try {
216+ File file = new File (filepath );
217+
218+ if (!file .exists ()) throw new Exception ("File does not exist" );
219+
220+ DeleteRecursive (file );
221+
222+ promise .resolve (null );
223+ } catch (Exception ex ) {
224+ ex .printStackTrace ();
225+ reject (promise , filepath , ex );
226+ }
227+ }
228+
229+ private void DeleteRecursive (File fileOrDirectory ) {
230+ if (fileOrDirectory .isDirectory ()) {
231+ for (File child : fileOrDirectory .listFiles ()) {
232+ DeleteRecursive (child );
233+ }
234+ }
235+
236+ fileOrDirectory .delete ();
237+ }
238+
239+ private void copyFile (String filepath , String destPath ) throws IOException , IORejectionException {
240+ InputStream in = getInputStream (filepath );
241+ OutputStream out = getOutputStream (destPath , false );
242+
243+ byte [] buffer = new byte [1024 ];
244+ int length ;
245+ while ((length = in .read (buffer )) > 0 ) {
246+ out .write (buffer , 0 , length );
247+ }
248+ in .close ();
249+ out .close ();
250+ }
251+
252+ private InputStream getInputStream (String filepath ) throws IORejectionException {
253+ Uri uri = getFileUri (filepath );
254+ InputStream stream ;
255+ try {
256+ stream = reactContext .getContentResolver ().openInputStream (uri );
257+ } catch (FileNotFoundException ex ) {
258+ throw new IORejectionException ("ENOENT" , "ENOENT: no such file or directory, open '" + filepath + "'" );
259+ }
260+ if (stream == null ) {
261+ throw new IORejectionException ("ENOENT" , "ENOENT: could not open an input stream for '" + filepath + "'" );
262+ }
263+ return stream ;
264+ }
265+
266+ private OutputStream getOutputStream (String filepath , boolean append ) throws IORejectionException {
267+ Uri uri = getFileUri (filepath );
268+ OutputStream stream ;
269+ try {
270+ stream = reactContext .getContentResolver ().openOutputStream (uri , append ? "wa" : "w" );
271+ } catch (FileNotFoundException ex ) {
272+ throw new IORejectionException ("ENOENT" , "ENOENT: no such file or directory, open '" + filepath + "'" );
273+ }
274+ if (stream == null ) {
275+ throw new IORejectionException ("ENOENT" , "ENOENT: could not open an output stream for '" + filepath + "'" );
276+ }
277+ return stream ;
278+ }
279+
166280 private void rejectFileNotFound (Promise promise , String filepath ) {
167281 promise .reject ("ENOENT" , "ENOENT: no such file or directory, open '" + filepath + "'" );
168282 }
0 commit comments