react-native-fs/FS.common.js

232 lines
6.6 KiB
JavaScript
Raw Normal View History

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;
2016-05-14 23:19:27 +02:00
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter;
2015-05-08 20:05:37 +03:00
var Promise = require('bluebird');
var base64 = require('base-64');
var utf8 = require('utf8');
2015-05-08 20:05:37 +03:00
var _readDir = Promise.promisify(RNFSManager.readDir);
2016-01-29 02:20:02 +08:00
var _exists = Promise.promisify(RNFSManager.exists);
2015-05-08 20:05:37 +03:00
var _stat = Promise.promisify(RNFSManager.stat);
var _readFile = Promise.promisify(RNFSManager.readFile);
var _writeFile = Promise.promisify(RNFSManager.writeFile);
2016-01-16 10:52:56 -08:00
var _moveFile = Promise.promisify(RNFSManager.moveFile);
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);
2016-05-30 23:13:50 +01:00
var _uploadFiles = Promise.promisify(RNFSManager.uploadFiles);
2015-08-18 14:27:52 -04:00
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
2016-03-18 10:53:00 -07:00
var _getFSInfo = Promise.promisify(RNFSManager.getFSInfo);
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 = {
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,
}));
})
.catch(convertError);
2015-05-08 20:05:37 +03: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)
.then((result) => {
return {
2016-05-30 23:13:50 +01:00
'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
},
2016-03-18 10:53:00 -07:00
2016-01-29 02:20:02 +08:00
exists(filepath) {
return _exists(filepath)
2016-01-28 21:36:31 +08:00
.catch(convertError);
},
2015-05-08 20:05:37 +03:00
readFile(filepath, encoding) {
if (!encoding) encoding = 'utf8';
2015-05-08 20:05:37 +03: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
return contents;
})
.catch(convertError);
2015-05-08 20:05:37 +03: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)
.catch(convertError);
},
2016-01-27 16:52:56 +00:00
moveFile(filepath, destPath) {
return _moveFile(filepath, destPath)
2016-01-16 10:52:56 -08:00
.catch(convertError);
},
2015-08-18 14:27:52 -04:00
pathForBundle(bundleName) {
return _pathForBundle(bundleName);
},
2016-03-18 10:53:00 -07:00
getFSInfo() {
return _getFSInfo()
.catch(convertError);
},
2015-05-09 00:17:10 +03:00
unlink(filepath) {
return _unlink(filepath)
.catch(convertError);
},
mkdir(filepath, excludeFromBackup) {
excludeFromBackup = !!excludeFromBackup;
return _mkdir(filepath, excludeFromBackup)
2015-10-20 23:31:29 +01:00
.catch(convertError);
},
downloadFile(fromUrl, toFile, begin, progress) {
2015-11-20 00:16:13 +00:00
var jobId = getJobId();
2016-05-14 23:19:27 +02:00
var subscriptions = [];
2016-05-30 23:13:50 +01:00
if (begin) {
2016-05-14 23:19:27 +02:00
subscriptions.push(NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, begin));
}
if (progress) {
2016-05-14 23:19:27 +02:00
subscriptions.push(NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress));
2015-11-20 00:16:13 +00:00
}
return _downloadFile(fromUrl, toFile, jobId)
2015-11-20 00:16:13 +00:00
.then(res => {
2016-05-14 23:19:27 +02:00
subscriptions.forEach(sub => sub.remove());
2015-11-20 00:16:13 +00:00
return res;
})
2015-10-21 00:17:03 +01:00
.catch(convertError);
},
2015-11-23 17:08:52 +00:00
stopDownload(jobId) {
RNFSManager.stopDownload(jobId);
},
2016-05-30 23:13:50 +01:00
uploadFiles(options) {
var jobId = getJobId();
2016-05-30 23:35:34 +01:00
var subscriptions = [];
2016-05-30 23:13:50 +01:00
if (typeof options !== 'object') throw new Error('uploadFiles: Invalid value for argument `options`');
if (typeof options.toUrl !== 'string') throw new Error('uploadFiles: Invalid value for property `toUrl`');
if (!Array.isArray(options.files)) throw new Error('uploadFiles: Invalid value for property `files`');
if (options.headers && typeof options.headers !== 'object') throw new Error('uploadFiles: Invalid value for property `headers`');
if (options.fields && typeof options.fields !== 'object') throw new Error('uploadFiles: Invalid value for property `fields`');
if (options.method && typeof options.method !== 'string') throw new Error('uploadFiles: Invalid value for property `method`');
if (options.beginCallback) {
2016-05-30 23:35:34 +01:00
subscriptions.push(NativeAppEventEmitter.addListener('UploadBegin-' + jobId, options.beginCallback));
2016-05-30 23:13:50 +01:00
}
if (options.progressCallback) {
2016-05-30 23:35:34 +01:00
subscriptions.push(NativeAppEventEmitter.addListener('UploadProgress-' + jobId, options.progressCallback));
2016-05-30 23:13:50 +01:00
}
var bridgeOptions = {
jobId: jobId,
toUrl: options.toUrl,
files: options.files,
headers: options.headers || {},
fields: options.fields || {},
method: options.method || 'POST'
};
return _uploadFiles(bridgeOptions)
.then(res => {
2016-05-30 23:35:34 +01:00
subscriptions.forEach(sub => sub.remove());
2016-05-30 23:13:50 +01:00
return res;
});
},
stopUpload(jobId) {
RNFSManager.stopUpload(jobId);
},
2015-10-23 00:13:12 +01:00
MainBundlePath: RNFSManager.MainBundlePath,
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
2015-12-02 11:12:06 +09:00
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath,
ExternalDirectoryPath: RNFSManager.NSExternalDirectoryPath,
2016-05-14 23:42:57 +02:00
TemporaryDirectoryPath: RNFSManager.NSTemporaryDirectoryPath,
LibraryDirectoryPath: RNFSManager.NSLibraryDirectoryPath,
PicturesDirectoryPath: RNFSManager.NSPicturesDirectoryPath
2015-05-08 20:05:37 +03:00
};
module.exports = RNFS;