1 | import { EventEmitter, UnavailabilityError } from '@unimodules/core';
|
2 | import { Platform } from 'react-native';
|
3 | import { v4 as uuidv4 } from 'uuid';
|
4 | import ExponentFileSystem from './ExponentFileSystem';
|
5 | import { EncodingType, FileSystemSessionType, FileSystemUploadType, } from './FileSystem.types';
|
6 | if (!ExponentFileSystem) {
|
7 | console.warn("No native ExponentFileSystem module found, are you sure the expo-file-system's module is linked properly?");
|
8 | }
|
9 |
|
10 | const _unused = new EventEmitter(ExponentFileSystem);
|
11 | export { EncodingType, FileSystemSessionType, FileSystemUploadType, };
|
12 | function normalizeEndingSlash(p) {
|
13 | if (p != null) {
|
14 | return p.replace(/\/*$/, '') + '/';
|
15 | }
|
16 | return null;
|
17 | }
|
18 | export const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory);
|
19 | export const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory);
|
20 | export const { bundledAssets, bundleDirectory } = ExponentFileSystem;
|
21 | export async function getInfoAsync(fileUri, options = {}) {
|
22 | if (!ExponentFileSystem.getInfoAsync) {
|
23 | throw new UnavailabilityError('expo-file-system', 'getInfoAsync');
|
24 | }
|
25 | return await ExponentFileSystem.getInfoAsync(fileUri, options);
|
26 | }
|
27 | export async function readAsStringAsync(fileUri, options) {
|
28 | if (!ExponentFileSystem.readAsStringAsync) {
|
29 | throw new UnavailabilityError('expo-file-system', 'readAsStringAsync');
|
30 | }
|
31 | return await ExponentFileSystem.readAsStringAsync(fileUri, options || {});
|
32 | }
|
33 | export async function getContentUriAsync(fileUri) {
|
34 | if (Platform.OS === 'android') {
|
35 | if (!ExponentFileSystem.getContentUriAsync) {
|
36 | throw new UnavailabilityError('expo-file-system', 'getContentUriAsync');
|
37 | }
|
38 | return await ExponentFileSystem.getContentUriAsync(fileUri);
|
39 | }
|
40 | else {
|
41 | return new Promise(function (resolve, reject) {
|
42 | resolve(fileUri);
|
43 | });
|
44 | }
|
45 | }
|
46 | export async function writeAsStringAsync(fileUri, contents, options = {}) {
|
47 | if (!ExponentFileSystem.writeAsStringAsync) {
|
48 | throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
|
49 | }
|
50 | return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
|
51 | }
|
52 | export async function deleteAsync(fileUri, options = {}) {
|
53 | if (!ExponentFileSystem.deleteAsync) {
|
54 | throw new UnavailabilityError('expo-file-system', 'deleteAsync');
|
55 | }
|
56 | return await ExponentFileSystem.deleteAsync(fileUri, options);
|
57 | }
|
58 | export async function deleteLegacyDocumentDirectoryAndroid() {
|
59 | if (Platform.OS !== 'android' || documentDirectory == null) {
|
60 | return;
|
61 | }
|
62 | const legacyDocumentDirectory = `${documentDirectory}ExperienceData/`;
|
63 | return await deleteAsync(legacyDocumentDirectory, { idempotent: true });
|
64 | }
|
65 | export async function moveAsync(options) {
|
66 | if (!ExponentFileSystem.moveAsync) {
|
67 | throw new UnavailabilityError('expo-file-system', 'moveAsync');
|
68 | }
|
69 | return await ExponentFileSystem.moveAsync(options);
|
70 | }
|
71 | export async function copyAsync(options) {
|
72 | if (!ExponentFileSystem.copyAsync) {
|
73 | throw new UnavailabilityError('expo-file-system', 'copyAsync');
|
74 | }
|
75 | return await ExponentFileSystem.copyAsync(options);
|
76 | }
|
77 | export async function makeDirectoryAsync(fileUri, options = {}) {
|
78 | if (!ExponentFileSystem.makeDirectoryAsync) {
|
79 | throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
|
80 | }
|
81 | return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
|
82 | }
|
83 | export async function readDirectoryAsync(fileUri) {
|
84 | if (!ExponentFileSystem.readDirectoryAsync) {
|
85 | throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
|
86 | }
|
87 | return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
|
88 | }
|
89 | export async function getFreeDiskStorageAsync() {
|
90 | if (!ExponentFileSystem.getFreeDiskStorageAsync) {
|
91 | throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync');
|
92 | }
|
93 | return await ExponentFileSystem.getFreeDiskStorageAsync();
|
94 | }
|
95 | export async function getTotalDiskCapacityAsync() {
|
96 | if (!ExponentFileSystem.getTotalDiskCapacityAsync) {
|
97 | throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync');
|
98 | }
|
99 | return await ExponentFileSystem.getTotalDiskCapacityAsync();
|
100 | }
|
101 | export async function downloadAsync(uri, fileUri, options = {}) {
|
102 | if (!ExponentFileSystem.downloadAsync) {
|
103 | throw new UnavailabilityError('expo-file-system', 'downloadAsync');
|
104 | }
|
105 | return await ExponentFileSystem.downloadAsync(uri, fileUri, {
|
106 | sessionType: FileSystemSessionType.BACKGROUND,
|
107 | ...options,
|
108 | });
|
109 | }
|
110 | export async function uploadAsync(url, fileUri, options = {}) {
|
111 | if (!ExponentFileSystem.uploadAsync) {
|
112 | throw new UnavailabilityError('expo-file-system', 'uploadAsync');
|
113 | }
|
114 | return await ExponentFileSystem.uploadAsync(url, fileUri, {
|
115 | sessionType: FileSystemSessionType.BACKGROUND,
|
116 | uploadType: FileSystemUploadType.BINARY_CONTENT,
|
117 | ...options,
|
118 | httpMethod: (options.httpMethod || 'POST').toUpperCase(),
|
119 | });
|
120 | }
|
121 | export function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
|
122 | return new DownloadResumable(uri, fileUri, options, callback, resumeData);
|
123 | }
|
124 | export class DownloadResumable {
|
125 | constructor(url, fileUri, options = {}, callback, resumeData) {
|
126 | this._uuid = uuidv4();
|
127 | this._url = url;
|
128 | this._fileUri = fileUri;
|
129 | this._options = options;
|
130 | this._resumeData = resumeData;
|
131 | this._callback = callback;
|
132 | this._subscription = null;
|
133 | this._emitter = new EventEmitter(ExponentFileSystem);
|
134 | }
|
135 | async downloadAsync() {
|
136 | if (!ExponentFileSystem.downloadResumableStartAsync) {
|
137 | throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
|
138 | }
|
139 | this._addSubscription();
|
140 | return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
|
141 | }
|
142 | async pauseAsync() {
|
143 | if (!ExponentFileSystem.downloadResumablePauseAsync) {
|
144 | throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
|
145 | }
|
146 | const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
|
147 | this._removeSubscription();
|
148 | if (pauseResult) {
|
149 | this._resumeData = pauseResult.resumeData;
|
150 | return this.savable();
|
151 | }
|
152 | else {
|
153 | throw new Error('Unable to generate a savable pause state');
|
154 | }
|
155 | }
|
156 | async resumeAsync() {
|
157 | if (!ExponentFileSystem.downloadResumableStartAsync) {
|
158 | throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
|
159 | }
|
160 | this._addSubscription();
|
161 | return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
|
162 | }
|
163 | savable() {
|
164 | return {
|
165 | url: this._url,
|
166 | fileUri: this._fileUri,
|
167 | options: this._options,
|
168 | resumeData: this._resumeData,
|
169 | };
|
170 | }
|
171 | _addSubscription() {
|
172 | if (this._subscription) {
|
173 | return;
|
174 | }
|
175 | this._subscription = this._emitter.addListener('expo-file-system.downloadProgress', (event) => {
|
176 | if (event.uuid === this._uuid) {
|
177 | const callback = this._callback;
|
178 | if (callback) {
|
179 | callback(event.data);
|
180 | }
|
181 | }
|
182 | });
|
183 | }
|
184 | _removeSubscription() {
|
185 | if (!this._subscription) {
|
186 | return;
|
187 | }
|
188 | this._emitter.removeSubscription(this._subscription);
|
189 | this._subscription = null;
|
190 | }
|
191 | }
|
192 | const baseReadAsStringAsync = readAsStringAsync;
|
193 | const baseWriteAsStringAsync = writeAsStringAsync;
|
194 | const baseDeleteAsync = deleteAsync;
|
195 | const baseMoveAsync = moveAsync;
|
196 | const baseCopyAsync = copyAsync;
|
197 |
|
198 |
|
199 |
|
200 | export var StorageAccessFramework;
|
201 | (function (StorageAccessFramework) {
|
202 | function getUriForDirectoryInRoot(folderName) {
|
203 | return `content://com.android.externalstorage.documents/tree/primary:${folderName}/document/primary:${folderName}`;
|
204 | }
|
205 | StorageAccessFramework.getUriForDirectoryInRoot = getUriForDirectoryInRoot;
|
206 | async function requestDirectoryPermissionsAsync(initialFileUrl = null) {
|
207 | if (!ExponentFileSystem.requestDirectoryPermissionsAsync) {
|
208 | throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.requestDirectoryPermissionsAsync');
|
209 | }
|
210 | return await ExponentFileSystem.requestDirectoryPermissionsAsync(initialFileUrl);
|
211 | }
|
212 | StorageAccessFramework.requestDirectoryPermissionsAsync = requestDirectoryPermissionsAsync;
|
213 | async function readDirectoryAsync(dirUri) {
|
214 | if (!ExponentFileSystem.readSAFDirectoryAsync) {
|
215 | throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.readDirectoryAsync');
|
216 | }
|
217 | return await ExponentFileSystem.readSAFDirectoryAsync(dirUri, {});
|
218 | }
|
219 | StorageAccessFramework.readDirectoryAsync = readDirectoryAsync;
|
220 | async function makeDirectoryAsync(parentUri, dirName) {
|
221 | if (!ExponentFileSystem.makeSAFDirectoryAsync) {
|
222 | throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.makeDirectoryAsync');
|
223 | }
|
224 | return await ExponentFileSystem.makeSAFDirectoryAsync(parentUri, dirName);
|
225 | }
|
226 | StorageAccessFramework.makeDirectoryAsync = makeDirectoryAsync;
|
227 | async function createFileAsync(parentUri, fileName, mimeType) {
|
228 | if (!ExponentFileSystem.createSAFFileAsync) {
|
229 | throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.createFileAsync');
|
230 | }
|
231 | return await ExponentFileSystem.createSAFFileAsync(parentUri, fileName, mimeType);
|
232 | }
|
233 | StorageAccessFramework.createFileAsync = createFileAsync;
|
234 | StorageAccessFramework.writeAsStringAsync = baseWriteAsStringAsync;
|
235 | StorageAccessFramework.readAsStringAsync = baseReadAsStringAsync;
|
236 | StorageAccessFramework.deleteAsync = baseDeleteAsync;
|
237 | StorageAccessFramework.moveAsync = baseMoveAsync;
|
238 | StorageAccessFramework.copyAsync = baseCopyAsync;
|
239 | })(StorageAccessFramework || (StorageAccessFramework = {}));
|
240 |
|
\ | No newline at end of file |