forked from itinance/react-native-fs
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
582 lines (509 loc) · 15.9 KB
/
index.ts
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import { getReasonPhrase } from 'http-status-codes';
import { type EmitterSubscription, NativeEventEmitter } from 'react-native';
import RNFS from './ReactNativeFs';
import {
type DownloadBeginCallbackResultT,
type DownloadFileOptionsT,
type DownloadProgressCallbackResultT,
type DownloadResultT,
type FSInfoResultT,
type FileOptionsT,
type MkdirOptionsT,
type NativeDownloadFileOptionsT,
type NativeReadDirResItemT,
type NativeUploadFileOptionsT,
type PickFileOptionsT,
type ReadDirAssetsResItemT,
type ReadDirResItemT,
type StatResultT,
type StringMapT,
type UploadBeginCallbackArgT,
type UploadFileOptionsT,
type UploadFileItemT,
type UploadProgressCallbackArgT,
type UploadResultT,
} from './NativeReactNativeFs';
import {
type EncodingT,
type EncodingOptions,
decode,
encode,
normalizeFilePath,
readFileGeneric,
toEncoding,
} from './utils';
const nativeEventEmitter = new NativeEventEmitter(RNFS);
let lastJobId = 0;
// Internal functions.
type ReadDirCommand = (path: string) => Promise<NativeReadDirResItemT[]>;
/**
* Generic function used by readDir and readDirAssets.
*/
async function readDirGeneric(
dirpath: string,
command: ReadDirCommand,
): Promise<ReadDirResItemT[]> {
const files = await command(normalizeFilePath(dirpath));
const { FileTypeDirectory, FileTypeRegular } = RNFS.getConstants();
return files.map((file) => ({
ctime: (file.ctime && new Date((file.ctime as number) * 1000)) || null,
mtime: (file.mtime && new Date((file.mtime as number) * 1000)) || null,
name: file.name,
path: file.path,
size: file.size,
isFile: () => file.type === FileTypeRegular,
isDirectory: () => file.type === FileTypeDirectory,
}));
}
// Common exports.
export function appendFile(
filepath: string,
contents: string,
encodingOrOptions?: EncodingOptions,
): Promise<void> {
const b64 = encode(contents, toEncoding(encodingOrOptions));
return RNFS.appendFile(normalizeFilePath(filepath), b64);
}
export function copyFile(
from: string,
into: string,
options: FileOptionsT = {},
): Promise<void> {
return RNFS.copyFile(
normalizeFilePath(from),
normalizeFilePath(into),
options,
);
}
export function downloadFile(options: DownloadFileOptionsT): {
jobId: number;
promise: Promise<DownloadResultT>;
} {
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`',
);
}
if (
options.progressInterval &&
typeof options.progressInterval !== 'number'
) {
throw new Error(
'downloadFile: Invalid value for property `progressInterval`',
);
}
if (options.readTimeout && typeof options.readTimeout !== 'number') {
throw new Error('downloadFile: Invalid value for property `readTimeout`');
}
if (
options.connectionTimeout &&
typeof options.connectionTimeout !== 'number'
) {
throw new Error(
'downloadFile: Invalid value for property `connectionTimeout`',
);
}
if (
options.backgroundTimeout &&
typeof options.backgroundTimeout !== 'number'
) {
throw new Error(
'downloadFile: Invalid value for property `backgroundTimeout`',
);
}
const jobId = ++lastJobId;
const subscriptions: EmitterSubscription[] = [];
if (options.begin) {
subscriptions.push(
nativeEventEmitter.addListener('DownloadBegin', (res) => {
if (res.jobId === jobId && options.begin) options.begin(res);
}),
);
}
if (options.progress) {
subscriptions.push(
nativeEventEmitter.addListener('DownloadProgress', (res) => {
if (res.jobId === jobId && options.progress) options.progress(res);
}),
);
}
if (options.resumable) {
subscriptions.push(
nativeEventEmitter.addListener('DownloadResumable', (res) => {
if (res.jobId === jobId && options.resumable) options.resumable(res);
}),
);
}
var nativeOptions: NativeDownloadFileOptionsT = {
jobId: jobId,
fromUrl: options.fromUrl,
toFile: normalizeFilePath(options.toFile),
background: !!options.background,
backgroundTimeout: options.backgroundTimeout || 3600000, // 1 hour
cacheable: options.cacheable ?? true,
connectionTimeout: options.connectionTimeout || 5000,
discretionary: !!options.discretionary,
headers: options.headers || {},
progressDivider: options.progressDivider || 0,
progressInterval: options.progressInterval || 0,
readTimeout: options.readTimeout || 15000,
hasBeginCallback: !!options.begin,
hasProgressCallback: !!options.progress,
hasResumableCallback: !!options.resumable,
};
return {
jobId,
promise: (async () => {
try {
return await RNFS.downloadFile(nativeOptions);
} finally {
subscriptions.forEach((sub) => sub.remove());
}
})(),
};
}
export function exists(filepath: string): Promise<boolean> {
return RNFS.exists(normalizeFilePath(filepath));
}
export const getFSInfo: () => Promise<FSInfoResultT> = RNFS.getFSInfo;
export const isResumable: (jobId: number) => Promise<boolean> =
RNFS.isResumable;
export function mkdir(
path: string,
options: MkdirOptionsT = {},
): Promise<void> {
return RNFS.mkdir(normalizeFilePath(path), options);
}
export function moveFile(
filepath: string,
destPath: string,
options: FileOptionsT = {},
): Promise<void> {
return RNFS.moveFile(
normalizeFilePath(filepath),
normalizeFilePath(destPath),
options,
);
}
export function pickFile(
options: Partial<PickFileOptionsT> = {},
): Promise<string[]> {
return RNFS.pickFile({
mimeTypes: options.mimeTypes || ['*/*'],
});
}
export type ReadFileOptionsT = {
encoding?: EncodingT;
};
export async function read(
path: string,
// TODO: It would make more sense to read entire file by the default,
// or, if the position is given, to read from that position to the end.
length: number = 0,
position: number = 0,
encodingOrOptions?: EncodingT | ReadFileOptionsT,
): Promise<string> {
const b64 = await RNFS.read(normalizeFilePath(path), length, position);
return decode(b64, toEncoding(encodingOrOptions));
}
export function readFile(
path: string,
encodingOrOptions?: EncodingT | ReadFileOptionsT,
): Promise<string> {
return readFileGeneric(path, encodingOrOptions, RNFS.readFile);
}
export function readDir(dirpath: string): Promise<ReadDirResItemT[]> {
return readDirGeneric(dirpath, RNFS.readDir);
}
// Node style version (lowercase d). Returns just the names
export async function readdir(dirpath: string): Promise<string[]> {
const files = await RNFS.readDir(normalizeFilePath(dirpath));
return files.map((file) => file.name || '');
}
export async function stat(filepath: string): Promise<StatResultT> {
const result = await RNFS.stat(normalizeFilePath(filepath));
const { FileTypeDirectory, FileTypeRegular } = RNFS.getConstants();
return {
path: filepath,
ctime: new Date(result.ctime * 1000),
mtime: new Date(result.mtime * 1000),
size: result.size,
mode: result.mode,
originalFilepath: result.originalFilepath,
isFile: () => result.type === FileTypeRegular,
isDirectory: () => result.type === FileTypeDirectory,
};
}
export const stopDownload: (jobId: number) => void = RNFS.stopDownload;
export function touch(
filepath: string,
mtime?: Date,
ctime?: Date,
): Promise<void> {
if (ctime && !(ctime instanceof Date)) {
throw new Error('touch: Invalid value for argument `ctime`');
}
if (mtime && !(mtime instanceof Date)) {
throw new Error('touch: Invalid value for argument `mtime`');
}
return RNFS.touch(normalizeFilePath(filepath), {
ctime: ctime && ctime.getTime(),
mtime: mtime && mtime.getTime(),
});
}
export function unlink(path: string): Promise<void> {
return RNFS.unlink(normalizeFilePath(path));
}
export function uploadFiles(options: UploadFileOptionsT): {
jobId: number;
promise: Promise<UploadResultT>;
} {
const jobId = ++lastJobId;
const subscriptions: EmitterSubscription[] = [];
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(
nativeEventEmitter.addListener('UploadBegin', options.begin),
);
} else if (options.beginCallback) {
// Deprecated
subscriptions.push(
nativeEventEmitter.addListener('UploadBegin', options.beginCallback),
);
}
if (options.progress) {
subscriptions.push(
nativeEventEmitter.addListener('UploadProgress', options.progress),
);
} else if (options.progressCallback) {
// Deprecated
subscriptions.push(
nativeEventEmitter.addListener(
'UploadProgress',
options.progressCallback,
),
);
}
var nativeOptions: NativeUploadFileOptionsT = {
jobId: jobId,
toUrl: options.toUrl,
files: options.files,
binaryStreamOnly: options.binaryStreamOnly || false,
headers: options.headers || {},
fields: options.fields || {},
method: options.method || 'POST',
hasBeginCallback:
options.begin instanceof Function ||
options.beginCallback instanceof Function,
hasProgressCallback:
options.progress instanceof Function ||
options.progressCallback instanceof Function,
};
return {
jobId,
promise: RNFS.uploadFiles(nativeOptions).then((res: UploadResultT) => {
subscriptions.forEach((sub) => sub.remove());
if (res.statusCode >= 400) {
const error = Error(getReasonPhrase(res.statusCode));
(error as any).result = res;
throw error;
}
return res;
}),
};
}
export function write(
filepath: string,
contents: string,
position: number = -1,
encodingOrOptions?: EncodingOptions,
): Promise<void> {
const b64 = encode(contents, toEncoding(encodingOrOptions));
return RNFS.write(normalizeFilePath(filepath), b64, position);
}
type WriteFileOptionsT = {
encoding?: EncodingT;
NSFileProtectionKey?: string;
};
export function writeFile(
path: string,
content: string,
encodingOrOptions?: EncodingT | WriteFileOptionsT,
): Promise<void> {
const b64 = encode(content, toEncoding(encodingOrOptions));
return RNFS.writeFile(
normalizeFilePath(path),
b64,
typeof encodingOrOptions === 'object' ? encodingOrOptions : {},
);
}
// Android-specific.
export function copyFileAssets(from: string, into: string): Promise<void> {
return RNFS.copyFileAssets(normalizeFilePath(from), normalizeFilePath(into));
}
export function copyFileRes(from: string, into: string): Promise<void> {
return RNFS.copyFileRes(from, normalizeFilePath(into));
}
export function existsAssets(filepath: string): Promise<boolean> {
return RNFS.existsAssets(filepath);
}
export function existsRes(filename: string): Promise<boolean> {
return RNFS.existsRes(filename);
}
export const getAllExternalFilesDirs: () => Promise<string[]> =
RNFS.getAllExternalFilesDirs;
// TODO: algorithm should have a dedicated type, effectively enumerating all
// correct values.
export function hash(filepath: string, algorithm: string): Promise<string> {
return RNFS.hash(normalizeFilePath(filepath), algorithm);
}
export async function readDirAssets(
path: string,
): Promise<ReadDirAssetsResItemT[]> {
const res = await readDirGeneric(path, RNFS.readDirAssets);
return res as ReadDirAssetsResItemT[];
}
export function readFileAssets(
path: string,
encodingOrOptions?: EncodingOptions,
): Promise<string> {
return readFileGeneric(path, encodingOrOptions, RNFS.readFileAssets);
}
export function readFileRes(
filename: string,
encodingOrOptions?: EncodingOptions,
): Promise<string> {
return readFileGeneric(filename, encodingOrOptions, RNFS.readFileRes);
}
export const scanFile = RNFS.scanFile;
// TODO: Not documented!
// setReadable for Android
export const setReadable: (
filepath: string,
readable: boolean,
ownerOnly: boolean,
) => Promise<boolean> = RNFS.setReadable;
// iOS-specific
export const completeHandlerIOS: (jobId: number) => void =
RNFS.completeHandlerIOS;
// iOS only
// Copies fotos from asset-library (camera-roll) to a specific location
// with a given width or height
// @see: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
export function copyAssetsFileIOS(
imageUri: string,
destPath: string,
width: number,
height: number,
scale: number = 1.0,
compression: number = 1.0,
resizeMode: string = 'contain',
): Promise<string> {
return RNFS.copyAssetsFileIOS(
imageUri,
destPath,
width,
height,
scale,
compression,
resizeMode,
);
}
// iOS only
// Copies fotos from asset-library (camera-roll) to a specific location
// with a given width or height
// @see: https://developer.apple.com/reference/photos/phimagemanager/1616964-requestimageforasset
export const copyAssetsVideoIOS: (
imageUri: string,
destPath: string,
) => Promise<string> = RNFS.copyAssetsVideoIOS;
// TODO: This is presumably iOS-specific, it is not documented,
// so it should be double-checked, what it does.
export const pathForBundle: (bundle: string) => Promise<string> =
RNFS.pathForBundle;
export const pathForGroup: (group: string) => Promise<string> =
RNFS.pathForGroup;
export const resumeDownload: (jobId: number) => void = RNFS.resumeDownload;
export const stopUpload: (jobId: number) => void = RNFS.stopUpload;
// Windows-specific.
// Windows workaround for slow copying of large folders of files
export function copyFolder(from: string, into: string): Promise<void> {
return RNFS.copyFolder(normalizeFilePath(from), normalizeFilePath(into));
}
const {
MainBundlePath,
CachesDirectoryPath,
ExternalCachesDirectoryPath,
DocumentDirectoryPath,
DownloadDirectoryPath,
ExternalDirectoryPath,
ExternalStorageDirectoryPath,
TemporaryDirectoryPath,
LibraryDirectoryPath,
PicturesDirectoryPath, // For Windows
FileProtectionKeys,
RoamingDirectoryPath, // For Windows
} = RNFS.getConstants();
export {
type DownloadBeginCallbackResultT,
type DownloadFileOptionsT,
type DownloadProgressCallbackResultT,
type DownloadResultT,
type EncodingT,
type FileOptionsT,
type FSInfoResultT,
type MkdirOptionsT,
type ReadDirAssetsResItemT,
type ReadDirResItemT,
type StringMapT,
type UploadBeginCallbackArgT,
type UploadFileItemT,
type UploadFileOptionsT,
type UploadProgressCallbackArgT,
type UploadResultT,
type WriteFileOptionsT,
MainBundlePath,
CachesDirectoryPath,
ExternalCachesDirectoryPath,
DocumentDirectoryPath,
DownloadDirectoryPath,
ExternalDirectoryPath,
ExternalStorageDirectoryPath,
TemporaryDirectoryPath,
LibraryDirectoryPath,
PicturesDirectoryPath, // For Windows
FileProtectionKeys,
RoamingDirectoryPath, // For Windows
};