forked from itinance/react-native-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFS.ios.js
More file actions
77 lines (62 loc) · 1.95 KB
/
FS.ios.js
File metadata and controls
77 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'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);
var _writeFile = Promise.promisify(RNFSManager.writeFile);
var _unlink = Promise.promisify(RNFSManager.unlink);
var convertError = (err) => {
if (err.isOperational) {
err = err.cause;
}
var error = new Error(err.description || err.message);
error.code = err.code;
throw error;
};
var NSFileTypeRegular = RNFSManager.NSFileTypeRegular;
var NSFileTypeDirectory = RNFSManager.NSFileTypeDirectory;
var RNFS = {
readDir(path, rootDir) {
return _readDir(path, rootDir)
.catch(convertError);
},
stat(filepath) {
return _stat(filepath)
.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);
},
readFile(filepath, shouldDecode) {
var p = _readFile(filepath);
if (shouldDecode !== false) {
p = p.then((data) => {
return base64.decode(data);
});
}
return p.catch(convertError);
},
writeFile(filepath, contents, options) {
return _writeFile(filepath, base64.encode(contents), options)
.catch(convertError);
},
unlink(filepath) {
return _unlink(filepath)
.catch(convertError);
},
MainBundle: RNFSManager.MainBundleDirectory,
CachesDirectory: RNFSManager.NSCachesDirectory,
DocumentDirectory: RNFSManager.NSDocumentDirectory,
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath
};
module.exports = RNFS;