react-native-blob-util 0.22.0 → 0.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.eslintrc.js +56 -56
  2. package/LICENSE +21 -21
  3. package/Migration.md +41 -41
  4. package/NuGet.config +11 -11
  5. package/README.md +1109 -1104
  6. package/android/gradle.properties +1 -1
  7. package/android/src/main/AndroidManifest.xml +37 -37
  8. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +57 -57
  9. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConst.java +20 -20
  10. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +9 -9
  11. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilImpl.java +431 -431
  12. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilProgressConfig.java +39 -39
  13. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +1014 -1014
  14. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilDefaultResp.java +98 -98
  15. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +166 -166
  16. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -19
  17. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileProvider.java +4 -4
  18. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -70
  19. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/PathResolver.java +223 -223
  20. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/Tls12SocketFactory.java +68 -68
  21. package/android/src/main/res/values/strings.xml +3 -3
  22. package/android/src/main/res/xml/provider_paths.xml +12 -12
  23. package/android.js +58 -58
  24. package/class/ReactNativeBlobUtilCanceledFetchError.js +9 -9
  25. package/class/ReactNativeBlobUtilFile.js +5 -5
  26. package/class/ReactNativeBlobUtilReadStream.js +84 -84
  27. package/class/ReactNativeBlobUtilSession.js +68 -68
  28. package/class/ReactNativeBlobUtilWriteStream.js +50 -50
  29. package/class/StatefulPromise.js +7 -7
  30. package/codegenSpecs/NativeBlobUtils.js +86 -86
  31. package/components/Fetch.onPress.js +10 -10
  32. package/components/Fetch.when.js +15 -15
  33. package/fetch.js +350 -350
  34. package/fs.js +480 -480
  35. package/index.js +64 -64
  36. package/index.js.flow +192 -192
  37. package/index.web.js +1 -1
  38. package/ios/PrivacyInfo.xcprivacy +28 -28
  39. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h +55 -55
  40. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm +992 -992
  41. package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +405 -405
  42. package/ios/ReactNativeBlobUtilConst.h +58 -58
  43. package/ios/ReactNativeBlobUtilConst.mm +47 -47
  44. package/ios/ReactNativeBlobUtilFS.h +108 -108
  45. package/ios/ReactNativeBlobUtilFS.mm +1064 -1064
  46. package/ios/ReactNativeBlobUtilFileTransformer.h +23 -23
  47. package/ios/ReactNativeBlobUtilFileTransformer.mm +20 -20
  48. package/ios/ReactNativeBlobUtilNetwork.h +49 -49
  49. package/ios/ReactNativeBlobUtilNetwork.mm +159 -159
  50. package/ios/ReactNativeBlobUtilProgress.h +32 -32
  51. package/ios/ReactNativeBlobUtilProgress.mm +57 -57
  52. package/ios/ReactNativeBlobUtilReqBuilder.h +37 -37
  53. package/ios/ReactNativeBlobUtilReqBuilder.mm +292 -292
  54. package/ios/ReactNativeBlobUtilRequest.h +48 -48
  55. package/ios/ReactNativeBlobUtilRequest.mm +584 -584
  56. package/ios.js +61 -61
  57. package/json-stream.js +42 -42
  58. package/lib/oboe-browser.js +2703 -2703
  59. package/mediacollection.js +36 -36
  60. package/package.json +68 -68
  61. package/polyfill/Blob.js +362 -362
  62. package/polyfill/Event.js +11 -11
  63. package/polyfill/EventTarget.js +78 -78
  64. package/polyfill/Fetch.js +219 -219
  65. package/polyfill/File.js +27 -27
  66. package/polyfill/FileReader.js +91 -91
  67. package/polyfill/ProgressEvent.js +32 -32
  68. package/polyfill/XMLHttpRequest.js +466 -466
  69. package/polyfill/XMLHttpRequestEventTarget.js +126 -126
  70. package/polyfill/index.js +11 -11
  71. package/react-native-blob-util.podspec +48 -48
  72. package/types.js +69 -69
  73. package/utils/log.js +40 -40
  74. package/utils/unicode.js +7 -7
  75. package/utils/uri.js +33 -33
  76. package/utils/uuid.js +4 -4
  77. package/windows/ExperimentalFeatures.props +33 -33
  78. package/windows/README.md +2 -2
  79. package/windows/ReactNativeBlobUtil/PropertySheet.props +15 -15
  80. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.cpp +1836 -1836
  81. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.def +3 -3
  82. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.h +372 -372
  83. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj +139 -139
  84. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj.filters +43 -43
  85. package/windows/ReactNativeBlobUtil/ReactPackageProvider.cpp +20 -20
  86. package/windows/ReactNativeBlobUtil/ReactPackageProvider.h +24 -24
  87. package/windows/ReactNativeBlobUtil/ReactPackageProvider.idl +9 -9
  88. package/windows/ReactNativeBlobUtil/codegen/.clang-format +1 -1
  89. package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsDataTypes.g.h +42 -42
  90. package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsSpec.g.h +353 -353
  91. package/windows/ReactNativeBlobUtil/packages.config +3 -3
  92. package/windows/ReactNativeBlobUtil/packages.lock.json +59 -59
  93. package/windows/ReactNativeBlobUtil/pch.cpp +1 -1
  94. package/windows/ReactNativeBlobUtil/pch.h +30 -30
  95. package/windows/ReactNativeBlobUtil/resource.h +5 -5
  96. package/windows/ReactNativeBlobUtil/targetver.h +8 -8
  97. package/windows/ReactNativeBlobUtil.sln +43 -43
