react-native-blob-util 0.24.9 → 0.24.11

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.
package/index.d.ts CHANGED
@@ -625,7 +625,7 @@ export interface AndroidApi {
625
625
  getSDCardApplicationDir(): Promise<string>;
626
626
  }
627
627
 
628
- type Methods = "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "post" | "get" | "delete" | "put" | "patch";
628
+ type Methods = "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "HEAD" | "post" | "get" | "delete" | "put" | "patch" | "head";
629
629
 
630
630
  /**
631
631
  * A declare class inherits Promise, it has extra method like progress, uploadProgress,
@@ -640,7 +640,7 @@ export interface StatefulPromise<T> extends Promise<T> {
640
640
  /**
641
641
  * Add an event listener which triggers when data receiving from server.
642
642
  */
643
- progress(callback: (received: string, total: string) => void): StatefulPromise<FetchBlobResponse>;
643
+ progress(callback: (received: number, total: number) => void): StatefulPromise<FetchBlobResponse>;
644
644
 
645
645
  /**
646
646
  * Add an event listener with custom configuration
package/index.js.flow CHANGED
@@ -1,192 +1,192 @@
1
- // @flow strict
2
-
3
- declare class AndroidApi {
4
- actionViewIntent(path: string, mime?: string): Promise<void>;
5
- addCompleteDownload(options: AndroidDownloadOption): Promise<void>;
6
- getContentIntent(mime: string): Promise<string>;
7
- getSDCardApplicationDir(): Promise<string>;
8
- getSDCardDir(): Promise<string>;
9
- }
10
-
11
- declare class FetchBlobResponse {
12
- data: string;
13
- respInfo: ReactNativeBlobUtilResponseInfo;
14
- taskId: string;
15
- type: "utf8" | "base64" | "path";
16
- array(): Promise<number[]>;
17
- base64(): string | Promise<string>;
18
- blob(): Promise<Blob>;
19
- flush(): Promise<void>;
20
- info(): ReactNativeBlobUtilResponseInfo;
21
- // $FlowExpectedError Not possible to specify in strict mode.
22
- json(): any;
23
- path(): string;
24
- readFile(encoding: Encoding): ?Promise<number[] | string>;
25
- readStream(encoding: Encoding): ?Promise<ReactNativeBlobUtilReadStream>;
26
- session(name: string): ?ReactNativeBlobUtilSession;
27
- text(): string | Promise<string>;
28
- }
29
-
30
- declare class FsApi {
31
- ReactNativeBlobUtilSession: ReactNativeBlobUtilSession;
32
- dirs: Dirs;
33
- appendFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>;
34
- asset(path: string): string;
35
- cp(path: string, dest: string): Promise<boolean>;
36
- createFile(path: string, data: string, encoding: Encoding | "uri"): Promise<string>;
37
- df(): Promise<AndroidFsStat | IosFsStat>;
38
- exists(path: string): Promise<boolean>;
39
- hash(path: string, algorithm: HashAlgorithm): Promise<string>;
40
- isDir(path: string): Promise<boolean>;
41
- ls(path: string): Promise<string[]>;
42
- lstat(path: string): Promise<ReactNativeBlobUtilStat[]>;
43
- mkdir(path: string): Promise<boolean>;
44
- mv(path: string, dest: string): Promise<boolean>;
45
- pathForAppGroup(groupName: string): Promise<string>;
46
- readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<number[] | string>;
47
- readStream(path: string, encoding: Encoding, bufferSize?: number, tick?: number): Promise<ReactNativeBlobUtilReadStream>;
48
- scanFile(pairs: { mime: string, path: string }[]): Promise<void>;
49
- session(name: string): ReactNativeBlobUtilSession;
50
- slice(src: string, dest: string, start: number, end: number): Promise<string>;
51
- stat(path: string): Promise<ReactNativeBlobUtilStat>;
52
- unlink(path: string): Promise<void>;
53
- writeFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>;
54
- writeStream(path: string, encoding: Encoding, append?: boolean): Promise<ReactNativeBlobUtilWriteStream>;
55
- }
56
-
57
- declare class IosApi {
58
- excludeFromBackupKey(path: string): Promise<void>;
59
- openDocument(path: string, scheme?: string): Promise<void>;
60
- previewDocument(path: string, scheme?: string): Promise<void>;
61
- }
62
-
63
- declare class ReactNativeBlobUtilReadStream {
64
- bufferSize?: number;
65
- closed: boolean;
66
- encoding: Encoding;
67
- path: string;
68
- tick: number;
69
- onData(fn: (chunk: string) => void): void;
70
- onEnd(fn: () => void): void;
71
- onError(fn: (err: Error) => void): void;
72
- open(): void;
73
- }
74
-
75
- declare class ReactNativeBlobUtilSession {
76
- static getSession(name: string): ReactNativeBlobUtilSession;
77
- static removeSession(name: string): void;
78
- static setSession(name: string, val: ReactNativeBlobUtilSession): void;
79
-
80
- name: string;
81
- add(path: string): ReactNativeBlobUtilSession;
82
- constructor(name: string, list: string[]): ReactNativeBlobUtilSession;
83
- dispose(): Promise<void>;
84
- list(): string[];
85
- remove(path: string): ReactNativeBlobUtilSession;
86
- }
87
-
88
- declare class ReactNativeBlobUtilWriteStream {
89
- append: boolean;
90
- encoding: string;
91
- id: string;
92
- close(): void;
93
- write(data: string): Promise<void>;
94
- }
95
-
96
- declare class ReactNativeBlobUtil {
97
- android: AndroidApi;
98
- base64: { +decode: (input: string) => string, +encode: (input: string) => string };
99
- fs: FsApi;
100
- ios: IosApi;
101
- config(options: ReactNativeBlobUtilConfig): { fetch: (method: Methods, url: string, headers?: { [key: string]: string }, body?: string | FormField[]) => StatefulPromise<FetchBlobResponse> };
102
- fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: string | FormField[]): StatefulPromise<FetchBlobResponse>;
103
- session(name: string): ReactNativeBlobUtilSession;
104
- wrap(path: string): string;
105
- }
106
-
107
- declare class StatefulPromise<T> extends Promise<T> {
108
- cancel(callback?: (error: ?string, taskId: ?string) => void): void;
109
- expire(callback: () => void): StatefulPromise<void>;
110
- progress(config: { count?: number, interval?: number } | ProgressCallback, callback?: ProgressCallback): StatefulPromise<T>;
111
- stateChange(callback: (state: ReactNativeBlobUtilResponseInfo) => void): StatefulPromise<T>;
112
- uploadProgress(config: { count?: number, interval?: number } | UploadProgressCallback, callback?: UploadProgressCallback): StatefulPromise<T>;
113
- }
114
-
115
- export type AddAndroidDownloads = {
116
- description?: string,
117
- mediaScannable?: boolean,
118
- mime?: string,
119
- notification?: boolean,
120
- path?: string,
121
- title?: string,
122
- useDownloadManager?: boolean,
123
- storeLocal: boolean
124
- };
125
- export type AndroidDownloadOption = {
126
- description?: string,
127
- mime?: string,
128
- path: string,
129
- showNotification?: boolean,
130
- title?: string
131
- };
132
- export type AndroidFsStat = {
133
- external_free: number,
134
- external_total: number,
135
- internal_free: number,
136
- internal_total: number
137
- };
138
- export type Dirs = {
139
- CacheDir: string,
140
- DCIMDir: string,
141
- DocumentDir: string,
142
- DownloadDir: string,
143
- LibraryDir: string,
144
- MainBundleDir: string,
145
- MovieDir: string,
146
- MusicDir: string,
147
- PictureDir: string,
148
- SDCardApplicationDir: string,
149
- SDCardDir: string
150
- };
151
- export type Encoding = "utf8" | "ascii" | "base64";
152
- export type FormField = { data: string, filename?: string, name: string, type?: string };
153
- export type HashAlgorithm = "md5" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512";
154
- export type IosFsStat = { free: number, total: number };
155
- export type Methods = "POST" | "GET" | "DELETE" | "PUT" | "post" | "get" | "delete" | "put";
156
- export type ProgressCallback = (received: number, total: number) => void;
157
- export type ReactNativeBlobUtilConfig = {
158
- Progress?: { count?: number, interval?: number },
159
- UploadProgress?: { count?: number, interval?: number },
160
- IOSBackgroundTask?: boolean,
161
- addAndroidDownloads?: AddAndroidDownloads,
162
- appendExt?: string,
163
- fileCache?: boolean,
164
- indicator?: boolean,
165
- overwrite?: boolean,
166
- path?: string,
167
- session?: string,
168
- timeout?: number,
169
- trusty?: boolean,
170
- wifiOnly?: boolean,
171
- followRedirect?: boolean
172
- };
173
- export type ReactNativeBlobUtilResponseInfo = {
174
- headers: { [fieldName: string]: string },
175
- redirects: string[],
176
- respType: "text" | "blob" | "" | "json",
177
- rnfbEncode: "path" | Encoding,
178
- state: string,
179
- status: number,
180
- taskId: string,
181
- timeout: boolean
182
- };
183
- export type ReactNativeBlobUtilStat = {
184
- filename: string,
185
- lastModified: number,
186
- path: string,
187
- size: number,
188
- type: "directory" | "file" | "asset"
189
- };
190
- export type UploadProgressCallback = (sent: number, total: number) => void;
191
-
192
- declare export default ReactNativeBlobUtil;
1
+ // @flow strict
2
+
3
+ declare class AndroidApi {
4
+ actionViewIntent(path: string, mime?: string): Promise<void>;
5
+ addCompleteDownload(options: AndroidDownloadOption): Promise<void>;
6
+ getContentIntent(mime: string): Promise<string>;
7
+ getSDCardApplicationDir(): Promise<string>;
8
+ getSDCardDir(): Promise<string>;
9
+ }
10
+
11
+ declare class FetchBlobResponse {
12
+ data: string;
13
+ respInfo: ReactNativeBlobUtilResponseInfo;
14
+ taskId: string;
15
+ type: "utf8" | "base64" | "path";
16
+ array(): Promise<number[]>;
17
+ base64(): string | Promise<string>;
18
+ blob(): Promise<Blob>;
19
+ flush(): Promise<void>;
20
+ info(): ReactNativeBlobUtilResponseInfo;
21
+ // $FlowExpectedError Not possible to specify in strict mode.
22
+ json(): any;
23
+ path(): string;
24
+ readFile(encoding: Encoding): ?Promise<number[] | string>;
25
+ readStream(encoding: Encoding): ?Promise<ReactNativeBlobUtilReadStream>;
26
+ session(name: string): ?ReactNativeBlobUtilSession;
27
+ text(): string | Promise<string>;
28
+ }
29
+
30
+ declare class FsApi {
31
+ ReactNativeBlobUtilSession: ReactNativeBlobUtilSession;
32
+ dirs: Dirs;
33
+ appendFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>;
34
+ asset(path: string): string;
35
+ cp(path: string, dest: string): Promise<boolean>;
36
+ createFile(path: string, data: string, encoding: Encoding | "uri"): Promise<string>;
37
+ df(): Promise<AndroidFsStat | IosFsStat>;
38
+ exists(path: string): Promise<boolean>;
39
+ hash(path: string, algorithm: HashAlgorithm): Promise<string>;
40
+ isDir(path: string): Promise<boolean>;
41
+ ls(path: string): Promise<string[]>;
42
+ lstat(path: string): Promise<ReactNativeBlobUtilStat[]>;
43
+ mkdir(path: string): Promise<boolean>;
44
+ mv(path: string, dest: string): Promise<boolean>;
45
+ pathForAppGroup(groupName: string): Promise<string>;
46
+ readFile(path: string, encoding: Encoding, bufferSize?: number): Promise<number[] | string>;
47
+ readStream(path: string, encoding: Encoding, bufferSize?: number, tick?: number): Promise<ReactNativeBlobUtilReadStream>;
48
+ scanFile(pairs: { mime: string, path: string }[]): Promise<void>;
49
+ session(name: string): ReactNativeBlobUtilSession;
50
+ slice(src: string, dest: string, start: number, end: number): Promise<string>;
51
+ stat(path: string): Promise<ReactNativeBlobUtilStat>;
52
+ unlink(path: string): Promise<void>;
53
+ writeFile(path: string, data: string | number[], encoding?: Encoding | "uri"): Promise<number>;
54
+ writeStream(path: string, encoding: Encoding, append?: boolean): Promise<ReactNativeBlobUtilWriteStream>;
55
+ }
56
+
57
+ declare class IosApi {
58
+ excludeFromBackupKey(path: string): Promise<void>;
59
+ openDocument(path: string, scheme?: string): Promise<void>;
60
+ previewDocument(path: string, scheme?: string): Promise<void>;
61
+ }
62
+
63
+ declare class ReactNativeBlobUtilReadStream {
64
+ bufferSize?: number;
65
+ closed: boolean;
66
+ encoding: Encoding;
67
+ path: string;
68
+ tick: number;
69
+ onData(fn: (chunk: string) => void): void;
70
+ onEnd(fn: () => void): void;
71
+ onError(fn: (err: Error) => void): void;
72
+ open(): void;
73
+ }
74
+
75
+ declare class ReactNativeBlobUtilSession {
76
+ static getSession(name: string): ReactNativeBlobUtilSession;
77
+ static removeSession(name: string): void;
78
+ static setSession(name: string, val: ReactNativeBlobUtilSession): void;
79
+
80
+ name: string;
81
+ add(path: string): ReactNativeBlobUtilSession;
82
+ constructor(name: string, list: string[]): ReactNativeBlobUtilSession;
83
+ dispose(): Promise<void>;
84
+ list(): string[];
85
+ remove(path: string): ReactNativeBlobUtilSession;
86
+ }
87
+
88
+ declare class ReactNativeBlobUtilWriteStream {
89
+ append: boolean;
90
+ encoding: string;
91
+ id: string;
92
+ close(): void;
93
+ write(data: string): Promise<void>;
94
+ }
95
+
96
+ declare class ReactNativeBlobUtil {
97
+ android: AndroidApi;
98
+ base64: { +decode: (input: string) => string, +encode: (input: string) => string };
99
+ fs: FsApi;
100
+ ios: IosApi;
101
+ config(options: ReactNativeBlobUtilConfig): { fetch: (method: Methods, url: string, headers?: { [key: string]: string }, body?: string | FormField[]) => StatefulPromise<FetchBlobResponse> };
102
+ fetch(method: Methods, url: string, headers?: { [key: string]: string }, body?: string | FormField[]): StatefulPromise<FetchBlobResponse>;
103
+ session(name: string): ReactNativeBlobUtilSession;
104
+ wrap(path: string): string;
105
+ }
106
+
107
+ declare class StatefulPromise<T> extends Promise<T> {
108
+ cancel(callback?: (error: ?string, taskId: ?string) => void): void;
109
+ expire(callback: () => void): StatefulPromise<void>;
110
+ progress(config: { count?: number, interval?: number } | ProgressCallback, callback?: ProgressCallback): StatefulPromise<T>;
111
+ stateChange(callback: (state: ReactNativeBlobUtilResponseInfo) => void): StatefulPromise<T>;
112
+ uploadProgress(config: { count?: number, interval?: number } | UploadProgressCallback, callback?: UploadProgressCallback): StatefulPromise<T>;
113
+ }
114
+
115
+ export type AddAndroidDownloads = {
116
+ description?: string,
117
+ mediaScannable?: boolean,
118
+ mime?: string,
119
+ notification?: boolean,
120
+ path?: string,
121
+ title?: string,
122
+ useDownloadManager?: boolean,
123
+ storeLocal: boolean
124
+ };
125
+ export type AndroidDownloadOption = {
126
+ description?: string,
127
+ mime?: string,
128
+ path: string,
129
+ showNotification?: boolean,
130
+ title?: string
131
+ };
132
+ export type AndroidFsStat = {
133
+ external_free: number,
134
+ external_total: number,
135
+ internal_free: number,
136
+ internal_total: number
137
+ };
138
+ export type Dirs = {
139
+ CacheDir: string,
140
+ DCIMDir: string,
141
+ DocumentDir: string,
142
+ DownloadDir: string,
143
+ LibraryDir: string,
144
+ MainBundleDir: string,
145
+ MovieDir: string,
146
+ MusicDir: string,
147
+ PictureDir: string,
148
+ SDCardApplicationDir: string,
149
+ SDCardDir: string
150
+ };
151
+ export type Encoding = "utf8" | "ascii" | "base64";
152
+ export type FormField = { data: string, filename?: string, name: string, type?: string };
153
+ export type HashAlgorithm = "md5" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512";
154
+ export type IosFsStat = { free: number, total: number };
155
+ export type Methods = "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "HEAD" | "post" | "get" | "delete" | "put" | "patch" | "head";
156
+ export type ProgressCallback = (received: number, total: number) => void;
157
+ export type ReactNativeBlobUtilConfig = {
158
+ Progress?: { count?: number, interval?: number },
159
+ UploadProgress?: { count?: number, interval?: number },
160
+ IOSBackgroundTask?: boolean,
161
+ addAndroidDownloads?: AddAndroidDownloads,
162
+ appendExt?: string,
163
+ fileCache?: boolean,
164
+ indicator?: boolean,
165
+ overwrite?: boolean,
166
+ path?: string,
167
+ session?: string,
168
+ timeout?: number,
169
+ trusty?: boolean,
170
+ wifiOnly?: boolean,
171
+ followRedirect?: boolean
172
+ };
173
+ export type ReactNativeBlobUtilResponseInfo = {
174
+ headers: { [fieldName: string]: string },
175
+ redirects: string[],
176
+ respType: "text" | "blob" | "" | "json",
177
+ rnfbEncode: "path" | Encoding,
178
+ state: string,
179
+ status: number,
180
+ taskId: string,
181
+ timeout: boolean
182
+ };
183
+ export type ReactNativeBlobUtilStat = {
184
+ filename: string,
185
+ lastModified: number,
186
+ path: string,
187
+ size: number,
188
+ type: "directory" | "file" | "asset"
189
+ };
190
+ export type UploadProgressCallback = (sent: number, total: number) => void;
191
+
192
+ declare export default ReactNativeBlobUtil;
@@ -241,6 +241,52 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
241
241
  }
242
242
  }
243
243
 
244
+ - (BOOL)copyDownloadedFile:(NSURL *)sourceURL toPath:(NSString *)targetPath append:(BOOL)append
245
+ {
246
+ NSInputStream *inputStream = [NSInputStream inputStreamWithURL:sourceURL];
247
+ NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:append];
248
+
249
+ if (!inputStream || !outputStream) {
250
+ return NO;
251
+ }
252
+
253
+ [inputStream open];
254
+ [outputStream open];
255
+
256
+ uint8_t buffer[65536];
257
+ BOOL success = YES;
258
+
259
+ while ([inputStream hasBytesAvailable]) {
260
+ NSInteger bytesRead = [inputStream read:buffer maxLength:sizeof(buffer)];
261
+ if (bytesRead < 0) {
262
+ success = NO;
263
+ break;
264
+ }
265
+ if (bytesRead == 0) {
266
+ break;
267
+ }
268
+
269
+ NSInteger bytesWritten = 0;
270
+ while (bytesWritten < bytesRead) {
271
+ NSInteger writeResult = [outputStream write:&buffer[bytesWritten] maxLength:(NSUInteger)(bytesRead - bytesWritten)];
272
+ if (writeResult <= 0) {
273
+ success = NO;
274
+ break;
275
+ }
276
+ bytesWritten += writeResult;
277
+ }
278
+
279
+ if (!success) {
280
+ break;
281
+ }
282
+ }
283
+
284
+ [inputStream close];
285
+ [outputStream close];
286
+
287
+ return success;
288
+ }
289
+
244
290
 
245
291
  #pragma mark - Received Response
246
292
  // set expected content length on response received
@@ -558,10 +604,40 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
558
604
  - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
559
605
 
560
606
  NSFileManager *fm = [NSFileManager defaultManager];
561
- NSData *data = [fm contentsAtPath:location.path];
607
+ if (respFile && ![self ShouldTransformFile]) {
608
+ if (writeStream) {
609
+ [writeStream close];
610
+ writeStream = nil;
611
+ }
562
612
 
563
- [self configureWriteStream];
613
+ NSString *folder = [destPath stringByDeletingLastPathComponent];
614
+ if (![fm fileExistsAtPath:folder]) {
615
+ [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
616
+ }
617
+
618
+ BOOL overwrite = [options valueForKey:@"overwrite"] == nil ? YES : [[options valueForKey:@"overwrite"] boolValue];
619
+ BOOL appendToExistingFile = [destPath containsString:@"?append=true"] || !overwrite;
620
+ NSString *normalizedDestPath = [destPath stringByReplacingOccurrencesOfString:@"?append=true" withString:@""];
621
+ destPath = normalizedDestPath;
622
+
623
+ if (!appendToExistingFile && [fm fileExistsAtPath:destPath]) {
624
+ [fm removeItemAtPath:destPath error:nil];
625
+ }
626
+
627
+ if (!appendToExistingFile) {
628
+ NSError *moveError = nil;
629
+ if ([fm moveItemAtURL:location toURL:[NSURL fileURLWithPath:destPath] error:&moveError]) {
630
+ return;
631
+ }
632
+ }
564
633
 
634
+ if ([self copyDownloadedFile:location toPath:destPath append:appendToExistingFile]) {
635
+ return;
636
+ }
637
+ }
638
+
639
+ NSData *data = [fm contentsAtPath:location.path];
640
+ [self configureWriteStream];
565
641
  [self processData:data];
566
642
 
567
643
  }