2015-05-08 17:05:37 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var RNFSManager = require('NativeModules').RNFSManager;
|
|
|
|
var Promise = require('bluebird');
|
|
|
|
var base64 = require('base-64');
|
|
|
|
|
|
|
|
var _readDir = Promise.promisify(RNFSManager.readDir);
|
|
|
|
var _stat = Promise.promisify(RNFSManager.stat);
|
|
|
|
var _readFile = Promise.promisify(RNFSManager.readFile);
|
2015-05-08 20:47:54 +00:00
|
|
|
var _writeFile = Promise.promisify(RNFSManager.writeFile);
|
2015-05-08 17:05:37 +00:00
|
|
|
|
|
|
|
var convertError = (err) => {
|
|
|
|
var error = new Error(err.description);
|
|
|
|
error.code = err.code;
|
|
|
|
throw error;
|
|
|
|
};
|
|
|
|
|
|
|
|
var NSFileTypeRegular = RNFSManager.NSFileTypeRegular;
|
|
|
|
var NSFileTypeDirectory = RNFSManager.NSFileTypeDirectory;
|
|
|
|
|
|
|
|
var RNFS = {
|
|
|
|
|
|
|
|
readDir(path, rootDir) {
|
|
|
|
return _readDir(path, rootDir)
|
2015-05-08 20:47:54 +00:00
|
|
|
.catch(convertError);
|
2015-05-08 17:05:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stat(filepath) {
|
|
|
|
return _stat(filepath)
|
2015-05-08 20:47:54 +00:00
|
|
|
.then((result) => {
|
|
|
|
return {
|
|
|
|
'ctime': new Date(result.ctime*1000),
|
|
|
|
'mtime': new Date(result.mtime*1000),
|
|
|
|
'size': result.size,
|
|
|
|
'mode': result.mode,
|
|
|
|
isFile: () => result.type === NSFileTypeRegular,
|
|
|
|
isDirectory: () => result.type === NSFileTypeDirectory,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch(convertError);
|
2015-05-08 17:05:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
readFile(filepath, shouldDecode) {
|
|
|
|
var p = _readFile(filepath);
|
|
|
|
|
|
|
|
if (shouldDecode !== false) {
|
|
|
|
p = p.then((data) => {
|
|
|
|
return base64.decode(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-05-08 20:47:54 +00:00
|
|
|
writeFile(filepath, contents, options) {
|
|
|
|
return _writeFile(filepath, base64.encode(contents), options)
|
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-05-08 17:05:37 +00:00
|
|
|
MainBundle: RNFSManager.MainBundleDirectory,
|
|
|
|
CachesDirectory: RNFSManager.NSCachesDirectory,
|
2015-05-08 17:17:59 +00:00
|
|
|
DocumentDirectory: RNFSManager.NSDocumentDirectory,
|
|
|
|
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
|
|
|
|
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath
|
2015-05-08 17:05:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RNFS;
|