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-11-20 00:16:13 +00:00
|
|
|
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter; // iOS
|
|
|
|
var DeviceEventEmitter = require('react-native').DeviceEventEmitter; // Android
|
2015-05-08 20:05:37 +03:00
|
|
|
var Promise = require('bluebird');
|
|
|
|
var base64 = require('base-64');
|
2015-10-22 01:25:52 +01:00
|
|
|
var utf8 = require('utf8');
|
2015-05-08 20:05:37 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-11-20 00:16:13 +00:00
|
|
|
var jobId = 0;
|
|
|
|
|
|
|
|
var getJobId = () => {
|
|
|
|
jobId += 1;
|
|
|
|
return jobId;
|
|
|
|
};
|
|
|
|
|
2015-05-08 20:05:37 +03:00
|
|
|
var RNFS = {
|
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
readDir(dirpath) {
|
|
|
|
return _readDir(dirpath)
|
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
|
|
|
},
|
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
// Node style version (lowercase d). Returns just the names
|
|
|
|
readdir(dirpath) {
|
|
|
|
return RNFS.readDir(dirpath)
|
|
|
|
.then(files => {
|
|
|
|
return files.map(file => file.name);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
readFile(filepath, encoding) {
|
|
|
|
if (!encoding) encoding = 'utf8';
|
2015-05-08 20:05:37 +03:00
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
return _readFile(filepath)
|
|
|
|
.then((b64) => {
|
|
|
|
var contents;
|
|
|
|
|
|
|
|
if (encoding === 'utf8') {
|
|
|
|
contents = utf8.decode(base64.decode(b64));
|
|
|
|
} else if (encoding === 'ascii') {
|
|
|
|
contents = base64.decode(b64);
|
|
|
|
} else if (encoding === 'base64') {
|
|
|
|
contents = b64;
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid encoding type "' + encoding + '"');
|
|
|
|
}
|
2015-05-08 20:05:37 +03:00
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
return contents;
|
|
|
|
})
|
|
|
|
.catch(convertError);
|
2015-05-08 20:05:37 +03:00
|
|
|
},
|
|
|
|
|
2015-10-22 01:25:52 +01:00
|
|
|
writeFile(filepath, contents, encoding, options) {
|
|
|
|
var b64;
|
|
|
|
|
|
|
|
if (!encoding) encoding = 'utf8';
|
|
|
|
|
|
|
|
if (encoding === 'utf8') {
|
|
|
|
b64 = base64.encode(utf8.encode(contents));
|
|
|
|
} else if (encoding === 'ascii') {
|
|
|
|
b64 = base64.encode(contents);
|
|
|
|
} else if (encoding === 'base64') {
|
|
|
|
b64 = contents;
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid encoding type "' + encoding + '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
return _writeFile(filepath, b64, options)
|
2015-05-08 23:47:54 +03:00
|
|
|
.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-22 01:25:52 +01:00
|
|
|
mkdir(filepath, excludeFromBackup) {
|
|
|
|
excludeFromBackup = !!excludeFromBackup;
|
|
|
|
return _mkdir(filepath, excludeFromBackup)
|
2015-10-20 23:31:29 +01:00
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
downloadFile(fromUrl, toFile, begin, progress) {
|
2015-11-20 00:16:13 +00:00
|
|
|
var jobId = getJobId();
|
|
|
|
var subscriptionIos, subscriptionAndroid;
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
if (!begin) begin = (info) => {
|
|
|
|
console.log('Download begun:', info);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (begin) {
|
2015-11-20 00:16:13 +00:00
|
|
|
// Two different styles of subscribing to events for different platforms, hmmm....
|
|
|
|
if (NativeAppEventEmitter.addListener)
|
2015-11-23 16:29:25 +00:00
|
|
|
subscriptionIos = NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, begin);
|
|
|
|
if (DeviceEventEmitter.addListener)
|
|
|
|
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadBegin-' + jobId, begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progress) {
|
2015-11-20 00:16:13 +00:00
|
|
|
if (NativeAppEventEmitter.addListener)
|
2015-11-23 16:29:25 +00:00
|
|
|
subscriptionIos = NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress);
|
|
|
|
if (DeviceEventEmitter.addListener)
|
2015-11-20 00:16:13 +00:00
|
|
|
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadProgress-' + jobId, progress);
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
return _downloadFile(fromUrl, toFile, jobId)
|
2015-11-20 00:16:13 +00:00
|
|
|
.then(res => {
|
|
|
|
if (subscriptionIos) subscriptionIos.remove();
|
|
|
|
if (subscriptionAndroid) subscriptionAndroid.remove();
|
|
|
|
return res;
|
|
|
|
})
|
2015-10-21 00:17:03 +01:00
|
|
|
.catch(convertError);
|
|
|
|
},
|
|
|
|
|
2015-10-23 00:13:12 +01:00
|
|
|
MainBundlePath: RNFSManager.MainBundlePath,
|
2015-05-08 20:17:59 +03:00
|
|
|
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
|
|
|
|
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath
|
2015-05-08 20:05:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RNFS;
|