@symbiote-native/file-system 0.1.0
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/LICENSE +21 -0
- package/README.md +262 -0
- package/build/angular/index.d.ts +1 -0
- package/build/angular/index.js +1 -0
- package/build/core/file-system.d.ts +169 -0
- package/build/core/file-system.js +404 -0
- package/build/core/index.d.ts +2 -0
- package/build/core/index.js +2 -0
- package/build/core/native-module.d.ts +32 -0
- package/build/core/native-module.js +2 -0
- package/build/core/types.d.ts +104 -0
- package/build/core/types.js +15 -0
- package/build/next/directory.d.ts +36 -0
- package/build/next/directory.js +59 -0
- package/build/next/file.d.ts +71 -0
- package/build/next/file.js +207 -0
- package/build/next/index.d.ts +5 -0
- package/build/next/index.js +5 -0
- package/build/next/native-module.d.ts +109 -0
- package/build/next/native-module.js +2 -0
- package/build/next/network-tasks.d.ts +118 -0
- package/build/next/network-tasks.js +333 -0
- package/build/next/node-path.d.ts +16 -0
- package/build/next/node-path.js +353 -0
- package/build/next/path-utilities.d.ts +42 -0
- package/build/next/path-utilities.js +110 -0
- package/build/next/paths.d.ts +22 -0
- package/build/next/paths.js +43 -0
- package/build/next/streams.d.ts +25 -0
- package/build/next/streams.js +55 -0
- package/build/next/types.d.ts +152 -0
- package/build/next/types.js +19 -0
- package/build/next/url-utilities.d.ts +3 -0
- package/build/next/url-utilities.js +62 -0
- package/build/next/watcher.d.ts +19 -0
- package/build/next/watcher.js +64 -0
- package/build-ngc/angular/index.d.ts +1 -0
- package/build-ngc/angular/index.js +2 -0
- package/build-ngc/angular/index.js.map +1 -0
- package/build-ngc/next/directory.d.ts +36 -0
- package/build-ngc/next/directory.js +60 -0
- package/build-ngc/next/directory.js.map +1 -0
- package/build-ngc/next/file.d.ts +71 -0
- package/build-ngc/next/file.js +208 -0
- package/build-ngc/next/file.js.map +1 -0
- package/build-ngc/next/index.d.ts +5 -0
- package/build-ngc/next/index.js +6 -0
- package/build-ngc/next/index.js.map +1 -0
- package/build-ngc/next/native-module.d.ts +109 -0
- package/build-ngc/next/native-module.js +3 -0
- package/build-ngc/next/native-module.js.map +1 -0
- package/build-ngc/next/network-tasks.d.ts +118 -0
- package/build-ngc/next/network-tasks.js +334 -0
- package/build-ngc/next/network-tasks.js.map +1 -0
- package/build-ngc/next/node-path.d.ts +16 -0
- package/build-ngc/next/node-path.js +354 -0
- package/build-ngc/next/node-path.js.map +1 -0
- package/build-ngc/next/path-utilities.d.ts +42 -0
- package/build-ngc/next/path-utilities.js +111 -0
- package/build-ngc/next/path-utilities.js.map +1 -0
- package/build-ngc/next/paths.d.ts +22 -0
- package/build-ngc/next/paths.js +44 -0
- package/build-ngc/next/paths.js.map +1 -0
- package/build-ngc/next/streams.d.ts +25 -0
- package/build-ngc/next/streams.js +56 -0
- package/build-ngc/next/streams.js.map +1 -0
- package/build-ngc/next/types.d.ts +152 -0
- package/build-ngc/next/types.js +20 -0
- package/build-ngc/next/types.js.map +1 -0
- package/build-ngc/next/url-utilities.d.ts +3 -0
- package/build-ngc/next/url-utilities.js +63 -0
- package/build-ngc/next/url-utilities.js.map +1 -0
- package/build-ngc/next/watcher.d.ts +19 -0
- package/build-ngc/next/watcher.js +65 -0
- package/build-ngc/next/watcher.js.map +1 -0
- package/native-link.json +17 -0
- package/package.json +154 -0
- package/src/angular/index.ts +1 -0
- package/src/core/file-system.ts +634 -0
- package/src/core/index.ts +2 -0
- package/src/core/native-module.ts +99 -0
- package/src/core/types.ts +135 -0
- package/src/next/directory.ts +83 -0
- package/src/next/file.ts +318 -0
- package/src/next/index.ts +5 -0
- package/src/next/native-module.ts +188 -0
- package/src/next/network-tasks.ts +456 -0
- package/src/next/node-path.ts +362 -0
- package/src/next/path-utilities.ts +144 -0
- package/src/next/paths.ts +52 -0
- package/src/next/streams-ambient.d.ts +34 -0
- package/src/next/streams.ts +81 -0
- package/src/next/types.ts +194 -0
- package/src/next/url-utilities.ts +70 -0
- package/src/next/watcher.ts +106 -0
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UnavailabilityError,
|
|
3
|
+
uuid,
|
|
4
|
+
type EventSubscription,
|
|
5
|
+
} from 'expo-modules-core';
|
|
6
|
+
import { Platform } from 'react-native';
|
|
7
|
+
|
|
8
|
+
import { expoFileSystem } from './native-module';
|
|
9
|
+
import {
|
|
10
|
+
FileSystemSessionType,
|
|
11
|
+
FileSystemUploadType,
|
|
12
|
+
type IFileSystemDeletingOptions,
|
|
13
|
+
type IFileSystemDownloadOptions,
|
|
14
|
+
type IFileSystemDownloadPauseState,
|
|
15
|
+
type IFileSystemDownloadProgressData,
|
|
16
|
+
type IFileSystemDownloadResult,
|
|
17
|
+
type IFileSystemFileInfo,
|
|
18
|
+
type IFileSystemInfoOptions,
|
|
19
|
+
type IFileSystemMakeDirectoryOptions,
|
|
20
|
+
type IFileSystemNetworkTaskProgressCallback,
|
|
21
|
+
type IFileSystemProgressEvent,
|
|
22
|
+
type IFileSystemReadingOptions,
|
|
23
|
+
type IFileSystemRelocatingOptions,
|
|
24
|
+
type IFileSystemRequestDirectoryPermissionsResult,
|
|
25
|
+
type IFileSystemUploadOptions,
|
|
26
|
+
type IFileSystemAcceptedUploadHttpMethod,
|
|
27
|
+
type IFileSystemUploadProgressData,
|
|
28
|
+
type IFileSystemUploadResult,
|
|
29
|
+
type IFileSystemWritingOptions,
|
|
30
|
+
} from './types';
|
|
31
|
+
|
|
32
|
+
if (!expoFileSystem) {
|
|
33
|
+
console.warn(
|
|
34
|
+
'No native ExponentFileSystem module found, are you sure the @symbiote-native/file-system module is linked properly?',
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeEndingSlash(p: string | null): string | null {
|
|
39
|
+
if (p != null) {
|
|
40
|
+
return p.replace(/\/*$/, '') + '/';
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `file://` URI pointing to the directory where user documents for this app will be stored.
|
|
47
|
+
* Files stored here will remain until explicitly deleted by the app. Ends with a trailing `/`.
|
|
48
|
+
*/
|
|
49
|
+
export const documentDirectory = normalizeEndingSlash(
|
|
50
|
+
expoFileSystem?.documentDirectory ?? null,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* `file://` URI pointing to the directory where temporary files used by this app will be stored.
|
|
55
|
+
* Files stored here may be automatically deleted by the system when low on storage.
|
|
56
|
+
*/
|
|
57
|
+
export const cacheDirectory = normalizeEndingSlash(
|
|
58
|
+
expoFileSystem?.cacheDirectory ?? null,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* URI to the directory where assets bundled with the application are stored.
|
|
63
|
+
*/
|
|
64
|
+
export const bundleDirectory = normalizeEndingSlash(
|
|
65
|
+
expoFileSystem?.bundleDirectory ?? null,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get metadata information about a file, directory or external content/asset.
|
|
70
|
+
*/
|
|
71
|
+
export async function getInfoAsync(
|
|
72
|
+
fileUri: string,
|
|
73
|
+
options: IFileSystemInfoOptions = {},
|
|
74
|
+
): Promise<IFileSystemFileInfo> {
|
|
75
|
+
if (!expoFileSystem?.getInfoAsync) {
|
|
76
|
+
throw new UnavailabilityError(
|
|
77
|
+
'@symbiote-native/file-system',
|
|
78
|
+
'getInfoAsync',
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return await expoFileSystem.getInfoAsync(fileUri, options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Read the entire contents of a file as a string. Binary is returned in raw format — prepend
|
|
86
|
+
* `data:image/png;base64,` yourself to use it as a data URI.
|
|
87
|
+
*/
|
|
88
|
+
export async function readAsStringAsync(
|
|
89
|
+
fileUri: string,
|
|
90
|
+
options: IFileSystemReadingOptions = {},
|
|
91
|
+
): Promise<string> {
|
|
92
|
+
if (!expoFileSystem?.readAsStringAsync) {
|
|
93
|
+
throw new UnavailabilityError(
|
|
94
|
+
'@symbiote-native/file-system',
|
|
95
|
+
'readAsStringAsync',
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return await expoFileSystem.readAsStringAsync(fileUri, options);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Takes a `file://` URI and converts it into a `content://` URI so that it can be accessed by
|
|
103
|
+
* other applications outside of the app.
|
|
104
|
+
* @platform android
|
|
105
|
+
*/
|
|
106
|
+
export async function getContentUriAsync(fileUri: string): Promise<string> {
|
|
107
|
+
if (Platform.OS === 'android') {
|
|
108
|
+
if (!expoFileSystem?.getContentUriAsync) {
|
|
109
|
+
throw new UnavailabilityError(
|
|
110
|
+
'@symbiote-native/file-system',
|
|
111
|
+
'getContentUriAsync',
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return await expoFileSystem.getContentUriAsync(fileUri);
|
|
115
|
+
}
|
|
116
|
+
return fileUri;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Write the entire contents of a file as a string.
|
|
121
|
+
*/
|
|
122
|
+
export async function writeAsStringAsync(
|
|
123
|
+
fileUri: string,
|
|
124
|
+
contents: string,
|
|
125
|
+
options: IFileSystemWritingOptions = {},
|
|
126
|
+
): Promise<void> {
|
|
127
|
+
if (!expoFileSystem?.writeAsStringAsync) {
|
|
128
|
+
throw new UnavailabilityError(
|
|
129
|
+
'@symbiote-native/file-system',
|
|
130
|
+
'writeAsStringAsync',
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
return await expoFileSystem.writeAsStringAsync(fileUri, contents, options);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Delete a file or directory. If the URI points to a directory, the directory and all its
|
|
138
|
+
* contents are recursively deleted.
|
|
139
|
+
*/
|
|
140
|
+
export async function deleteAsync(
|
|
141
|
+
fileUri: string,
|
|
142
|
+
options: IFileSystemDeletingOptions = {},
|
|
143
|
+
): Promise<void> {
|
|
144
|
+
if (!expoFileSystem?.deleteAsync) {
|
|
145
|
+
throw new UnavailabilityError(
|
|
146
|
+
'@symbiote-native/file-system',
|
|
147
|
+
'deleteAsync',
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
return await expoFileSystem.deleteAsync(fileUri, options);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Move a file or directory to a new location.
|
|
155
|
+
*/
|
|
156
|
+
export async function moveAsync(
|
|
157
|
+
options: IFileSystemRelocatingOptions,
|
|
158
|
+
): Promise<void> {
|
|
159
|
+
if (!expoFileSystem?.moveAsync) {
|
|
160
|
+
throw new UnavailabilityError('@symbiote-native/file-system', 'moveAsync');
|
|
161
|
+
}
|
|
162
|
+
return await expoFileSystem.moveAsync(options);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Create a copy of a file or directory. Directories are recursively copied with all of their
|
|
167
|
+
* contents. Also works to copy content shared by other apps into local storage.
|
|
168
|
+
*/
|
|
169
|
+
export async function copyAsync(
|
|
170
|
+
options: IFileSystemRelocatingOptions,
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
if (!expoFileSystem?.copyAsync) {
|
|
173
|
+
throw new UnavailabilityError('@symbiote-native/file-system', 'copyAsync');
|
|
174
|
+
}
|
|
175
|
+
return await expoFileSystem.copyAsync(options);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Create a new empty directory.
|
|
180
|
+
*/
|
|
181
|
+
export async function makeDirectoryAsync(
|
|
182
|
+
fileUri: string,
|
|
183
|
+
options: IFileSystemMakeDirectoryOptions = {},
|
|
184
|
+
): Promise<void> {
|
|
185
|
+
if (!expoFileSystem?.makeDirectoryAsync) {
|
|
186
|
+
throw new UnavailabilityError(
|
|
187
|
+
'@symbiote-native/file-system',
|
|
188
|
+
'makeDirectoryAsync',
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
return await expoFileSystem.makeDirectoryAsync(fileUri, options);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Enumerate the contents of a directory.
|
|
196
|
+
*/
|
|
197
|
+
export async function readDirectoryAsync(fileUri: string): Promise<string[]> {
|
|
198
|
+
if (!expoFileSystem?.readDirectoryAsync) {
|
|
199
|
+
throw new UnavailabilityError(
|
|
200
|
+
'@symbiote-native/file-system',
|
|
201
|
+
'readDirectoryAsync',
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
return await expoFileSystem.readDirectoryAsync(fileUri);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Gets the available internal disk storage size, in bytes.
|
|
209
|
+
*/
|
|
210
|
+
export async function getFreeDiskStorageAsync(): Promise<number> {
|
|
211
|
+
if (!expoFileSystem?.getFreeDiskStorageAsync) {
|
|
212
|
+
throw new UnavailabilityError(
|
|
213
|
+
'@symbiote-native/file-system',
|
|
214
|
+
'getFreeDiskStorageAsync',
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
return await expoFileSystem.getFreeDiskStorageAsync();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Gets total internal disk storage size, in bytes.
|
|
222
|
+
*/
|
|
223
|
+
export async function getTotalDiskCapacityAsync(): Promise<number> {
|
|
224
|
+
if (!expoFileSystem?.getTotalDiskCapacityAsync) {
|
|
225
|
+
throw new UnavailabilityError(
|
|
226
|
+
'@symbiote-native/file-system',
|
|
227
|
+
'getTotalDiskCapacityAsync',
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return await expoFileSystem.getTotalDiskCapacityAsync();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Download the contents at a remote URI to a file in the app's file system. The directory for
|
|
235
|
+
* the local file URI must exist prior to calling this function.
|
|
236
|
+
*/
|
|
237
|
+
export async function downloadAsync(
|
|
238
|
+
uri: string,
|
|
239
|
+
fileUri: string,
|
|
240
|
+
options: IFileSystemDownloadOptions = {},
|
|
241
|
+
): Promise<IFileSystemDownloadResult> {
|
|
242
|
+
if (!expoFileSystem?.downloadAsync) {
|
|
243
|
+
throw new UnavailabilityError(
|
|
244
|
+
'@symbiote-native/file-system',
|
|
245
|
+
'downloadAsync',
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
return await expoFileSystem.downloadAsync(uri, fileUri, {
|
|
249
|
+
sessionType: FileSystemSessionType.BACKGROUND,
|
|
250
|
+
...options,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Upload the contents of the file pointed by `fileUri` to the remote url.
|
|
256
|
+
*/
|
|
257
|
+
export async function uploadAsync(
|
|
258
|
+
url: string,
|
|
259
|
+
fileUri: string,
|
|
260
|
+
options: IFileSystemUploadOptions = {},
|
|
261
|
+
): Promise<IFileSystemUploadResult> {
|
|
262
|
+
if (!expoFileSystem?.uploadAsync) {
|
|
263
|
+
throw new UnavailabilityError(
|
|
264
|
+
'@symbiote-native/file-system',
|
|
265
|
+
'uploadAsync',
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return await expoFileSystem.uploadAsync(url, fileUri, {
|
|
269
|
+
sessionType: FileSystemSessionType.BACKGROUND,
|
|
270
|
+
uploadType: FileSystemUploadType.BINARY_CONTENT,
|
|
271
|
+
...options,
|
|
272
|
+
httpMethod: (
|
|
273
|
+
options.httpMethod || 'POST'
|
|
274
|
+
).toUpperCase() as IFileSystemAcceptedUploadHttpMethod,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Create a `DownloadResumable` object which can start, pause, and resume a download of contents
|
|
280
|
+
* at a remote URI to a file in the app's file system.
|
|
281
|
+
*/
|
|
282
|
+
export function createDownloadResumable(
|
|
283
|
+
uri: string,
|
|
284
|
+
fileUri: string,
|
|
285
|
+
options?: IFileSystemDownloadOptions,
|
|
286
|
+
callback?: IFileSystemNetworkTaskProgressCallback<IFileSystemDownloadProgressData>,
|
|
287
|
+
resumeData?: string,
|
|
288
|
+
): DownloadResumable {
|
|
289
|
+
return new DownloadResumable(uri, fileUri, options, callback, resumeData);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function createUploadTask(
|
|
293
|
+
url: string,
|
|
294
|
+
fileUri: string,
|
|
295
|
+
options?: IFileSystemUploadOptions,
|
|
296
|
+
callback?: IFileSystemNetworkTaskProgressCallback<IFileSystemUploadProgressData>,
|
|
297
|
+
): UploadTask {
|
|
298
|
+
return new UploadTask(url, fileUri, options, callback);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export abstract class FileSystemCancellableNetworkTask<
|
|
302
|
+
T extends IFileSystemDownloadProgressData | IFileSystemUploadProgressData,
|
|
303
|
+
> {
|
|
304
|
+
private _uuid = uuid.v4();
|
|
305
|
+
protected taskWasCanceled = false;
|
|
306
|
+
private subscription?: EventSubscription | null;
|
|
307
|
+
|
|
308
|
+
public async cancelAsync(): Promise<void> {
|
|
309
|
+
if (!expoFileSystem?.networkTaskCancelAsync) {
|
|
310
|
+
throw new UnavailabilityError(
|
|
311
|
+
'@symbiote-native/file-system',
|
|
312
|
+
'networkTaskCancelAsync',
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
this.removeSubscription();
|
|
316
|
+
this.taskWasCanceled = true;
|
|
317
|
+
return await expoFileSystem.networkTaskCancelAsync(this.uuid);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
protected isTaskCancelled(): boolean {
|
|
321
|
+
if (this.taskWasCanceled) {
|
|
322
|
+
console.warn('This task was already canceled.');
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
protected get uuid(): string {
|
|
329
|
+
return this._uuid;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
protected abstract getEventName(): string;
|
|
333
|
+
|
|
334
|
+
protected abstract getCallback():
|
|
335
|
+
IFileSystemNetworkTaskProgressCallback<T> | undefined;
|
|
336
|
+
|
|
337
|
+
protected addSubscription() {
|
|
338
|
+
if (this.subscription || !expoFileSystem?.addListener) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.subscription = expoFileSystem.addListener(
|
|
342
|
+
this.getEventName(),
|
|
343
|
+
(event: unknown) => {
|
|
344
|
+
const progressEvent = event as IFileSystemProgressEvent<T>;
|
|
345
|
+
if (progressEvent.uuid === this.uuid) {
|
|
346
|
+
const callback = this.getCallback();
|
|
347
|
+
if (callback) {
|
|
348
|
+
callback(progressEvent.data);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
protected removeSubscription() {
|
|
356
|
+
if (!this.subscription) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
this.subscription.remove();
|
|
360
|
+
this.subscription = null;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export class UploadTask extends FileSystemCancellableNetworkTask<IFileSystemUploadProgressData> {
|
|
365
|
+
private options: IFileSystemUploadOptions;
|
|
366
|
+
|
|
367
|
+
constructor(
|
|
368
|
+
private url: string,
|
|
369
|
+
private fileUri: string,
|
|
370
|
+
options?: IFileSystemUploadOptions,
|
|
371
|
+
private callback?: IFileSystemNetworkTaskProgressCallback<IFileSystemUploadProgressData>,
|
|
372
|
+
) {
|
|
373
|
+
super();
|
|
374
|
+
|
|
375
|
+
const httpMethod = (options?.httpMethod?.toUpperCase() ||
|
|
376
|
+
'POST') as IFileSystemAcceptedUploadHttpMethod;
|
|
377
|
+
|
|
378
|
+
this.options = {
|
|
379
|
+
sessionType: FileSystemSessionType.BACKGROUND,
|
|
380
|
+
uploadType: FileSystemUploadType.BINARY_CONTENT,
|
|
381
|
+
...options,
|
|
382
|
+
httpMethod,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
protected getEventName(): string {
|
|
387
|
+
return 'expo-file-system.uploadProgress';
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
protected getCallback():
|
|
391
|
+
| IFileSystemNetworkTaskProgressCallback<IFileSystemUploadProgressData>
|
|
392
|
+
| undefined {
|
|
393
|
+
return this.callback;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
public async uploadAsync(): Promise<
|
|
397
|
+
IFileSystemUploadResult | undefined | null
|
|
398
|
+
> {
|
|
399
|
+
if (!expoFileSystem?.uploadTaskStartAsync) {
|
|
400
|
+
throw new UnavailabilityError(
|
|
401
|
+
'@symbiote-native/file-system',
|
|
402
|
+
'uploadTaskStartAsync',
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (this.isTaskCancelled()) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
this.addSubscription();
|
|
409
|
+
const result = await expoFileSystem.uploadTaskStartAsync(
|
|
410
|
+
this.url,
|
|
411
|
+
this.fileUri,
|
|
412
|
+
this.uuid,
|
|
413
|
+
this.options,
|
|
414
|
+
);
|
|
415
|
+
this.removeSubscription();
|
|
416
|
+
return result;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export class DownloadResumable extends FileSystemCancellableNetworkTask<IFileSystemDownloadProgressData> {
|
|
421
|
+
constructor(
|
|
422
|
+
private url: string,
|
|
423
|
+
private _fileUri: string,
|
|
424
|
+
private options: IFileSystemDownloadOptions = {},
|
|
425
|
+
private callback?: IFileSystemNetworkTaskProgressCallback<IFileSystemDownloadProgressData>,
|
|
426
|
+
private resumeData?: string,
|
|
427
|
+
) {
|
|
428
|
+
super();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
public get fileUri(): string {
|
|
432
|
+
return this._fileUri;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
protected getEventName(): string {
|
|
436
|
+
return 'expo-file-system.downloadProgress';
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
protected getCallback():
|
|
440
|
+
| IFileSystemNetworkTaskProgressCallback<IFileSystemDownloadProgressData>
|
|
441
|
+
| undefined {
|
|
442
|
+
return this.callback;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Download the contents at a remote URI to a file in the app's file system.
|
|
447
|
+
*/
|
|
448
|
+
async downloadAsync(): Promise<IFileSystemDownloadResult | undefined> {
|
|
449
|
+
if (!expoFileSystem?.downloadResumableStartAsync) {
|
|
450
|
+
throw new UnavailabilityError(
|
|
451
|
+
'@symbiote-native/file-system',
|
|
452
|
+
'downloadResumableStartAsync',
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
if (this.isTaskCancelled()) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
this.addSubscription();
|
|
459
|
+
return await expoFileSystem.downloadResumableStartAsync(
|
|
460
|
+
this.url,
|
|
461
|
+
this._fileUri,
|
|
462
|
+
this.uuid,
|
|
463
|
+
this.options,
|
|
464
|
+
this.resumeData,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Pause the current download operation. `resumeData` is added to the `DownloadResumable`
|
|
470
|
+
* object after a successful pause operation. Returns an object that can be persisted
|
|
471
|
+
* (e.g. via a storage module) for future retrieval.
|
|
472
|
+
*/
|
|
473
|
+
async pauseAsync(): Promise<IFileSystemDownloadPauseState> {
|
|
474
|
+
if (!expoFileSystem?.downloadResumablePauseAsync) {
|
|
475
|
+
throw new UnavailabilityError(
|
|
476
|
+
'@symbiote-native/file-system',
|
|
477
|
+
'downloadResumablePauseAsync',
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (this.isTaskCancelled()) {
|
|
482
|
+
return {
|
|
483
|
+
fileUri: this._fileUri,
|
|
484
|
+
options: this.options,
|
|
485
|
+
url: this.url,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const pauseResult = await expoFileSystem.downloadResumablePauseAsync(
|
|
490
|
+
this.uuid,
|
|
491
|
+
);
|
|
492
|
+
this.removeSubscription();
|
|
493
|
+
if (pauseResult) {
|
|
494
|
+
this.resumeData = pauseResult.resumeData;
|
|
495
|
+
return this.savable();
|
|
496
|
+
}
|
|
497
|
+
throw new Error('Unable to generate a savable pause state');
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Resume a paused download operation.
|
|
502
|
+
*/
|
|
503
|
+
async resumeAsync(): Promise<IFileSystemDownloadResult | undefined> {
|
|
504
|
+
if (!expoFileSystem?.downloadResumableStartAsync) {
|
|
505
|
+
throw new UnavailabilityError(
|
|
506
|
+
'@symbiote-native/file-system',
|
|
507
|
+
'downloadResumableStartAsync',
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
if (this.isTaskCancelled()) {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
this.addSubscription();
|
|
514
|
+
return await expoFileSystem.downloadResumableStartAsync(
|
|
515
|
+
this.url,
|
|
516
|
+
this.fileUri,
|
|
517
|
+
this.uuid,
|
|
518
|
+
this.options,
|
|
519
|
+
this.resumeData,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* The object which can be persisted for future retrieval (e.g. via a storage module), and
|
|
525
|
+
* fed back into `createDownloadResumable` after an app restart.
|
|
526
|
+
*/
|
|
527
|
+
savable(): IFileSystemDownloadPauseState {
|
|
528
|
+
return {
|
|
529
|
+
url: this.url,
|
|
530
|
+
fileUri: this.fileUri,
|
|
531
|
+
options: this.options,
|
|
532
|
+
resumeData: this.resumeData,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const baseReadAsStringAsync = readAsStringAsync;
|
|
538
|
+
const baseWriteAsStringAsync = writeAsStringAsync;
|
|
539
|
+
const baseDeleteAsync = deleteAsync;
|
|
540
|
+
const baseMoveAsync = moveAsync;
|
|
541
|
+
const baseCopyAsync = copyAsync;
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* `StorageAccessFramework` encapsulates the functions usable with SAF URIs, for accessing
|
|
545
|
+
* user-selected directories outside the app's own storage.
|
|
546
|
+
* @platform android
|
|
547
|
+
*/
|
|
548
|
+
export const StorageAccessFramework = {
|
|
549
|
+
/**
|
|
550
|
+
* Gets a SAF URI pointing to a folder in the Android root directory, given the folder name.
|
|
551
|
+
* @platform android
|
|
552
|
+
*/
|
|
553
|
+
getUriForDirectoryInRoot(folderName: string): string {
|
|
554
|
+
return `content://com.android.externalstorage.documents/tree/primary:${folderName}/document/primary:${folderName}`;
|
|
555
|
+
},
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Allows users to select a specific directory, granting the app access to all of the files and
|
|
559
|
+
* sub-directories within it.
|
|
560
|
+
* @platform android 11+
|
|
561
|
+
*/
|
|
562
|
+
async requestDirectoryPermissionsAsync(
|
|
563
|
+
initialFileUrl: string | null = null,
|
|
564
|
+
): Promise<IFileSystemRequestDirectoryPermissionsResult> {
|
|
565
|
+
if (!expoFileSystem?.requestDirectoryPermissionsAsync) {
|
|
566
|
+
throw new UnavailabilityError(
|
|
567
|
+
'@symbiote-native/file-system',
|
|
568
|
+
'StorageAccessFramework.requestDirectoryPermissionsAsync',
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
return await expoFileSystem.requestDirectoryPermissionsAsync(
|
|
572
|
+
initialFileUrl,
|
|
573
|
+
);
|
|
574
|
+
},
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Enumerate the contents of a directory addressed by a SAF URI.
|
|
578
|
+
* @platform android
|
|
579
|
+
*/
|
|
580
|
+
async readDirectoryAsync(dirUri: string): Promise<string[]> {
|
|
581
|
+
if (!expoFileSystem?.readSAFDirectoryAsync) {
|
|
582
|
+
throw new UnavailabilityError(
|
|
583
|
+
'@symbiote-native/file-system',
|
|
584
|
+
'StorageAccessFramework.readDirectoryAsync',
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return await expoFileSystem.readSAFDirectoryAsync(dirUri);
|
|
588
|
+
},
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Creates a new empty directory addressed by a SAF URI.
|
|
592
|
+
* @platform android
|
|
593
|
+
*/
|
|
594
|
+
async makeDirectoryAsync(
|
|
595
|
+
parentUri: string,
|
|
596
|
+
dirName: string,
|
|
597
|
+
): Promise<string> {
|
|
598
|
+
if (!expoFileSystem?.makeSAFDirectoryAsync) {
|
|
599
|
+
throw new UnavailabilityError(
|
|
600
|
+
'@symbiote-native/file-system',
|
|
601
|
+
'StorageAccessFramework.makeDirectoryAsync',
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
return await expoFileSystem.makeSAFDirectoryAsync(parentUri, dirName);
|
|
605
|
+
},
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Creates a new empty file addressed by a SAF URI.
|
|
609
|
+
* @platform android
|
|
610
|
+
*/
|
|
611
|
+
async createFileAsync(
|
|
612
|
+
parentUri: string,
|
|
613
|
+
fileName: string,
|
|
614
|
+
mimeType: string,
|
|
615
|
+
): Promise<string> {
|
|
616
|
+
if (!expoFileSystem?.createSAFFileAsync) {
|
|
617
|
+
throw new UnavailabilityError(
|
|
618
|
+
'@symbiote-native/file-system',
|
|
619
|
+
'StorageAccessFramework.createFileAsync',
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
return await expoFileSystem.createSAFFileAsync(
|
|
623
|
+
parentUri,
|
|
624
|
+
fileName,
|
|
625
|
+
mimeType,
|
|
626
|
+
);
|
|
627
|
+
},
|
|
628
|
+
|
|
629
|
+
writeAsStringAsync: baseWriteAsStringAsync,
|
|
630
|
+
readAsStringAsync: baseReadAsStringAsync,
|
|
631
|
+
deleteAsync: baseDeleteAsync,
|
|
632
|
+
moveAsync: baseMoveAsync,
|
|
633
|
+
copyAsync: baseCopyAsync,
|
|
634
|
+
};
|