forked from wkh237/react-native-fetch-blob
-
Notifications
You must be signed in to change notification settings - Fork 787
/
fs.js
431 lines (397 loc) · 12.3 KB
/
fs.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
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
// Copyright 2016 wkh237@github. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// import type {RNFetchBlobConfig, RNFetchBlobNative, RNFetchBlobStream} from './types'
import {NativeModules, Platform} from 'react-native'
import RNFetchBlobSession from './class/RNFetchBlobSession'
import RNFetchBlobWriteStream from './class/RNFetchBlobWriteStream'
import RNFetchBlobReadStream from './class/RNFetchBlobReadStream'
import RNFetchBlobFile from './class/RNFetchBlobFile'
const RNFetchBlob: RNFetchBlobNative = NativeModules.RNFetchBlob
const dirs = {
DocumentDir : RNFetchBlob.DocumentDir,
CacheDir : RNFetchBlob.CacheDir,
PictureDir : RNFetchBlob.PictureDir,
MusicDir : RNFetchBlob.MusicDir,
MovieDir : RNFetchBlob.MovieDir,
DownloadDir : RNFetchBlob.DownloadDir,
DCIMDir : RNFetchBlob.DCIMDir,
SDCardDir: RNFetchBlob.SDCardDir, // Depracated
SDCardApplicationDir: RNFetchBlob.SDCardApplicationDir, // Deprecated
MainBundleDir : RNFetchBlob.MainBundleDir,
LibraryDir : RNFetchBlob.LibraryDir
}
function addCode(code: string, error: Error): Error {
error.code = code
return error
}
/**
* Get a file cache session
* @param {string} name Stream ID
* @return {RNFetchBlobSession}
*/
function session(name: string): RNFetchBlobSession {
let s = RNFetchBlobSession.getSession(name)
if (s)
return new RNFetchBlobSession(name)
else {
RNFetchBlobSession.setSession(name, [])
return new RNFetchBlobSession(name, [])
}
}
function asset(path: string): string {
if (Platform.OS === 'ios') {
// path from camera roll
if (/^assets-library\:\/\//.test(path))
return path
}
return 'bundle-assets://' + path
}
function createFile(path: string, data: string, encoding: 'base64' | 'ascii' | 'utf8' = 'utf8'): Promise<string> {
if (encoding.toLowerCase() === 'ascii') {
return Array.isArray(data) ?
RNFetchBlob.createFileASCII(path, data) :
Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')))
}
else {
return RNFetchBlob.createFile(path, data, encoding)
}
}
/**
* Create write stream to a file.
* @param {string} path Target path of file stream.
* @param {string} encoding Encoding of input data.
* @param {boolean} [append] A flag represent if data append to existing ones.
* @return {Promise<RNFetchBlobWriteStream>} A promise resolves a `WriteStream` object.
*/
function writeStream(
path: string,
encoding?: 'utf8' | 'ascii' | 'base64' = 'utf8',
append?: boolean = false,
): Promise<RNFetchBlobWriteStream> {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
return new Promise((resolve, reject) => {
RNFetchBlob.writeStream(path, encoding, append, (errCode, errMsg, streamId: string) => {
if (errMsg) {
const err = new Error(errMsg)
err.code = errCode
reject(err)
}
else
resolve(new RNFetchBlobWriteStream(streamId, encoding))
})
})
}
/**
* Create file stream from file at `path`.
* @param {string} path The file path.
* @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
* @param {boolean} bufferSize Size of stream buffer.
* @param {number} [tick=10] Interval in milliseconds between reading chunks of data
* @return {RNFetchBlobStream} RNFetchBlobStream stream instance.
*/
function readStream(
path: string,
encoding: 'utf8' | 'ascii' | 'base64' = 'utf8',
bufferSize?: number,
tick?: number = 10
): Promise<RNFetchBlobReadStream> {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize, tick))
}
/**
* Create a directory.
* @param {string} path Path of directory to be created
* @return {Promise}
*/
function mkdir(path: string): Promise {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
return RNFetchBlob.mkdir(path)
}
/**
* Returns the path for the app group.
* @param {string} groupName Name of app group
* @return {Promise}
*/
function pathForAppGroup(groupName: string): Promise {
return RNFetchBlob.pathForAppGroup(groupName)
}
/**
* Returns the path for the app group synchronous.
* @param {string} groupName Name of app group
* @return {string} Path of App Group dir
*/
function syncPathAppGroup(groupName: string): string {
if (Platform.OS === 'ios') {
return RNFetchBlob.syncPathAppGroup(groupName);
} else {
return '';
}
}
/**
* Wrapper method of readStream.
* @param {string} path Path of the file.
* @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
* @return {Promise<Array<number> | string>}
*/
function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
return RNFetchBlob.readFile(path, encoding)
}
/**
* Write data to file.
* @param {string} path Path of the file.
* @param {string | number[]} data Data to write to the file.
* @param {string} encoding Encoding of data (Optional).
* @return {Promise}
*/
function writeFile(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
if (encoding.toLocaleLowerCase() === 'ascii') {
if (!Array.isArray(data)) {
return Promise.reject(addCode('EINVAL', new TypeError('"data" must be an Array when encoding is "ascii"')))
}
else
return RNFetchBlob.writeFileArray(path, data, false)
}
else {
if (typeof data !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)))
}
else
return RNFetchBlob.writeFile(path, encoding, data, false)
}
}
function appendFile(path: string, data: string | Array<number>, encoding?: string = 'utf8'): Promise<number> {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
if (encoding.toLocaleLowerCase() === 'ascii') {
if (!Array.isArray(data)) {
return Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')))
}
else
return RNFetchBlob.writeFileArray(path, data, true)
}
else {
if (typeof data !== 'string') {
return Promise.reject(addCode('EINVAL'), new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`))
}
else
return RNFetchBlob.writeFile(path, encoding, data, true)
}
}
/**
* Show statistic data of a path.
* @param {string} path Target path
* @return {RNFetchBlobFile}
*/
function stat(path: string): Promise<RNFetchBlobFile> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
RNFetchBlob.stat(path, (err, stat) => {
if (err)
reject(new Error(err))
else {
if (stat) {
stat.size = parseInt(stat.size)
stat.lastModified = parseInt(stat.lastModified)
}
resolve(stat)
}
})
})
}
/**
* Android only method, request media scanner to scan the file.
* @param {Array<Object<string, string>>} pairs Array contains Key value pairs with key `path` and `mime`.
* @return {Promise}
*/
function scanFile(pairs: any): Promise {
return new Promise((resolve, reject) => {
if (pairs === undefined) {
return reject(addCode('EINVAL', new TypeError('Missing argument')))
}
RNFetchBlob.scanFile(pairs, (err) => {
if (err)
reject(addCode('EUNSPECIFIED', new Error(err)))
else
resolve()
})
})
}
function hash(path: string, algorithm: string): Promise<string> {
if (typeof path !== 'string' || typeof algorithm !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "algorithm"')))
}
return RNFetchBlob.hash(path, algorithm)
}
function cp(path: string, dest: string): Promise<boolean> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string' || typeof dest !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')))
}
RNFetchBlob.cp(path, dest, (err, res) => {
if (err)
reject(addCode('EUNSPECIFIED', new Error(err)))
else
resolve(res)
})
})
}
function mv(path: string, dest: string): Promise<boolean> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string' || typeof dest !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')))
}
RNFetchBlob.mv(path, dest, (err, res) => {
if (err)
reject(addCode('EUNSPECIFIED', new Error(err)))
else
resolve(res)
})
})
}
function lstat(path: string): Promise<Array<RNFetchBlobFile>> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
RNFetchBlob.lstat(path, (err, stat) => {
if (err)
reject(addCode('EUNSPECIFIED', new Error(err)))
else
resolve(stat)
})
})
}
function ls(path: string): Promise<Array<String>> {
if (typeof path !== 'string') {
return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
return RNFetchBlob.ls(path)
}
/**
* Remove file at path.
* @param {string} path:string Path of target file.
* @return {Promise}
*/
function unlink(path: string): Promise {
return new Promise((resolve, reject) => {
if (typeof path !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
RNFetchBlob.unlink(path, (err) => {
if (err) {
reject(addCode('EUNSPECIFIED', new Error(err)))
}
else
resolve()
})
})
}
/**
* Check if file exists and if it is a folder.
* @param {string} path Path to check
* @return {Promise<boolean>}
*/
function exists(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
try {
RNFetchBlob.exists(path, (exist) => {
resolve(exist)
})
}catch (err){
reject(addCode('EUNSPECIFIED', new Error(err)))
}
})
}
function slice(src: string, dest: string, start: number, end: number): Promise {
if (typeof src !== 'string' || typeof dest !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "src" and/or "destination"')))
}
let p = Promise.resolve()
let size = 0
function normalize(num, size) {
if (num < 0)
return Math.max(0, size + num)
if (!num && num !== 0)
return size
return num
}
if (start < 0 || end < 0 || !start || !end) {
p = p.then(() => stat(src))
.then((stat) => {
size = Math.floor(stat.size)
start = normalize(start || 0, size)
end = normalize(end, size)
})
}
return p.then(() => RNFetchBlob.slice(src, dest, start, end))
}
function isDir(path: string): Promise<bool> {
return new Promise((resolve, reject) => {
if (typeof path !== 'string') {
return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
}
try {
RNFetchBlob.exists(path, (exist, isDir) => {
resolve(isDir)
})
}catch (err){
reject(addCode('EUNSPECIFIED', new Error(err)))
}
})
}
function df(): Promise<{ free: number, total: number }> {
return new Promise((resolve, reject) => {
RNFetchBlob.df((err, stat) => {
if (err)
reject(addCode('EUNSPECIFIED', new Error(err)))
else
resolve(stat)
})
})
}
export default {
RNFetchBlobSession,
unlink,
mkdir,
session,
ls,
readStream,
mv,
cp,
writeStream,
writeFile,
appendFile,
pathForAppGroup,
syncPathAppGroup,
readFile,
hash,
exists,
createFile,
isDir,
stat,
lstat,
scanFile,
dirs,
slice,
asset,
df
}