2015-05-08 20:05:37 +03:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-19 14:40:05 +01:00
|
|
|
// This file supports both iOS and Android
|
|
|
|
|
|
|
|
// Stop bluebird going nuts because it can't find "self"
|
|
|
|
if (typeof self === 'undefined') {
|
|
|
|
global.self = global;
|
|
|
|
}
|
|
|
|
|
2015-09-30 18:32:58 -07:00
|
|
|
var RNFSManager = require('react-native').NativeModules.RNFSManager;
|
2015-05-08 20:05:37 +03:00
|
|
|
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 23:47:54 +03:00
|
|
|
var _writeFile = Promise.promisify(RNFSManager.writeFile);
|
2015-05-09 00:17:10 +03:00
|
|
|
var _unlink = Promise.promisify(RNFSManager.unlink);
|
2015-10-20 23:31:29 +01:00
|
|
|
var _mkdir = Promise.promisify(RNFSManager.mkdir);
|
2015-10-21 00:17:03 +01:00
|
|
|
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
|
2015-08-18 14:27:52 -04:00
|
|
|
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
|
2015-05-08 20:05:37 +03:00
|
|
|
|
|
|
|
var convertError = (err) => {
|
2015-10-20 23:31:29 +01:00
|
|
|
if (err.isOperational && err.cause) {
|
2015-05-09 00:17:10 +03:00
|
|
|
err = err.cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
var error = new Error(err.description || err.message);
|
2015-05-08 20:05:37 +03:00
|
|
|
error.code = err.code;
|
|
|
|
throw error;
|
|
|
|
};
|
|
|
|
|
|
|
|
var NSFileTypeRegular = RNFSManager.NSFileTypeRegular;
|
|
|
|
var NSFileTypeDirectory = RNFSManager.NSFileTypeDirectory;
|
|
|
|
|
|
|
|
var RNFS = {
|
|
|
|
|
|
|
|
readDir(path, rootDir) {
|
|
|
|
return _readDir(path, rootDir)
|
2015-10-20 23:31:29 +01:00
|
|
|
.then(files => {
|
|
|
|
return files.map(file => ({
|
|
|
|
name: file.name,
|
|
|
|
path: file.path,
|
|
|
|
size: file.size,
|
|
|
|
isFile: () => file.type === NSFileTypeRegular,
|
|
|
|
isDirectory: () => file.type === NSFileTypeDirectory,
|
|
|
|
}));
|
|
|
|
})
|
2015-05-08 23:47:54 +03:00
|
|
|
.catch(convertError);
|
2015-05-08 20:05:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
stat(filepath) {
|
|
|
|
return _stat(filepath)
|
2015-05-08 23:47:54 +03: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 20:05:37 +03: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 23:47:54 +03:00
|
|
|
writeFile(filepath, contents, options) {
|
|
|
|
return _writeFile(filepath, base64.encode(contents), options)
|
|
|
|
.catch(convertError);
|
|
|
|
},
|
2015-08-19 14:31:30 -04:00
|
|
|
|
2015-08-18 14:27:52 -04:00
|
|
|
pathForBundle(bundleName) {
|
2015-08-19 14:31:30 -04:00
|
|
|
return _pathForBundle(bundleName);
|
|
|
|
},
|
2015-05-08 23:47:54 +03:00
|
|
|
|
2015-05-09 00:17:10 +03:00
|
|
|
unlink(filepath) {
|
|
|
|
return _unlink(filepath)
|
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-10-20 23:31:29 +01:00
|
|
|
mkdir(filepath) {
|
|
|
|
return _mkdir(filepath)
|
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-10-21 00:17:03 +01:00
|
|
|
downloadFile(url, filepath) {
|
|
|
|
return _downloadFile(url, filepath)
|
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-05-08 20:05:37 +03:00
|
|
|
MainBundle: RNFSManager.MainBundleDirectory,
|
|
|
|
CachesDirectory: RNFSManager.NSCachesDirectory,
|
2015-05-08 20:17:59 +03:00
|
|
|
DocumentDirectory: RNFSManager.NSDocumentDirectory,
|
|
|
|
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
|
|
|
|
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath
|
2015-05-08 20:05:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RNFS;
|