package/fs.js CHANGED
@@ -1,480 +1,480 @@
1
- // Copyright 2016 wkh237@github. All rights reserved.
2
- // Use of this source code is governed by a MIT-style license that can be
3
- // found in the LICENSE file.
4
-
5
- // import type {ReactNativeBlobUtilConfig, ReactNativeBlobUtilNative, ReactNativeBlobUtilStream} from './types'
6
-
7
- import {Platform} from 'react-native';
8
- import ReactNativeBlobUtilSession from './class/ReactNativeBlobUtilSession';
9
- import ReactNativeBlobUtilWriteStream from './class/ReactNativeBlobUtilWriteStream';
10
- import ReactNativeBlobUtilReadStream from './class/ReactNativeBlobUtilReadStream';
11
- import ReactNativeBlobUtilFile from './class/ReactNativeBlobUtilFile';
12
- import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';
13
-
14
- const constants = ReactNativeBlobUtil.getConstants();
15
-
16
- const dirs = {
17
- DocumentDir: constants.DocumentDir,
18
- CacheDir: constants.CacheDir,
19
- PictureDir: constants.PictureDir,
20
- MusicDir: constants.MusicDir,
21
- MovieDir: constants.MovieDir,
22
- DownloadDir: constants.DownloadDir,
23
- DCIMDir: constants.DCIMDir,
24
- SDCardDir: constants.SDCardDir, // Depracated
25
- SDCardApplicationDir: constants.SDCardApplicationDir, // Deprecated
26
- MainBundleDir: constants.MainBundleDir,
27
- LibraryDir: constants.LibraryDir,
28
- ApplicationSupportDir: constants.ApplicationSupportDir,
29
-
30
- LegacyPictureDir: constants.LegacyPictureDir,
31
- LegacyMusicDir: constants.LegacyMusicDir,
32
- LegacyMovieDir: constants.LegacyMovieDir,
33
- LegacyDownloadDir: constants.LegacyDownloadDir,
34
- LegacyDCIMDir: constants.LegacyDCIMDir,
35
- LegacySDCardDir: constants.LegacySDCardDir, // Depracated
36
- };
37
-
38
- function addCode(code: string, error: Error): Error {
39
- error.code = code;
40
- return error;
41
- }
42
-
43
- /**
44
- * Get a file cache session
45
- * @param {string} name Stream ID
46
- * @return {ReactNativeBlobUtilSession}
47
- */
48
- function session(name: string): ReactNativeBlobUtilSession {
49
- let s = ReactNativeBlobUtilSession.getSession(name);
50
- if (s)
51
- return new ReactNativeBlobUtilSession(name);
52
- else {
53
- ReactNativeBlobUtilSession.setSession(name, []);
54
- return new ReactNativeBlobUtilSession(name, []);
55
- }
56
- }
57
-
58
- function asset(path: string): string {
59
- if (Platform.OS === 'ios') {
60
- // path from camera roll
61
- if (/^assets-library\:\/\//.test(path))
62
- return path;
63
- }
64
- return 'bundle-assets://' + path;
65
- }
66
-
67
- function createFile(path: string, data: string, encoding: 'base64' | 'ascii' | 'utf8' = 'utf8'): Promise<string> {
68
- if (encoding.toLowerCase() === 'ascii') {
69
- return Array.isArray(data) ?
70
- ReactNativeBlobUtil.createFileASCII(path, data) :
71
- Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')));
72
- }
73
- else {
74
- return ReactNativeBlobUtil.createFile(path, data, encoding);
75
- }
76
- }
77
-
78
- /**
79
- * Create write stream to a file.
80
- * @param {string} path Target path of file stream.
81
- * @param {string} encoding Encoding of input data.
82
- * @param {boolean} [append] A flag represent if data append to existing ones.
83
- * @return {Promise<ReactNativeBlobUtilWriteStream>} A promise resolves a `WriteStream` object.
84
- */
85
- function writeStream(
86
- path: string,
87
- encoding?: 'utf8' | 'ascii' | 'base64' = 'utf8',
88
- append?: boolean = false,
89
- ): Promise<ReactNativeBlobUtilWriteStream> {
90
- if (typeof path !== 'string') {
91
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
92
- }
93
- return new Promise((resolve, reject) => {
94
- ReactNativeBlobUtil.writeStream(path, encoding, append, (errCode, errMsg, streamId: string) => {
95
- if (errMsg) {
96
- const err = new Error(errMsg);
97
- err.code = errCode;
98
- reject(err);
99
- }
100
- else
101
- resolve(new ReactNativeBlobUtilWriteStream(streamId, encoding));
102
- });
103
- });
104
- }
105
-
106
- /**
107
- * Create file stream from file at `path`.
108
- * @param {string} path The file path.
109
- * @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
110
- * @param {boolean} bufferSize Size of stream buffer.
111
- * @param {number} [tick=10] Interval in milliseconds between reading chunks of data
112
- * @return {ReactNativeBlobUtilStream} ReactNativeBlobUtilStream stream instance.
113
- */
114
- function readStream(
115
- path: string,
116
- encoding: 'utf8' | 'ascii' | 'base64' = 'utf8',
117
- bufferSize?: number,
118
- tick?: number = 10
119
- ): Promise<ReactNativeBlobUtilReadStream> {
120
- if (typeof path !== 'string') {
121
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
122
- }
123
- return Promise.resolve(new ReactNativeBlobUtilReadStream(path, encoding, bufferSize, tick));
124
- }
125
-
126
- /**
127
- * Create a directory.
128
- * @param {string} path Path of directory to be created
129
- * @return {Promise}
130
- */
131
- function mkdir(path: string): Promise {
132
- if (typeof path !== 'string') {
133
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
134
- }
135
- return ReactNativeBlobUtil.mkdir(path);
136
- }
137
-
138
- /**
139
- * Returns the path for the app group.
140
- * @param {string} groupName Name of app group
141
- * @return {Promise}
142
- */
143
- function pathForAppGroup(groupName: string): Promise {
144
- return ReactNativeBlobUtil.pathForAppGroup(groupName);
145
- }
146
-
147
- /**
148
- * Returns the path for the app group synchronous.
149
- * @param {string} groupName Name of app group
150
- * @return {string} Path of App Group dir
151
- */
152
- function syncPathAppGroup(groupName: string): string {
153
- if (Platform.OS === 'ios') {
154
- return ReactNativeBlobUtil.syncPathAppGroup(groupName);
155
- }
156
- else {
157
- return '';
158
- }
159
- }
160
-
161
- /**
162
- * Wrapper method of readStream.
163
- * @param {string} path Path of the file.
164
- * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
165
- * @return {Promise<Array<number> | string>}
166
- */
167
- function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
168
- if (typeof path !== 'string') {
169
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
170
- }
171
- return ReactNativeBlobUtil.readFile(path, encoding, false);
172
- }
173
-
174
- /**
175
- * Reads the file, then transforms it before returning the content
176
- * @param {string} path Path of the file.
177
- * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
178
- * @return {Promise<Array<number> | string>}
179
- */
180
- function readFileWithTransform(path: string, encoding: string = 'utf8'): Promise<any> {
181
- if (typeof path !== 'string') {
182
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
183
- }
184
- return ReactNativeBlobUtil.readFile(path, encoding, true);
185
- }
186
-
187
- /**
188
- * Write data to file.
189
- * @param {string} path Path of the file.
190
- * @param {string | number[]} data Data to write to the file.
191
- * @param {string} encoding Encoding of data (Optional).
192
- * @return {Promise}
193
- */
194
- function writeFile(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
195
- if (typeof path !== 'string') {
196
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
197
- }
198
- if (encoding.toLocaleLowerCase() === 'ascii') {
199
- if (!Array.isArray(data)) {
200
- return Promise.reject(addCode('EINVAL', new TypeError('"data" must be an Array when encoding is "ascii"')));
201
- }
202
- else
203
- return ReactNativeBlobUtil.writeFileArray(path, data, false);
204
- }
205
- else {
206
- if (typeof data !== 'string') {
207
- return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)));
208
- }
209
- else
210
- return ReactNativeBlobUtil.writeFile(path, encoding, data, false, false);
211
- }
212
- }
213
-
214
- /**
215
- * Transforms the data and then writes to the file.
216
- * @param {string} path Path of the file.
217
- * @param {string | number[]} data Data to write to the file.
218
- * @param {string} encoding Encoding of data (Optional).
219
- * @return {Promise}
220
- */
221
- function writeFileWithTransform(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
222
- if (typeof path !== 'string') {
223
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
224
- }
225
- if (encoding.toLocaleLowerCase() === 'ascii') {
226
- return Promise.reject(addCode('EINVAL', new TypeError('ascii is not supported for converted files')))
227
- }
228
- else {
229
- if (typeof data !== 'string') {
230
- return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)))
231
- }
232
-
233
- else
234
- return ReactNativeBlobUtil.writeFile(path, encoding, data, true, false)
235
- }
236
- }
237
-
238
- function appendFile(path: string, data: string | Array<number>, encoding?: string = 'utf8'): Promise<number> {
239
- if (typeof path !== 'string') {
240
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
241
- }
242
- if (encoding.toLocaleLowerCase() === 'ascii') {
243
- if (!Array.isArray(data)) {
244
- return Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')));
245
- }
246
- else
247
- return ReactNativeBlobUtil.writeFileArray(path, data, true);
248
- }
249
- else {
250
- if (typeof data !== 'string') {
251
- return Promise.reject(addCode('EINVAL'), new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`));
252
- }
253
- else
254
- return ReactNativeBlobUtil.writeFile(path, encoding, data, false, true);
255
- }
256
- }
257
-
258
- /**
259
- * Show statistic data of a path.
260
- * @param {string} path Target path
261
- * @return {ReactNativeBlobUtilFile}
262
- */
263
- function stat(path: string): Promise<ReactNativeBlobUtilFile> {
264
- return new Promise((resolve, reject) => {
265
- if (typeof path !== 'string') {
266
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
267
- }
268
- ReactNativeBlobUtil.stat(path, (err, stat) => {
269
- if (err)
270
- reject(new Error(err));
271
- else {
272
- if (stat) {
273
- stat.size = parseInt(stat.size);
274
- stat.lastModified = parseInt(stat.lastModified);
275
- }
276
- resolve(stat);
277
- }
278
- });
279
- });
280
- }
281
-
282
- /**
283
- * Android only method, request media scanner to scan the file.
284
- * @param {Array<Object<string, string>>} pairs Array contains Key value pairs with key `path` and `mime`.
285
- * @return {Promise}
286
- */
287
- function scanFile(pairs: any): Promise {
288
- return new Promise((resolve, reject) => {
289
- if (pairs === undefined) {
290
- return reject(addCode('EINVAL', new TypeError('Missing argument')));
291
- }
292
- ReactNativeBlobUtil.scanFile(pairs, (err) => {
293
- if (err)
294
- reject(addCode('EUNSPECIFIED', new Error(err)));
295
- else
296
- resolve();
297
- });
298
- });
299
- }
300
-
301
- function hash(path: string, algorithm: string): Promise<string> {
302
- if (typeof path !== 'string' || typeof algorithm !== 'string') {
303
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "algorithm"')));
304
- }
305
- return ReactNativeBlobUtil.hash(path, algorithm);
306
- }
307
-
308
- function cp(path: string, dest: string): Promise<boolean> {
309
- return new Promise((resolve, reject) => {
310
- if (typeof path !== 'string' || typeof dest !== 'string') {
311
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')));
312
- }
313
- ReactNativeBlobUtil.cp(path, dest, (err, res) => {
314
- if (err)
315
- reject(addCode('EUNSPECIFIED', new Error(err)));
316
- else
317
- resolve(res);
318
- });
319
- });
320
- }
321
-
322
- function mv(path: string, dest: string): Promise<boolean> {
323
- return new Promise((resolve, reject) => {
324
- if (typeof path !== 'string' || typeof dest !== 'string') {
325
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')));
326
- }
327
- ReactNativeBlobUtil.mv(path, dest, (err, res) => {
328
- if (err)
329
- reject(addCode('EUNSPECIFIED', new Error(err)));
330
- else
331
- resolve(res);
332
- });
333
- });
334
- }
335
-
336
- function lstat(path: string): Promise<Array<ReactNativeBlobUtilFile>> {
337
- return new Promise((resolve, reject) => {
338
- if (typeof path !== 'string') {
339
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
340
- }
341
- ReactNativeBlobUtil.lstat(path, (err, stat) => {
342
- if (err)
343
- reject(addCode('EUNSPECIFIED', new Error(err)));
344
- else
345
- resolve(stat);
346
- });
347
- });
348
- }
349
-
350
- function ls(path: string): Promise<Array<String>> {
351
- if (typeof path !== 'string') {
352
- return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
353
- }
354
- return ReactNativeBlobUtil.ls(path);
355
- }
356
-
357
- /**
358
- * Remove file at path.
359
- * @param {string} path:string Path of target file.
360
- * @return {Promise}
361
- */
362
- function unlink(path: string): Promise {
363
- return new Promise((resolve, reject) => {
364
- if (typeof path !== 'string') {
365
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
366
- }
367
- ReactNativeBlobUtil.unlink(path, (err) => {
368
- if (err) {
369
- reject(addCode('EUNSPECIFIED', new Error(err)));
370
- }
371
- else
372
- resolve();
373
- });
374
- });
375
- }
376
-
377
- /**
378
- * Check if file exists and if it is a folder.
379
- * @param {string} path Path to check
380
- * @return {Promise<boolean>}
381
- */
382
- function exists(path: string): Promise<boolean> {
383
- return new Promise((resolve, reject) => {
384
- if (typeof path !== 'string') {
385
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
386
- }
387
- try {
388
- ReactNativeBlobUtil.exists(path, (exist) => {
389
- resolve(exist);
390
- });
391
- } catch (err) {
392
- reject(addCode('EUNSPECIFIED', new Error(err)));
393
- }
394
- });
395
-
396
- }
397
-
398
- function slice(src: string, dest: string, start: number, end: number): Promise {
399
- if (typeof src !== 'string' || typeof dest !== 'string') {
400
- return reject(addCode('EINVAL', new TypeError('Missing argument "src" and/or "destination"')));
401
- }
402
-
403
- let p = Promise.resolve();
404
- let size = 0;
405
-
406
- function normalize(num, size) {
407
- if (num < 0)
408
- return Math.max(0, size + num);
409
- if (!num && num !== 0)
410
- return size;
411
- return num;
412
- }
413
-
414
- if (start < 0 || end < 0 || !start || !end) {
415
- p = p.then(() => stat(src))
416
- .then((stat) => {
417
- size = Math.floor(stat.size);
418
- start = normalize(start || 0, size);
419
- end = normalize(end, size);
420
- });
421
- }
422
- return p.then(() => ReactNativeBlobUtil.slice(src, dest, start, end));
423
- }
424
-
425
- function isDir(path: string): Promise<bool> {
426
- return new Promise((resolve, reject) => {
427
- if (typeof path !== 'string') {
428
- return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
429
- }
430
- try {
431
- ReactNativeBlobUtil.exists(path, (exist, isDir) => {
432
- resolve(isDir);
433
- });
434
- } catch (err) {
435
- reject(addCode('EUNSPECIFIED', new Error(err)));
436
- }
437
- });
438
-
439
- }
440
-
441
- function df(): Promise<{ free: number, total: number }> {
442
- return new Promise((resolve, reject) => {
443
- ReactNativeBlobUtil.df((err, stat) => {
444
- if (err)
445
- reject(addCode('EUNSPECIFIED', new Error(err)));
446
- else
447
- resolve(stat);
448
- });
449
- });
450
- }
451
-
452
- export default {
453
- ReactNativeBlobUtilSession,
454
- unlink,
455
- mkdir,
456
- session,
457
- ls,
458
- readStream,
459
- mv,
460
- cp,
461
- writeStream,
462
- writeFile,
463
- writeFileWithTransform,
464
- readFileWithTransform,
465
- appendFile,
466
- pathForAppGroup,
467
- syncPathAppGroup,
468
- readFile,
469
- hash,
470
- exists,
471
- createFile,
472
- isDir,
473
- stat,
474
- lstat,
475
- scanFile,
476
- dirs,
477
- slice,
478
- asset,
479
- df
480
- };
1
+ // Copyright 2016 wkh237@github. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // import type {ReactNativeBlobUtilConfig, ReactNativeBlobUtilNative, ReactNativeBlobUtilStream} from './types'
6
+
7
+ import {Platform} from 'react-native';
8
+ import ReactNativeBlobUtilSession from './class/ReactNativeBlobUtilSession';
9
+ import ReactNativeBlobUtilWriteStream from './class/ReactNativeBlobUtilWriteStream';
10
+ import ReactNativeBlobUtilReadStream from './class/ReactNativeBlobUtilReadStream';
11
+ import ReactNativeBlobUtilFile from './class/ReactNativeBlobUtilFile';
12
+ import ReactNativeBlobUtil from './codegenSpecs/NativeBlobUtils';
13
+
14
+ const constants = ReactNativeBlobUtil.getConstants();
15
+
16
+ const dirs = {
17
+ DocumentDir: constants.DocumentDir,
18
+ CacheDir: constants.CacheDir,
19
+ PictureDir: constants.PictureDir,
20
+ MusicDir: constants.MusicDir,
21
+ MovieDir: constants.MovieDir,
22
+ DownloadDir: constants.DownloadDir,
23
+ DCIMDir: constants.DCIMDir,
24
+ SDCardDir: constants.SDCardDir, // Depracated
25
+ SDCardApplicationDir: constants.SDCardApplicationDir, // Deprecated
26
+ MainBundleDir: constants.MainBundleDir,
27
+ LibraryDir: constants.LibraryDir,
28
+ ApplicationSupportDir: constants.ApplicationSupportDir,
29
+
30
+ LegacyPictureDir: constants.LegacyPictureDir,
31
+ LegacyMusicDir: constants.LegacyMusicDir,
32
+ LegacyMovieDir: constants.LegacyMovieDir,
33
+ LegacyDownloadDir: constants.LegacyDownloadDir,
34
+ LegacyDCIMDir: constants.LegacyDCIMDir,
35
+ LegacySDCardDir: constants.LegacySDCardDir, // Depracated
36
+ };
37
+
38
+ function addCode(code: string, error: Error): Error {
39
+ error.code = code;
40
+ return error;
41
+ }
42
+
43
+ /**
44
+ * Get a file cache session
45
+ * @param {string} name Stream ID
46
+ * @return {ReactNativeBlobUtilSession}
47
+ */
48
+ function session(name: string): ReactNativeBlobUtilSession {
49
+ let s = ReactNativeBlobUtilSession.getSession(name);
50
+ if (s)
51
+ return new ReactNativeBlobUtilSession(name);
52
+ else {
53
+ ReactNativeBlobUtilSession.setSession(name, []);
54
+ return new ReactNativeBlobUtilSession(name, []);
55
+ }
56
+ }
57
+
58
+ function asset(path: string): string {
59
+ if (Platform.OS === 'ios') {
60
+ // path from camera roll
61
+ if (/^assets-library\:\/\//.test(path))
62
+ return path;
63
+ }
64
+ return 'bundle-assets://' + path;
65
+ }
66
+
67
+ function createFile(path: string, data: string, encoding: 'base64' | 'ascii' | 'utf8' = 'utf8'): Promise<string> {
68
+ if (encoding.toLowerCase() === 'ascii') {
69
+ return Array.isArray(data) ?
70
+ ReactNativeBlobUtil.createFileASCII(path, data) :
71
+ Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')));
72
+ }
73
+ else {
74
+ return ReactNativeBlobUtil.createFile(path, data, encoding);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Create write stream to a file.
80
+ * @param {string} path Target path of file stream.
81
+ * @param {string} encoding Encoding of input data.
82
+ * @param {boolean} [append] A flag represent if data append to existing ones.
83
+ * @return {Promise<ReactNativeBlobUtilWriteStream>} A promise resolves a `WriteStream` object.
84
+ */
85
+ function writeStream(
86
+ path: string,
87
+ encoding?: 'utf8' | 'ascii' | 'base64' = 'utf8',
88
+ append?: boolean = false,
89
+ ): Promise<ReactNativeBlobUtilWriteStream> {
90
+ if (typeof path !== 'string') {
91
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
92
+ }
93
+ return new Promise((resolve, reject) => {
94
+ ReactNativeBlobUtil.writeStream(path, encoding, append, (errCode, errMsg, streamId: string) => {
95
+ if (errMsg) {
96
+ const err = new Error(errMsg);
97
+ err.code = errCode;
98
+ reject(err);
99
+ }
100
+ else
101
+ resolve(new ReactNativeBlobUtilWriteStream(streamId, encoding));
102
+ });
103
+ });
104
+ }
105
+
106
+ /**
107
+ * Create file stream from file at `path`.
108
+ * @param {string} path The file path.
109
+ * @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
110
+ * @param {boolean} bufferSize Size of stream buffer.
111
+ * @param {number} [tick=10] Interval in milliseconds between reading chunks of data
112
+ * @return {ReactNativeBlobUtilStream} ReactNativeBlobUtilStream stream instance.
113
+ */
114
+ function readStream(
115
+ path: string,
116
+ encoding: 'utf8' | 'ascii' | 'base64' = 'utf8',
117
+ bufferSize?: number,
118
+ tick?: number = 10
119
+ ): Promise<ReactNativeBlobUtilReadStream> {
120
+ if (typeof path !== 'string') {
121
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
122
+ }
123
+ return Promise.resolve(new ReactNativeBlobUtilReadStream(path, encoding, bufferSize, tick));
124
+ }
125
+
126
+ /**
127
+ * Create a directory.
128
+ * @param {string} path Path of directory to be created
129
+ * @return {Promise}
130
+ */
131
+ function mkdir(path: string): Promise {
132
+ if (typeof path !== 'string') {
133
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
134
+ }
135
+ return ReactNativeBlobUtil.mkdir(path);
136
+ }
137
+
138
+ /**
139
+ * Returns the path for the app group.
140
+ * @param {string} groupName Name of app group
141
+ * @return {Promise}
142
+ */
143
+ function pathForAppGroup(groupName: string): Promise {
144
+ return ReactNativeBlobUtil.pathForAppGroup(groupName);
145
+ }
146
+
147
+ /**
148
+ * Returns the path for the app group synchronous.
149
+ * @param {string} groupName Name of app group
150
+ * @return {string} Path of App Group dir
151
+ */
152
+ function syncPathAppGroup(groupName: string): string {
153
+ if (Platform.OS === 'ios') {
154
+ return ReactNativeBlobUtil.syncPathAppGroup(groupName);
155
+ }
156
+ else {
157
+ return '';
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Wrapper method of readStream.
163
+ * @param {string} path Path of the file.
164
+ * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
165
+ * @return {Promise<Array<number> | string>}
166
+ */
167
+ function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
168
+ if (typeof path !== 'string') {
169
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
170
+ }
171
+ return ReactNativeBlobUtil.readFile(path, encoding, false);
172
+ }
173
+
174
+ /**
175
+ * Reads the file, then transforms it before returning the content
176
+ * @param {string} path Path of the file.
177
+ * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
178
+ * @return {Promise<Array<number> | string>}
179
+ */
180
+ function readFileWithTransform(path: string, encoding: string = 'utf8'): Promise<any> {
181
+ if (typeof path !== 'string') {
182
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
183
+ }
184
+ return ReactNativeBlobUtil.readFile(path, encoding, true);
185
+ }
186
+
187
+ /**
188
+ * Write data to file.
189
+ * @param {string} path Path of the file.
190
+ * @param {string | number[]} data Data to write to the file.
191
+ * @param {string} encoding Encoding of data (Optional).
192
+ * @return {Promise}
193
+ */
194
+ function writeFile(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
195
+ if (typeof path !== 'string') {
196
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
197
+ }
198
+ if (encoding.toLocaleLowerCase() === 'ascii') {
199
+ if (!Array.isArray(data)) {
200
+ return Promise.reject(addCode('EINVAL', new TypeError('"data" must be an Array when encoding is "ascii"')));
201
+ }
202
+ else
203
+ return ReactNativeBlobUtil.writeFileArray(path, data, false);
204
+ }
205
+ else {
206
+ if (typeof data !== 'string') {
207
+ return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)));
208
+ }
209
+ else
210
+ return ReactNativeBlobUtil.writeFile(path, encoding, data, false, false);
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Transforms the data and then writes to the file.
216
+ * @param {string} path Path of the file.
217
+ * @param {string | number[]} data Data to write to the file.
218
+ * @param {string} encoding Encoding of data (Optional).
219
+ * @return {Promise}
220
+ */
221
+ function writeFileWithTransform(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
222
+ if (typeof path !== 'string') {
223
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
224
+ }
225
+ if (encoding.toLocaleLowerCase() === 'ascii') {
226
+ return Promise.reject(addCode('EINVAL', new TypeError('ascii is not supported for converted files')))
227
+ }
228
+ else {
229
+ if (typeof data !== 'string') {
230
+ return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)))
231
+ }
232
+
233
+ else
234
+ return ReactNativeBlobUtil.writeFile(path, encoding, data, true, false)
235
+ }
236
+ }
237
+
238
+ function appendFile(path: string, data: string | Array<number>, encoding?: string = 'utf8'): Promise<number> {
239
+ if (typeof path !== 'string') {
240
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
241
+ }
242
+ if (encoding.toLocaleLowerCase() === 'ascii') {
243
+ if (!Array.isArray(data)) {
244
+ return Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')));
245
+ }
246
+ else
247
+ return ReactNativeBlobUtil.writeFileArray(path, data, true);
248
+ }
249
+ else {
250
+ if (typeof data !== 'string') {
251
+ return Promise.reject(addCode('EINVAL'), new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`));
252
+ }
253
+ else
254
+ return ReactNativeBlobUtil.writeFile(path, encoding, data, false, true);
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Show statistic data of a path.
260
+ * @param {string} path Target path
261
+ * @return {ReactNativeBlobUtilFile}
262
+ */
263
+ function stat(path: string): Promise<ReactNativeBlobUtilFile> {
264
+ return new Promise((resolve, reject) => {
265
+ if (typeof path !== 'string') {
266
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
267
+ }
268
+ ReactNativeBlobUtil.stat(path, (err, stat) => {
269
+ if (err)
270
+ reject(new Error(err));
271
+ else {
272
+ if (stat) {
273
+ stat.size = parseInt(stat.size);
274
+ stat.lastModified = parseInt(stat.lastModified);
275
+ }
276
+ resolve(stat);
277
+ }
278
+ });
279
+ });
280
+ }
281
+
282
+ /**
283
+ * Android only method, request media scanner to scan the file.
284
+ * @param {Array<Object<string, string>>} pairs Array contains Key value pairs with key `path` and `mime`.
285
+ * @return {Promise}
286
+ */
287
+ function scanFile(pairs: any): Promise {
288
+ return new Promise((resolve, reject) => {
289
+ if (pairs === undefined) {
290
+ return reject(addCode('EINVAL', new TypeError('Missing argument')));
291
+ }
292
+ ReactNativeBlobUtil.scanFile(pairs, (err) => {
293
+ if (err)
294
+ reject(addCode('EUNSPECIFIED', new Error(err)));
295
+ else
296
+ resolve();
297
+ });
298
+ });
299
+ }
300
+
301
+ function hash(path: string, algorithm: string): Promise<string> {
302
+ if (typeof path !== 'string' || typeof algorithm !== 'string') {
303
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "algorithm"')));
304
+ }
305
+ return ReactNativeBlobUtil.hash(path, algorithm);
306
+ }
307
+
308
+ function cp(path: string, dest: string): Promise<boolean> {
309
+ return new Promise((resolve, reject) => {
310
+ if (typeof path !== 'string' || typeof dest !== 'string') {
311
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')));
312
+ }
313
+ ReactNativeBlobUtil.cp(path, dest, (err, res) => {
314
+ if (err)
315
+ reject(addCode('EUNSPECIFIED', new Error(err)));
316
+ else
317
+ resolve(res);
318
+ });
319
+ });
320
+ }
321
+
322
+ function mv(path: string, dest: string): Promise<boolean> {
323
+ return new Promise((resolve, reject) => {
324
+ if (typeof path !== 'string' || typeof dest !== 'string') {
325
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')));
326
+ }
327
+ ReactNativeBlobUtil.mv(path, dest, (err, res) => {
328
+ if (err)
329
+ reject(addCode('EUNSPECIFIED', new Error(err)));
330
+ else
331
+ resolve(res);
332
+ });
333
+ });
334
+ }
335
+
336
+ function lstat(path: string): Promise<Array<ReactNativeBlobUtilFile>> {
337
+ return new Promise((resolve, reject) => {
338
+ if (typeof path !== 'string') {
339
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
340
+ }
341
+ ReactNativeBlobUtil.lstat(path, (err, stat) => {
342
+ if (err)
343
+ reject(addCode('EUNSPECIFIED', new Error(err)));
344
+ else
345
+ resolve(stat);
346
+ });
347
+ });
348
+ }
349
+
350
+ function ls(path: string): Promise<Array<String>> {
351
+ if (typeof path !== 'string') {
352
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
353
+ }
354
+ return ReactNativeBlobUtil.ls(path);
355
+ }
356
+
357
+ /**
358
+ * Remove file at path.
359
+ * @param {string} path:string Path of target file.
360
+ * @return {Promise}
361
+ */
362
+ function unlink(path: string): Promise {
363
+ return new Promise((resolve, reject) => {
364
+ if (typeof path !== 'string') {
365
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
366
+ }
367
+ ReactNativeBlobUtil.unlink(path, (err) => {
368
+ if (err) {
369
+ reject(addCode('EUNSPECIFIED', new Error(err)));
370
+ }
371
+ else
372
+ resolve();
373
+ });
374
+ });
375
+ }
376
+
377
+ /**
378
+ * Check if file exists and if it is a folder.
379
+ * @param {string} path Path to check
380
+ * @return {Promise<boolean>}
381
+ */
382
+ function exists(path: string): Promise<boolean> {
383
+ return new Promise((resolve, reject) => {
384
+ if (typeof path !== 'string') {
385
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
386
+ }
387
+ try {
388
+ ReactNativeBlobUtil.exists(path, (exist) => {
389
+ resolve(exist);
390
+ });
391
+ } catch (err) {
392
+ reject(addCode('EUNSPECIFIED', new Error(err)));
393
+ }
394
+ });
395
+
396
+ }
397
+
398
+ function slice(src: string, dest: string, start: number, end: number): Promise {
399
+ if (typeof src !== 'string' || typeof dest !== 'string') {
400
+ return reject(addCode('EINVAL', new TypeError('Missing argument "src" and/or "destination"')));
401
+ }
402
+
403
+ let p = Promise.resolve();
404
+ let size = 0;
405
+
406
+ function normalize(num, size) {
407
+ if (num < 0)
408
+ return Math.max(0, size + num);
409
+ if (!num && num !== 0)
410
+ return size;
411
+ return num;
412
+ }
413
+
414
+ if (start < 0 || end < 0 || !start || !end) {
415
+ p = p.then(() => stat(src))
416
+ .then((stat) => {
417
+ size = Math.floor(stat.size);
418
+ start = normalize(start || 0, size);
419
+ end = normalize(end, size);
420
+ });
421
+ }
422
+ return p.then(() => ReactNativeBlobUtil.slice(src, dest, start, end));
423
+ }
424
+
425
+ function isDir(path: string): Promise<bool> {
426
+ return new Promise((resolve, reject) => {
427
+ if (typeof path !== 'string') {
428
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')));
429
+ }
430
+ try {
431
+ ReactNativeBlobUtil.exists(path, (exist, isDir) => {
432
+ resolve(isDir);
433
+ });
434
+ } catch (err) {
435
+ reject(addCode('EUNSPECIFIED', new Error(err)));
436
+ }
437
+ });
438
+
439
+ }
440
+
441
+ function df(): Promise<{ free: number, total: number }> {
442
+ return new Promise((resolve, reject) => {
443
+ ReactNativeBlobUtil.df((err, stat) => {
444
+ if (err)
445
+ reject(addCode('EUNSPECIFIED', new Error(err)));
446
+ else
447
+ resolve(stat);
448
+ });
449
+ });
450
+ }
451
+
452
+ export default {
453
+ ReactNativeBlobUtilSession,
454
+ unlink,
455
+ mkdir,
456
+ session,
457
+ ls,
458
+ readStream,
459
+ mv,
460
+ cp,
461
+ writeStream,
462
+ writeFile,
463
+ writeFileWithTransform,
464
+ readFileWithTransform,
465
+ appendFile,
466
+ pathForAppGroup,
467
+ syncPathAppGroup,
468
+ readFile,
469
+ hash,
470
+ exists,
471
+ createFile,
472
+ isDir,
473
+ stat,
474
+ lstat,
475
+ scanFile,
476
+ dirs,
477
+ slice,
478
+ asset,
479
+ df
480
+ };