add exist method

This commit is contained in:
iwater 2016-01-28 21:36:31 +08:00
parent 8ef4fa1f65
commit dd320b3a5a
4 changed files with 31 additions and 0 deletions

View File

@ -15,6 +15,7 @@ var base64 = require('base-64');
var utf8 = require('utf8');
var _readDir = Promise.promisify(RNFSManager.readDir);
var _exist = Promise.promisify(RNFSManager.exist);
var _stat = Promise.promisify(RNFSManager.stat);
var _readFile = Promise.promisify(RNFSManager.readFile);
var _writeFile = Promise.promisify(RNFSManager.writeFile);
@ -81,6 +82,11 @@ var RNFS = {
})
.catch(convertError);
},
exist(filepath) {
return _exist(filepath)
.catch(convertError);
},
readFile(filepath, encoding) {
if (!encoding) encoding = 'utf8';

View File

@ -200,6 +200,12 @@ The promise resolves with an array, which contains a boolean and the path that h
Also recursively deletes directories (works like Linux `rm -rf`).
### `promise exist(filepath)`
check if the item exist at `filepath`. If the item does not exist, return false.
The promise resolves with boolean.
### `promise mkdir(filepath [, excludeFromBackup])`
Create a directory at `filepath`. Automatically creates parents and does not throw if already exists (works like Linux `mkdir -p`).

View File

@ -56,6 +56,14 @@ RCT_EXPORT_METHOD(readDir:(NSString *)dirPath
callback(@[[NSNull null], contents]);
}
RCT_EXPORT_METHOD(exist:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath];
callback(@[[NSNull null], [NSNumber numberWithBool:fileExists]]);
}
RCT_EXPORT_METHOD(stat:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{

View File

@ -71,6 +71,17 @@ public class RNFSManager extends ReactContextBaseJavaModule {
callback.invoke(makeErrorPayload(ex));
}
}
@ReactMethod
public void exist(String filepath, Callback callback) {
try {
File file = new File(filepath);
callback.invoke(null, file.exists());
} catch (Exception ex) {
ex.printStackTrace();
callback.invoke(makeErrorPayload(ex));
}
}
@ReactMethod
public void readFile(String filepath, Callback callback) {