Merge pull request #47 from corbt/move-file
Add moveFile with Android support
This commit is contained in:
commit
9a4c2d890f
|
@ -18,6 +18,7 @@ var _readDir = Promise.promisify(RNFSManager.readDir);
|
|||
var _stat = Promise.promisify(RNFSManager.stat);
|
||||
var _readFile = Promise.promisify(RNFSManager.readFile);
|
||||
var _writeFile = Promise.promisify(RNFSManager.writeFile);
|
||||
var _moveFile = Promise.promisify(RNFSManager.moveFile);
|
||||
var _unlink = Promise.promisify(RNFSManager.unlink);
|
||||
var _mkdir = Promise.promisify(RNFSManager.mkdir);
|
||||
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
|
||||
|
@ -123,6 +124,11 @@ var RNFS = {
|
|||
.catch(convertError);
|
||||
},
|
||||
|
||||
moveFile(filepath, destPath) {
|
||||
return _moveFile(filepath, destPath)
|
||||
.catch(convertError);
|
||||
},
|
||||
|
||||
pathForBundle(bundleName) {
|
||||
return _pathForBundle(bundleName);
|
||||
},
|
||||
|
|
|
@ -192,6 +192,10 @@ Write the `contents` to `filepath`. `encoding` can be one of `utf8` (default), `
|
|||
|
||||
The promise resolves with a boolean.
|
||||
|
||||
### `promise moveFile(filepath, destPath)`
|
||||
|
||||
Moves the file located at `filepath` to `destPath`. This is more performant than reading and then re-writing the file data because the move is done natively and the data doesn't have to be copied or cross the bridge.
|
||||
|
||||
### `promise unlink(filepath)`
|
||||
|
||||
Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.
|
||||
|
|
|
@ -148,6 +148,22 @@ RCT_EXPORT_METHOD(readFile:(NSString *)filepath
|
|||
callback(@[[NSNull null], base64Content]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
|
||||
destPath:(NSString *)destPath
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
NSFileManager *manager = [NSFileManager defaultManager];
|
||||
|
||||
NSError *error = nil;
|
||||
BOOL success = [manager moveItemAtPath:filepath toPath:destPath error:&error];
|
||||
|
||||
if (!success) {
|
||||
return callback([self makeErrorPayload:error]);
|
||||
}
|
||||
|
||||
callback(@[[NSNull null], [NSNumber numberWithBool:success], destPath]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
|
||||
filepath:(NSString *)filepath
|
||||
jobId:(nonnull NSNumber *)jobId
|
||||
|
|
|
@ -92,6 +92,20 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
|||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void moveFile(String filepath, String destPath, Callback callback) {
|
||||
try {
|
||||
File from = new File(filepath);
|
||||
File to = new File(destPath);
|
||||
from.renameTo(to);
|
||||
|
||||
callback.invoke(null, true, destPath);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void readDir(String directory, Callback callback) {
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue