18d441c7eSStanisław Chmielaimport { UnavailabilityError } from '@unimodules/core';
28d441c7eSStanisław Chmielaimport { EventEmitter } from '@unimodules/core';
32307aaceSEvan Baconimport UUID from 'uuid-js';
47e929553SEvan Baconimport ExponentFileSystem from './ExponentFileSystem';
52307aaceSEvan Baconimport { EncodingType, } 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.
108d441c7eSStanisław Chmielaconst _unused = new EventEmitter(ExponentFileSystem);
112307aaceSEvan Baconexport { EncodingType, };
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}
33*e4897761STongyu Zhuexport async function getContentUriAsync(fileUri) {
34*e4897761STongyu Zhu    if (!ExponentFileSystem.getContentUriAsync) {
35*e4897761STongyu Zhu        throw new UnavailabilityError('expo-file-system', 'getContentUriAsync');
36*e4897761STongyu Zhu    }
37*e4897761STongyu Zhu    return await ExponentFileSystem.getContentUriAsync(fileUri);
38*e4897761STongyu Zhu}
392307aaceSEvan Baconexport async function writeAsStringAsync(fileUri, contents, options = {}) {
407e929553SEvan Bacon    if (!ExponentFileSystem.writeAsStringAsync) {
412307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
422307aaceSEvan Bacon    }
437e929553SEvan Bacon    return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
442307aaceSEvan Bacon}
452307aaceSEvan Baconexport async function deleteAsync(fileUri, options = {}) {
467e929553SEvan Bacon    if (!ExponentFileSystem.deleteAsync) {
472307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'deleteAsync');
482307aaceSEvan Bacon    }
497e929553SEvan Bacon    return await ExponentFileSystem.deleteAsync(fileUri, options);
502307aaceSEvan Bacon}
512307aaceSEvan Baconexport async function moveAsync(options) {
527e929553SEvan Bacon    if (!ExponentFileSystem.moveAsync) {
532307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'moveAsync');
542307aaceSEvan Bacon    }
557e929553SEvan Bacon    return await ExponentFileSystem.moveAsync(options);
562307aaceSEvan Bacon}
572307aaceSEvan Baconexport async function copyAsync(options) {
587e929553SEvan Bacon    if (!ExponentFileSystem.copyAsync) {
592307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'copyAsync');
602307aaceSEvan Bacon    }
617e929553SEvan Bacon    return await ExponentFileSystem.copyAsync(options);
622307aaceSEvan Bacon}
632307aaceSEvan Baconexport async function makeDirectoryAsync(fileUri, options = {}) {
647e929553SEvan Bacon    if (!ExponentFileSystem.makeDirectoryAsync) {
652307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
662307aaceSEvan Bacon    }
677e929553SEvan Bacon    return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
682307aaceSEvan Bacon}
692307aaceSEvan Baconexport async function readDirectoryAsync(fileUri) {
707e929553SEvan Bacon    if (!ExponentFileSystem.readDirectoryAsync) {
712307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
722307aaceSEvan Bacon    }
737e929553SEvan Bacon    return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
742307aaceSEvan Bacon}
752307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) {
767e929553SEvan Bacon    if (!ExponentFileSystem.downloadAsync) {
772307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
782307aaceSEvan Bacon    }
797e929553SEvan Bacon    return await ExponentFileSystem.downloadAsync(uri, fileUri, options);
802307aaceSEvan Bacon}
812307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
822307aaceSEvan Bacon    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
832307aaceSEvan Bacon}
842307aaceSEvan Baconexport class DownloadResumable {
852307aaceSEvan Bacon    constructor(url, fileUri, options = {}, callback, resumeData) {
862307aaceSEvan Bacon        this._uuid = UUID.create(4).toString();
872307aaceSEvan Bacon        this._url = url;
882307aaceSEvan Bacon        this._fileUri = fileUri;
892307aaceSEvan Bacon        this._options = options;
902307aaceSEvan Bacon        this._resumeData = resumeData;
912307aaceSEvan Bacon        this._callback = callback;
922307aaceSEvan Bacon        this._subscription = null;
937e929553SEvan Bacon        this._emitter = new EventEmitter(ExponentFileSystem);
942307aaceSEvan Bacon    }
952307aaceSEvan Bacon    async downloadAsync() {
967e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
972307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
982307aaceSEvan Bacon        }
992307aaceSEvan Bacon        this._addSubscription();
1007e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
1012307aaceSEvan Bacon    }
1022307aaceSEvan Bacon    async pauseAsync() {
1037e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumablePauseAsync) {
1042307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
1052307aaceSEvan Bacon        }
1067e929553SEvan Bacon        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
1072307aaceSEvan Bacon        this._removeSubscription();
1082307aaceSEvan Bacon        if (pauseResult) {
1092307aaceSEvan Bacon            this._resumeData = pauseResult.resumeData;
1102307aaceSEvan Bacon            return this.savable();
1112307aaceSEvan Bacon        }
1122307aaceSEvan Bacon        else {
1132307aaceSEvan Bacon            throw new Error('Unable to generate a savable pause state');
1142307aaceSEvan Bacon        }
1152307aaceSEvan Bacon    }
1162307aaceSEvan Bacon    async resumeAsync() {
1177e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
1182307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
1192307aaceSEvan Bacon        }
1202307aaceSEvan Bacon        this._addSubscription();
1217e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
1222307aaceSEvan Bacon    }
1232307aaceSEvan Bacon    savable() {
1242307aaceSEvan Bacon        return {
1252307aaceSEvan Bacon            url: this._url,
1262307aaceSEvan Bacon            fileUri: this._fileUri,
1272307aaceSEvan Bacon            options: this._options,
1282307aaceSEvan Bacon            resumeData: this._resumeData,
1292307aaceSEvan Bacon        };
1302307aaceSEvan Bacon    }
1312307aaceSEvan Bacon    _addSubscription() {
1322307aaceSEvan Bacon        if (this._subscription) {
1332307aaceSEvan Bacon            return;
1342307aaceSEvan Bacon        }
1352307aaceSEvan Bacon        this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => {
1362307aaceSEvan Bacon            if (event.uuid === this._uuid) {
1372307aaceSEvan Bacon                const callback = this._callback;
1382307aaceSEvan Bacon                if (callback) {
1392307aaceSEvan Bacon                    callback(event.data);
1402307aaceSEvan Bacon                }
1412307aaceSEvan Bacon            }
1422307aaceSEvan Bacon        });
1432307aaceSEvan Bacon    }
1442307aaceSEvan Bacon    _removeSubscription() {
1452307aaceSEvan Bacon        if (!this._subscription) {
1462307aaceSEvan Bacon            return;
1472307aaceSEvan Bacon        }
1482307aaceSEvan Bacon        this._emitter.removeSubscription(this._subscription);
1492307aaceSEvan Bacon        this._subscription = null;
1502307aaceSEvan Bacon    }
1512307aaceSEvan Bacon}
1522307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map