192f6de58SWill Schurmanimport { EventEmitter, UnavailabilityError } from '@unimodules/core';
21da2f189STomasz Sapetaimport { Platform } from 'react-native';
37f896a5fSGiau. Tran Minhimport { v4 as uuidv4 } from 'uuid';
479a5fc63SJames Ideimport ExponentFileSystem from './ExponentFileSystem';
537630a92SŁukasz Kosmatyimport { EncodingType, FileSystemSessionType, FileSystemUploadType, } from './FileSystem.types';
67e929553SEvan Baconif (!ExponentFileSystem) {
72307aaceSEvan Bacon    console.warn("No native ExponentFileSystem module found, are you sure the expo-file-system's module is linked properly?");
82307aaceSEvan Bacon}
98f6e1365SEvan Bacon// Prevent webpack from pruning this.
1079a5fc63SJames Ideconst _unused = new EventEmitter(ExponentFileSystem); // eslint-disable-line
1137630a92SŁukasz Kosmatyexport { EncodingType, FileSystemSessionType, FileSystemUploadType, };
122307aaceSEvan Baconfunction normalizeEndingSlash(p) {
132307aaceSEvan Bacon    if (p != null) {
142307aaceSEvan Bacon        return p.replace(/\/*$/, '') + '/';
152307aaceSEvan Bacon    }
162307aaceSEvan Bacon    return null;
172307aaceSEvan Bacon}
187e929553SEvan Baconexport const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory);
197e929553SEvan Baconexport const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory);
207e929553SEvan Baconexport const { bundledAssets, bundleDirectory } = ExponentFileSystem;
212307aaceSEvan Baconexport async function getInfoAsync(fileUri, options = {}) {
227e929553SEvan Bacon    if (!ExponentFileSystem.getInfoAsync) {
232307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'getInfoAsync');
242307aaceSEvan Bacon    }
257e929553SEvan Bacon    return await ExponentFileSystem.getInfoAsync(fileUri, options);
262307aaceSEvan Bacon}
272307aaceSEvan Baconexport async function readAsStringAsync(fileUri, options) {
287e929553SEvan Bacon    if (!ExponentFileSystem.readAsStringAsync) {
292307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'readAsStringAsync');
302307aaceSEvan Bacon    }
317e929553SEvan Bacon    return await ExponentFileSystem.readAsStringAsync(fileUri, options || {});
322307aaceSEvan Bacon}
33e4897761STongyu Zhuexport async function getContentUriAsync(fileUri) {
341da2f189STomasz Sapeta    if (Platform.OS === 'android') {
35e4897761STongyu Zhu        if (!ExponentFileSystem.getContentUriAsync) {
36e4897761STongyu Zhu            throw new UnavailabilityError('expo-file-system', 'getContentUriAsync');
37e4897761STongyu Zhu        }
38e4897761STongyu Zhu        return await ExponentFileSystem.getContentUriAsync(fileUri);
39e4897761STongyu Zhu    }
401da2f189STomasz Sapeta    else {
411da2f189STomasz Sapeta        return new Promise(function (resolve, reject) {
421da2f189STomasz Sapeta            resolve(fileUri);
431da2f189STomasz Sapeta        });
441da2f189STomasz Sapeta    }
451da2f189STomasz Sapeta}
462307aaceSEvan Baconexport async function writeAsStringAsync(fileUri, contents, options = {}) {
477e929553SEvan Bacon    if (!ExponentFileSystem.writeAsStringAsync) {
482307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
492307aaceSEvan Bacon    }
507e929553SEvan Bacon    return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
512307aaceSEvan Bacon}
522307aaceSEvan Baconexport async function deleteAsync(fileUri, options = {}) {
537e929553SEvan Bacon    if (!ExponentFileSystem.deleteAsync) {
542307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'deleteAsync');
552307aaceSEvan Bacon    }
567e929553SEvan Bacon    return await ExponentFileSystem.deleteAsync(fileUri, options);
572307aaceSEvan Bacon}
580d6db115SEric Samelsonexport async function deleteLegacyDocumentDirectoryAndroid() {
590d6db115SEric Samelson    if (Platform.OS !== 'android' || documentDirectory == null) {
600d6db115SEric Samelson        return;
610d6db115SEric Samelson    }
620d6db115SEric Samelson    const legacyDocumentDirectory = `${documentDirectory}ExperienceData/`;
630d6db115SEric Samelson    return await deleteAsync(legacyDocumentDirectory, { idempotent: true });
640d6db115SEric Samelson}
652307aaceSEvan Baconexport async function moveAsync(options) {
667e929553SEvan Bacon    if (!ExponentFileSystem.moveAsync) {
672307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'moveAsync');
682307aaceSEvan Bacon    }
697e929553SEvan Bacon    return await ExponentFileSystem.moveAsync(options);
702307aaceSEvan Bacon}
712307aaceSEvan Baconexport async function copyAsync(options) {
727e929553SEvan Bacon    if (!ExponentFileSystem.copyAsync) {
732307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'copyAsync');
742307aaceSEvan Bacon    }
757e929553SEvan Bacon    return await ExponentFileSystem.copyAsync(options);
762307aaceSEvan Bacon}
772307aaceSEvan Baconexport async function makeDirectoryAsync(fileUri, options = {}) {
787e929553SEvan Bacon    if (!ExponentFileSystem.makeDirectoryAsync) {
792307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
802307aaceSEvan Bacon    }
817e929553SEvan Bacon    return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
822307aaceSEvan Bacon}
832307aaceSEvan Baconexport async function readDirectoryAsync(fileUri) {
847e929553SEvan Bacon    if (!ExponentFileSystem.readDirectoryAsync) {
852307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
862307aaceSEvan Bacon    }
877e929553SEvan Bacon    return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
882307aaceSEvan Bacon}
89ae9a5f1aSTongyu Zhuexport async function getFreeDiskStorageAsync() {
90ae9a5f1aSTongyu Zhu    if (!ExponentFileSystem.getFreeDiskStorageAsync) {
91ae9a5f1aSTongyu Zhu        throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync');
92ae9a5f1aSTongyu Zhu    }
93ae9a5f1aSTongyu Zhu    return await ExponentFileSystem.getFreeDiskStorageAsync();
94ae9a5f1aSTongyu Zhu}
95ae9a5f1aSTongyu Zhuexport async function getTotalDiskCapacityAsync() {
96ae9a5f1aSTongyu Zhu    if (!ExponentFileSystem.getTotalDiskCapacityAsync) {
97ae9a5f1aSTongyu Zhu        throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync');
98ae9a5f1aSTongyu Zhu    }
99ae9a5f1aSTongyu Zhu    return await ExponentFileSystem.getTotalDiskCapacityAsync();
100ae9a5f1aSTongyu Zhu}
1012307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) {
1027e929553SEvan Bacon    if (!ExponentFileSystem.downloadAsync) {
1032307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
1042307aaceSEvan Bacon    }
105da65a760SŁukasz Kosmaty    return await ExponentFileSystem.downloadAsync(uri, fileUri, {
106da65a760SŁukasz Kosmaty        sessionType: FileSystemSessionType.BACKGROUND,
107da65a760SŁukasz Kosmaty        ...options,
108da65a760SŁukasz Kosmaty    });
109da65a760SŁukasz Kosmaty}
110da65a760SŁukasz Kosmatyexport async function uploadAsync(url, fileUri, options = {}) {
111da65a760SŁukasz Kosmaty    if (!ExponentFileSystem.uploadAsync) {
112da65a760SŁukasz Kosmaty        throw new UnavailabilityError('expo-file-system', 'uploadAsync');
113da65a760SŁukasz Kosmaty    }
114da65a760SŁukasz Kosmaty    return await ExponentFileSystem.uploadAsync(url, fileUri, {
115da65a760SŁukasz Kosmaty        sessionType: FileSystemSessionType.BACKGROUND,
11637630a92SŁukasz Kosmaty        uploadType: FileSystemUploadType.BINARY_CONTENT,
117da65a760SŁukasz Kosmaty        ...options,
118da65a760SŁukasz Kosmaty        httpMethod: (options.httpMethod || 'POST').toUpperCase(),
119da65a760SŁukasz Kosmaty    });
1202307aaceSEvan Bacon}
1212307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
1222307aaceSEvan Bacon    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
1232307aaceSEvan Bacon}
1242307aaceSEvan Baconexport class DownloadResumable {
125*896103abSandy    _uuid;
126*896103abSandy    _url;
127*896103abSandy    _fileUri;
128*896103abSandy    _options;
129*896103abSandy    _resumeData;
130*896103abSandy    _callback;
131*896103abSandy    _subscription;
132*896103abSandy    _emitter;
1332307aaceSEvan Bacon    constructor(url, fileUri, options = {}, callback, resumeData) {
134d38805f3SJames Ide        this._uuid = uuidv4();
1352307aaceSEvan Bacon        this._url = url;
1362307aaceSEvan Bacon        this._fileUri = fileUri;
1372307aaceSEvan Bacon        this._options = options;
1382307aaceSEvan Bacon        this._resumeData = resumeData;
1392307aaceSEvan Bacon        this._callback = callback;
1402307aaceSEvan Bacon        this._subscription = null;
1417e929553SEvan Bacon        this._emitter = new EventEmitter(ExponentFileSystem);
1422307aaceSEvan Bacon    }
1432307aaceSEvan Bacon    async downloadAsync() {
1447e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
1452307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
1462307aaceSEvan Bacon        }
1472307aaceSEvan Bacon        this._addSubscription();
1487e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
1492307aaceSEvan Bacon    }
1502307aaceSEvan Bacon    async pauseAsync() {
1517e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumablePauseAsync) {
1522307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
1532307aaceSEvan Bacon        }
1547e929553SEvan Bacon        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
1552307aaceSEvan Bacon        this._removeSubscription();
1562307aaceSEvan Bacon        if (pauseResult) {
1572307aaceSEvan Bacon            this._resumeData = pauseResult.resumeData;
1582307aaceSEvan Bacon            return this.savable();
1592307aaceSEvan Bacon        }
1602307aaceSEvan Bacon        else {
1612307aaceSEvan Bacon            throw new Error('Unable to generate a savable pause state');
1622307aaceSEvan Bacon        }
1632307aaceSEvan Bacon    }
1642307aaceSEvan Bacon    async resumeAsync() {
1657e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
1662307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
1672307aaceSEvan Bacon        }
1682307aaceSEvan Bacon        this._addSubscription();
1697e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
1702307aaceSEvan Bacon    }
1712307aaceSEvan Bacon    savable() {
1722307aaceSEvan Bacon        return {
1732307aaceSEvan Bacon            url: this._url,
1742307aaceSEvan Bacon            fileUri: this._fileUri,
1752307aaceSEvan Bacon            options: this._options,
1762307aaceSEvan Bacon            resumeData: this._resumeData,
1772307aaceSEvan Bacon        };
1782307aaceSEvan Bacon    }
1792307aaceSEvan Bacon    _addSubscription() {
1802307aaceSEvan Bacon        if (this._subscription) {
1812307aaceSEvan Bacon            return;
1822307aaceSEvan Bacon        }
183da65a760SŁukasz Kosmaty        this._subscription = this._emitter.addListener('expo-file-system.downloadProgress', (event) => {
1842307aaceSEvan Bacon            if (event.uuid === this._uuid) {
1852307aaceSEvan Bacon                const callback = this._callback;
1862307aaceSEvan Bacon                if (callback) {
1872307aaceSEvan Bacon                    callback(event.data);
1882307aaceSEvan Bacon                }
1892307aaceSEvan Bacon            }
1902307aaceSEvan Bacon        });
1912307aaceSEvan Bacon    }
1922307aaceSEvan Bacon    _removeSubscription() {
1932307aaceSEvan Bacon        if (!this._subscription) {
1942307aaceSEvan Bacon            return;
1952307aaceSEvan Bacon        }
1962307aaceSEvan Bacon        this._emitter.removeSubscription(this._subscription);
1972307aaceSEvan Bacon        this._subscription = null;
1982307aaceSEvan Bacon    }
1992307aaceSEvan Bacon}
200400de723SŁukasz Kosmatyconst baseReadAsStringAsync = readAsStringAsync;
201400de723SŁukasz Kosmatyconst baseWriteAsStringAsync = writeAsStringAsync;
202400de723SŁukasz Kosmatyconst baseDeleteAsync = deleteAsync;
203400de723SŁukasz Kosmatyconst baseMoveAsync = moveAsync;
204400de723SŁukasz Kosmatyconst baseCopyAsync = copyAsync;
205400de723SŁukasz Kosmaty/**
206400de723SŁukasz Kosmaty * Android only
207400de723SŁukasz Kosmaty */
208400de723SŁukasz Kosmatyexport var StorageAccessFramework;
209400de723SŁukasz Kosmaty(function (StorageAccessFramework) {
210400de723SŁukasz Kosmaty    function getUriForDirectoryInRoot(folderName) {
211400de723SŁukasz Kosmaty        return `content://com.android.externalstorage.documents/tree/primary:${folderName}/document/primary:${folderName}`;
212400de723SŁukasz Kosmaty    }
213400de723SŁukasz Kosmaty    StorageAccessFramework.getUriForDirectoryInRoot = getUriForDirectoryInRoot;
214400de723SŁukasz Kosmaty    async function requestDirectoryPermissionsAsync(initialFileUrl = null) {
215400de723SŁukasz Kosmaty        if (!ExponentFileSystem.requestDirectoryPermissionsAsync) {
216400de723SŁukasz Kosmaty            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.requestDirectoryPermissionsAsync');
217400de723SŁukasz Kosmaty        }
218400de723SŁukasz Kosmaty        return await ExponentFileSystem.requestDirectoryPermissionsAsync(initialFileUrl);
219400de723SŁukasz Kosmaty    }
220400de723SŁukasz Kosmaty    StorageAccessFramework.requestDirectoryPermissionsAsync = requestDirectoryPermissionsAsync;
221400de723SŁukasz Kosmaty    async function readDirectoryAsync(dirUri) {
222400de723SŁukasz Kosmaty        if (!ExponentFileSystem.readSAFDirectoryAsync) {
223400de723SŁukasz Kosmaty            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.readDirectoryAsync');
224400de723SŁukasz Kosmaty        }
225400de723SŁukasz Kosmaty        return await ExponentFileSystem.readSAFDirectoryAsync(dirUri, {});
226400de723SŁukasz Kosmaty    }
227400de723SŁukasz Kosmaty    StorageAccessFramework.readDirectoryAsync = readDirectoryAsync;
228400de723SŁukasz Kosmaty    async function makeDirectoryAsync(parentUri, dirName) {
229400de723SŁukasz Kosmaty        if (!ExponentFileSystem.makeSAFDirectoryAsync) {
230400de723SŁukasz Kosmaty            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.makeDirectoryAsync');
231400de723SŁukasz Kosmaty        }
232400de723SŁukasz Kosmaty        return await ExponentFileSystem.makeSAFDirectoryAsync(parentUri, dirName);
233400de723SŁukasz Kosmaty    }
234400de723SŁukasz Kosmaty    StorageAccessFramework.makeDirectoryAsync = makeDirectoryAsync;
235400de723SŁukasz Kosmaty    async function createFileAsync(parentUri, fileName, mimeType) {
236400de723SŁukasz Kosmaty        if (!ExponentFileSystem.createSAFFileAsync) {
237400de723SŁukasz Kosmaty            throw new UnavailabilityError('expo-file-system', 'StorageAccessFramework.createFileAsync');
238400de723SŁukasz Kosmaty        }
239400de723SŁukasz Kosmaty        return await ExponentFileSystem.createSAFFileAsync(parentUri, fileName, mimeType);
240400de723SŁukasz Kosmaty    }
241400de723SŁukasz Kosmaty    StorageAccessFramework.createFileAsync = createFileAsync;
242400de723SŁukasz Kosmaty    StorageAccessFramework.writeAsStringAsync = baseWriteAsStringAsync;
243400de723SŁukasz Kosmaty    StorageAccessFramework.readAsStringAsync = baseReadAsStringAsync;
244400de723SŁukasz Kosmaty    StorageAccessFramework.deleteAsync = baseDeleteAsync;
245400de723SŁukasz Kosmaty    StorageAccessFramework.moveAsync = baseMoveAsync;
246400de723SŁukasz Kosmaty    StorageAccessFramework.copyAsync = baseCopyAsync;
247400de723SŁukasz Kosmaty})(StorageAccessFramework || (StorageAccessFramework = {}));
2482307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map