-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
207 lines (171 loc) · 5.74 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict';
// This file supports both iOS and Android
// Stop bluebird going nuts because it can't find "self"
if (typeof self === 'undefined') {
global.self = global;
}
var RNFSManager = require('react-native').NativeModules.RNFileManager;
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter; // iOS
var DeviceEventEmitter = require('react-native').DeviceEventEmitter; // Android
var Promise = require('bluebird');
var base64 = require('base-64');
var utf8 = require('utf8');
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 _mkdir = Promise.promisify(RNFSManager.mkdir);
var _downloadFile = Promise.promisify(RNFSManager.downloadFile);
var _pathForBundle = Promise.promisify(RNFSManager.pathForBundle);
var _exists = Promise.promisify(RNFSManager.exists);
var _folderExists = Promise.promisify(RNFSManager.folderExists);
var _rename = Promise.promisify(RNFSManager.rename);
var _moveFile = Promise.promisify(RNFSManager.moveFile);
var convertError = (err) => {
if (err.isOperational && err.cause) {
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 jobId = 0;
var getJobId = () => {
jobId += 1;
return jobId;
};
var RNFS = {
readDir(dirpath) {
return _readDir(dirpath)
.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);
},
// Node style version (lowercase d). Returns just the names
readdir(dirpath) {
return RNFS.readDir(dirpath)
.then(files => {
return files.map(file => file.name);
});
},
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, encoding) {
if (!encoding) encoding = 'utf8';
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 + '"');
}
return contents;
})
.catch(convertError);
},
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);
},
pathForBundle(bundleName) {
return _pathForBundle(bundleName);
},
unlink(filepath) {
return _unlink(filepath)
.catch(convertError);
},
mkdir(filepath, excludeFromBackup) {
excludeFromBackup = !!excludeFromBackup;
return _mkdir(filepath, excludeFromBackup)
.catch(convertError);
},
downloadFile(fromUrl, toFile, begin, progress) {
var jobId = getJobId();
var subscriptionIos, subscriptionAndroid;
if (!begin) begin = (info) => {
// console.log('Download begun:', info);
};
if (begin) {
// Two different styles of subscribing to events for different platforms, hmmm....
if (NativeAppEventEmitter.addListener)
subscriptionIos = NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, begin);
if (DeviceEventEmitter.addListener)
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadBegin-' + jobId, begin);
}
if (progress) {
if (NativeAppEventEmitter.addListener)
subscriptionIos = NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, progress);
if (DeviceEventEmitter.addListener)
subscriptionAndroid = DeviceEventEmitter.addListener('DownloadProgress-' + jobId, progress);
}
return _downloadFile(fromUrl, toFile, jobId)
.then(res => {
if (subscriptionIos) subscriptionIos.remove();
if (subscriptionAndroid) subscriptionAndroid.remove();
return res;
})
.catch(convertError);
},
stopDownload(jobId) {
RNFSManager.stopDownload(jobId);
},
fileExistsAtPath(filepath) {
return _exists(filepath)
.catch(convertError);
},
folderExistsAtPath(path) {
return _folderExists(path)
.catch(convertError);
},
renameFile(filepath, newName) {
return _rename(filepath, newName)
.catch(convertError);
},
moveFile(filepath, newPath) {
return _moveFile(filepath, newPath)
.catch(convertError);
},
MainBundlePath: RNFSManager.MainBundlePath,
CachesDirectoryPath: RNFSManager.NSCachesDirectoryPath,
DocumentDirectoryPath: RNFSManager.NSDocumentDirectoryPath,
ApplicationSupportDirectoryPath: RNFSManager.NSApplicationSupportDirectoryPath
};
module.exports = RNFS;