forked from CommercialTribe/react-native-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FS.common.js
377 lines (311 loc) · 13.3 KB
/
FS.common.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* React Native FS
* @flow
*/
'use strict';
// This file supports both iOS and Android
var RNFSManager = require('react-native').NativeModules.RNFSManager;
var NativeAppEventEmitter = require('react-native').NativeAppEventEmitter; // iOS
var DeviceEventEmitter = require('react-native').DeviceEventEmitter; // Android
var base64 = require('base-64');
var utf8 = require('utf8');
var RNFSFileTypeRegular = RNFSManager.RNFSFileTypeRegular;
var RNFSFileTypeDirectory = RNFSManager.RNFSFileTypeDirectory;
var jobId = 0;
var getJobId = () => {
jobId += 1;
return jobId;
};
var normalizeFilePath = (path: string) => (path.startsWith('file://') ? path.slice(7) : path);
type MkdirOptions = {
NSURLIsExcludedFromBackupKey?: boolean; // iOS only
};
type ReadDirItem = {
name: string; // The name of the item
path: string; // The absolute path to the item
size: string; // Size in bytes
isFile: () => boolean; // Is the file just a file?
isDirectory: () => boolean; // Is the file a directory?
};
type StatResult = {
name: string; // The name of the item
path: string; // The absolute path to the item
size: string; // Size in bytes
mode: number; // UNIX file mode
isFile: () => boolean; // Is the file just a file?
isDirectory: () => boolean; // Is the file a directory?
};
type Headers = { [name: string]: string };
type Fields = { [name: string]: string };
type DownloadFileOptions = {
fromUrl: string; // URL to download file from
toFile: string; // Local filesystem path to save the file to
headers?: Headers; // An object of headers to be passed to the server
background?: boolean; // iOS only
progressDivider?: number;
begin?: (res: DownloadBeginCallbackResult) => void;
progress?: (res: DownloadProgressCallbackResult) => void;
};
type DownloadBeginCallbackResult = {
jobId: number; // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
statusCode: number; // The HTTP status code
contentLength: number; // The total size in bytes of the download resource
headers: Headers; // The HTTP response headers from the server
};
type DownloadProgressCallbackResult = {
jobId: number; // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
contentLength: number; // The total size in bytes of the download resource
bytesWritten: number; // The number of bytes written to the file so far
};
type DownloadResult = {
jobId: number; // The download job ID, required if one wishes to cancel the download. See `stopDownload`.
statusCode: number; // The HTTP status code
bytesWritten: number; // The number of bytes written to the file
};
type UploadFileOptions = {
toUrl: string; // URL to upload file to
files: UploadFileItem[]; // An array of objects with the file information to be uploaded.
headers?: Headers; // An object of headers to be passed to the server
fields?: Fields; // An object of fields to be passed to the server
method?: string; // Default is 'POST', supports 'POST' and 'PUT'
begin?: (res: UploadBeginCallbackResult) => void;
progress?: (res: UploadProgressCallbackResult) => void;
};
type UploadFileItem = {
name: string; // Name of the file, if not defined then filename is used
filename: string; // Name of file
filepath: string; // Path to file
filetype: string; // The mimetype of the file to be uploaded, if not defined it will get mimetype from `filepath` extension
};
type UploadBeginCallbackResult = {
jobId: number; // The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.
};
type UploadProgressCallbackResult = {
jobId: number; // The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.
totalBytesExpectedToSend: number; // The total number of bytes that will be sent to the server
totalBytesSent: number; // The number of bytes sent to the server
};
type UploadResult = {
jobId: number; // The upload job ID, required if one wishes to cancel the upload. See `stopUpload`.
statusCode: number; // The HTTP status code
headers: Headers; // The HTTP response headers from the server
body: string; // The HTTP response body
};
type FSInfoResult = {
totalSpace: number; // The total amount of storage space on the device (in bytes).
freeSpace: number; // The amount of available storage space on the device (in bytes).
};
var RNFS = {
mkdir(filepath: string, options: MkdirOptions = {}): Promise<void> {
return RNFSManager.mkdir(normalizeFilePath(filepath), options).then(() => void 0);
},
moveFile(filepath: string, destPath: string): Promise<void> {
return RNFSManager.moveFile(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
},
copyFile(filepath: string, destPath: string): Promise<void> {
return RNFSManager.copyFile(normalizeFilePath(filepath), normalizeFilePath(destPath)).then(() => void 0);
},
pathForBundle(bundleNamed: string): Promise<string> {
return RNFSManager.pathForBundle(bundleNamed);
},
getFSInfo(): Promise<FSInfoResult> {
return RNFSManager.getFSInfo();
},
unlink(filepath: string): Promise<void> {
return RNFSManager.unlink(normalizeFilePath(filepath)).then(() => void 0);
},
exists(filepath: string): Promise<boolean> {
return RNFSManager.exists(normalizeFilePath(filepath));
},
stopDownload(jobId: number): void {
RNFSManager.stopDownload(jobId);
},
stopUpload(jobId: number): void {
RNFSManager.stopUpload(jobId);
},
readDir(dirpath: string): Promise<ReadDirItem[]> {
return RNFSManager.readDir(normalizeFilePath(dirpath)).then(files => {
return files.map(file => ({
name: file.name,
path: file.path,
size: file.size,
isFile: () => file.type === RNFSFileTypeRegular,
isDirectory: () => file.type === RNFSFileTypeDirectory,
}));
});
},
// Node style version (lowercase d). Returns just the names
readdir(dirpath: string): Promise<string[]> {
return RNFS.readDir(normalizeFilePath(dirpath)).then(files => {
return files.map(file => file.name);
});
},
stat(filepath: string): Promise<StatResult> {
return RNFSManager.stat(normalizeFilePath(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 === RNFSFileTypeRegular,
isDirectory: () => result.type === RNFSFileTypeDirectory,
};
});
},
readFile(filepath: string, encodingOrOptions?: any): Promise<string> {
var options = {
encoding: 'utf8'
};
if (encodingOrOptions) {
if (typeof encodingOrOptions === 'string') {
options.encoding = encodingOrOptions;
} else if (typeof encodingOrOptions === 'object') {
options = encodingOrOptions;
}
}
return RNFSManager.readFile(normalizeFilePath(filepath)).then((b64) => {
var contents;
if (options.encoding === 'utf8') {
contents = utf8.decode(base64.decode(b64));
} else if (options.encoding === 'ascii') {
contents = base64.decode(b64);
} else if (options.encoding === 'base64') {
contents = b64;
} else {
throw new Error('Invalid encoding type "' + String(options.encoding) + '"');
}
return contents;
});
},
writeFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
var b64;
var options = {
encoding: 'utf8'
};
if (encodingOrOptions) {
if (typeof encodingOrOptions === 'string') {
options.encoding = encodingOrOptions;
} else if (typeof encodingOrOptions === 'object') {
options = encodingOrOptions;
}
}
if (options.encoding === 'utf8') {
b64 = base64.encode(utf8.encode(contents));
} else if (options.encoding === 'ascii') {
b64 = base64.encode(contents);
} else if (options.encoding === 'base64') {
b64 = contents;
} else {
throw new Error('Invalid encoding type "' + options.encoding + '"');
}
return RNFSManager.writeFile(normalizeFilePath(filepath), b64).then(() => void 0);
},
appendFile(filepath: string, contents: string, encodingOrOptions?: any): Promise<void> {
var b64;
var options = {
encoding: 'utf8'
};
if (encodingOrOptions) {
if (typeof encodingOrOptions === 'string') {
options.encoding = encodingOrOptions;
} else if (typeof encodingOrOptions === 'object') {
options = encodingOrOptions;
}
}
if (options.encoding === 'utf8') {
b64 = base64.encode(utf8.encode(contents));
} else if (options.encoding === 'ascii') {
b64 = base64.encode(contents);
} else if (options.encoding === 'base64') {
b64 = contents;
} else {
throw new Error('Invalid encoding type "' + options.encoding + '"');
}
return RNFSManager.appendFile(normalizeFilePath(filepath), b64);
},
downloadFile(options: DownloadFileOptions): { jobId: number, promise: Promise<DownloadResult> } {
if (typeof options !== 'object') throw new Error('downloadFile: Invalid value for argument `options`');
if (typeof options.fromUrl !== 'string') throw new Error('downloadFile: Invalid value for property `fromUrl`');
if (typeof options.toFile !== 'string') throw new Error('downloadFile: Invalid value for property `toFile`');
if (options.headers && typeof options.headers !== 'object') throw new Error('downloadFile: Invalid value for property `headers`');
if (options.background && typeof options.background !== 'boolean') throw new Error('downloadFile: Invalid value for property `background`');
if (options.progressDivider && typeof options.progressDivider !== 'number') throw new Error('downloadFile: Invalid value for property `progressDivider`');
var jobId = getJobId();
var subscriptions = [];
if (options.begin) {
subscriptions.push(NativeAppEventEmitter.addListener('DownloadBegin-' + jobId, options.begin));
}
if (options.progress) {
subscriptions.push(NativeAppEventEmitter.addListener('DownloadProgress-' + jobId, options.progress));
}
var bridgeOptions = {
jobId: jobId,
fromUrl: options.fromUrl,
toFile: normalizeFilePath(options.toFile),
headers: options.headers || {},
background: !!options.background,
progressDivider: options.progressDivider || 0
};
return {
jobId,
promise: RNFSManager.downloadFile(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
})
};
},
uploadFiles(options: UploadFileOptions): { jobId: number, promise: Promise<UploadResult> } {
if (!RNFSManager.uploadFiles) {
return {
jobId: -1,
promise: Promise.reject(new Error('`uploadFiles` is unsupported on this platform'))
};
}
var jobId = getJobId();
var subscriptions = [];
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.begin) {
subscriptions.push(NativeAppEventEmitter.addListener('UploadBegin-' + jobId, options.begin));
}
if (options.beginCallback) {
// Deprecated
subscriptions.push(NativeAppEventEmitter.addListener('UploadBegin-' + jobId, options.beginCallback));
}
if (options.progress) {
subscriptions.push(NativeAppEventEmitter.addListener('UploadProgress-' + jobId, options.progress));
}
if (options.progressCallback) {
// Deprecated
subscriptions.push(NativeAppEventEmitter.addListener('UploadProgress-' + jobId, options.progressCallback));
}
var bridgeOptions = {
jobId: jobId,
toUrl: options.toUrl,
files: options.files,
headers: options.headers || {},
fields: options.fields || {},
method: options.method || 'POST'
};
return {
jobId,
promise: RNFSManager.uploadFiles(bridgeOptions).then(res => {
subscriptions.forEach(sub => sub.remove());
return res;
})
};
},
MainBundlePath: RNFSManager.RNFSMainBundlePath,
CachesDirectoryPath: RNFSManager.RNFSCachesDirectoryPath,
DocumentDirectoryPath: RNFSManager.RNFSDocumentDirectoryPath,
ExternalDirectoryPath: RNFSManager.RNFSExternalDirectoryPath,
ExternalStorageDirectoryPath: RNFSManager.RNFSExternalStorageDirectoryPath,
TemporaryDirectoryPath: RNFSManager.RNFSTemporaryDirectoryPath,
LibraryDirectoryPath: RNFSManager.RNFSLibraryDirectoryPath,
PicturesDirectoryPath: RNFSManager.RNFSPicturesDirectoryPath
};
module.exports = RNFS;