Add copyFile
This commit is contained in:
parent
f1128b26cd
commit
838e39d984
|
@ -25,6 +25,7 @@ var _readFile = Promise.promisify(RNFSManager.readFile);
|
|||
var _writeFile = Promise.promisify(RNFSManager.writeFile);
|
||||
var _appendFile = Promise.promisify(RNFSManager.appendFile);
|
||||
var _moveFile = Promise.promisify(RNFSManager.moveFile);
|
||||
var _copyFile = Promise.promisify(RNFSManager.copyFile);
|
||||
var _unlink = Promise.promisify(RNFSManager.unlink);
|
||||
var _mkdir = Promise.promisify(RNFSManager.mkdir);
|
||||
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
|
||||
|
@ -250,6 +251,10 @@ var RNFS = {
|
|||
return _moveFile(filepath, destPath).catch(convertError);
|
||||
},
|
||||
|
||||
copyFile(filepath: string, destPath: string): Promise<void> {
|
||||
return _copyFile(filepath, destPath).catch(convertError);
|
||||
},
|
||||
|
||||
pathForBundle(bundleName: string): Promise<string> {
|
||||
return _pathForBundle(bundleName);
|
||||
},
|
||||
|
|
|
@ -307,6 +307,10 @@ type WriteFileOptions = {
|
|||
|
||||
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.
|
||||
|
||||
### `copyFile(filepath: string, destPath: string): Promise<void>`
|
||||
|
||||
Copies the file located at `filepath` to `destPath`.
|
||||
|
||||
### `unlink(filepath: string): Promise<void>`
|
||||
|
||||
Unlinks the item at `filepath`. If the item does not exist, an error will be thrown.
|
||||
|
|
|
@ -207,6 +207,22 @@ RCT_EXPORT_METHOD(moveFile:(NSString *)filepath
|
|||
callback(@[[NSNull null], [NSNumber numberWithBool:success], destPath]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(copyFile:(NSString *)filepath
|
||||
destPath:(NSString *)destPath
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
NSFileManager *manager = [NSFileManager defaultManager];
|
||||
|
||||
NSError *error = nil;
|
||||
BOOL success = [manager copyItemAtPath:filepath toPath:destPath error:&error];
|
||||
|
||||
if (!success) {
|
||||
return callback([self makeErrorPayload:error]);
|
||||
}
|
||||
|
||||
callback(@[[NSNull null], [NSNumber numberWithBool:success], destPath]);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(downloadFile:(NSDictionary *)options
|
||||
callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
|
|
|
@ -136,6 +136,27 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
|||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void copyFile(String filepath, String destPath, Callback callback) {
|
||||
try {
|
||||
InputStream in = new FileInputStream(filepath);
|
||||
OutputStream out = new FileOutputStream(destPath);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, length);
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
|
||||
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