12307aaceSEvan Baconimport { UnavailabilityError } from 'expo-errors';
22307aaceSEvan Baconimport { EventEmitter } from 'expo-core';
32307aaceSEvan Baconimport UUID from 'uuid-js';
4*7e929553SEvan Baconimport ExponentFileSystem from './ExponentFileSystem';
52307aaceSEvan Baconimport { EncodingType, } from './FileSystem.types';
6*7e929553SEvan 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}
92307aaceSEvan Baconexport { EncodingType, };
102307aaceSEvan Baconfunction normalizeEndingSlash(p) {
112307aaceSEvan Bacon    if (p != null) {
122307aaceSEvan Bacon        return p.replace(/\/*$/, '') + '/';
132307aaceSEvan Bacon    }
142307aaceSEvan Bacon    return null;
152307aaceSEvan Bacon}
16*7e929553SEvan Baconexport const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory);
17*7e929553SEvan Baconexport const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory);
18*7e929553SEvan Baconexport const { bundledAssets, bundleDirectory } = ExponentFileSystem;
192307aaceSEvan Baconexport async function getInfoAsync(fileUri, options = {}) {
20*7e929553SEvan Bacon    if (!ExponentFileSystem.getInfoAsync) {
212307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'getInfoAsync');
222307aaceSEvan Bacon    }
23*7e929553SEvan Bacon    return await ExponentFileSystem.getInfoAsync(fileUri, options);
242307aaceSEvan Bacon}
252307aaceSEvan Baconexport async function readAsStringAsync(fileUri, options) {
26*7e929553SEvan Bacon    if (!ExponentFileSystem.readAsStringAsync) {
272307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'readAsStringAsync');
282307aaceSEvan Bacon    }
29*7e929553SEvan Bacon    return await ExponentFileSystem.readAsStringAsync(fileUri, options || {});
302307aaceSEvan Bacon}
312307aaceSEvan Baconexport async function writeAsStringAsync(fileUri, contents, options = {}) {
32*7e929553SEvan Bacon    if (!ExponentFileSystem.writeAsStringAsync) {
332307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync');
342307aaceSEvan Bacon    }
35*7e929553SEvan Bacon    return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options);
362307aaceSEvan Bacon}
372307aaceSEvan Baconexport async function deleteAsync(fileUri, options = {}) {
38*7e929553SEvan Bacon    if (!ExponentFileSystem.deleteAsync) {
392307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'deleteAsync');
402307aaceSEvan Bacon    }
41*7e929553SEvan Bacon    return await ExponentFileSystem.deleteAsync(fileUri, options);
422307aaceSEvan Bacon}
432307aaceSEvan Baconexport async function moveAsync(options) {
44*7e929553SEvan Bacon    if (!ExponentFileSystem.moveAsync) {
452307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'moveAsync');
462307aaceSEvan Bacon    }
47*7e929553SEvan Bacon    return await ExponentFileSystem.moveAsync(options);
482307aaceSEvan Bacon}
492307aaceSEvan Baconexport async function copyAsync(options) {
50*7e929553SEvan Bacon    if (!ExponentFileSystem.copyAsync) {
512307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'copyAsync');
522307aaceSEvan Bacon    }
53*7e929553SEvan Bacon    return await ExponentFileSystem.copyAsync(options);
542307aaceSEvan Bacon}
552307aaceSEvan Baconexport async function makeDirectoryAsync(fileUri, options = {}) {
56*7e929553SEvan Bacon    if (!ExponentFileSystem.makeDirectoryAsync) {
572307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync');
582307aaceSEvan Bacon    }
59*7e929553SEvan Bacon    return await ExponentFileSystem.makeDirectoryAsync(fileUri, options);
602307aaceSEvan Bacon}
612307aaceSEvan Baconexport async function readDirectoryAsync(fileUri) {
62*7e929553SEvan Bacon    if (!ExponentFileSystem.readDirectoryAsync) {
632307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync');
642307aaceSEvan Bacon    }
65*7e929553SEvan Bacon    return await ExponentFileSystem.readDirectoryAsync(fileUri, {});
662307aaceSEvan Bacon}
672307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) {
68*7e929553SEvan Bacon    if (!ExponentFileSystem.downloadAsync) {
692307aaceSEvan Bacon        throw new UnavailabilityError('expo-file-system', 'downloadAsync');
702307aaceSEvan Bacon    }
71*7e929553SEvan Bacon    return await ExponentFileSystem.downloadAsync(uri, fileUri, options);
722307aaceSEvan Bacon}
732307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) {
742307aaceSEvan Bacon    return new DownloadResumable(uri, fileUri, options, callback, resumeData);
752307aaceSEvan Bacon}
762307aaceSEvan Baconexport class DownloadResumable {
772307aaceSEvan Bacon    constructor(url, fileUri, options = {}, callback, resumeData) {
782307aaceSEvan Bacon        this._uuid = UUID.create(4).toString();
792307aaceSEvan Bacon        this._url = url;
802307aaceSEvan Bacon        this._fileUri = fileUri;
812307aaceSEvan Bacon        this._options = options;
822307aaceSEvan Bacon        this._resumeData = resumeData;
832307aaceSEvan Bacon        this._callback = callback;
842307aaceSEvan Bacon        this._subscription = null;
85*7e929553SEvan Bacon        this._emitter = new EventEmitter(ExponentFileSystem);
862307aaceSEvan Bacon    }
872307aaceSEvan Bacon    async downloadAsync() {
88*7e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
892307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
902307aaceSEvan Bacon        }
912307aaceSEvan Bacon        this._addSubscription();
92*7e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
932307aaceSEvan Bacon    }
942307aaceSEvan Bacon    async pauseAsync() {
95*7e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumablePauseAsync) {
962307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync');
972307aaceSEvan Bacon        }
98*7e929553SEvan Bacon        const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid);
992307aaceSEvan Bacon        this._removeSubscription();
1002307aaceSEvan Bacon        if (pauseResult) {
1012307aaceSEvan Bacon            this._resumeData = pauseResult.resumeData;
1022307aaceSEvan Bacon            return this.savable();
1032307aaceSEvan Bacon        }
1042307aaceSEvan Bacon        else {
1052307aaceSEvan Bacon            throw new Error('Unable to generate a savable pause state');
1062307aaceSEvan Bacon        }
1072307aaceSEvan Bacon    }
1082307aaceSEvan Bacon    async resumeAsync() {
109*7e929553SEvan Bacon        if (!ExponentFileSystem.downloadResumableStartAsync) {
1102307aaceSEvan Bacon            throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync');
1112307aaceSEvan Bacon        }
1122307aaceSEvan Bacon        this._addSubscription();
113*7e929553SEvan Bacon        return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData);
1142307aaceSEvan Bacon    }
1152307aaceSEvan Bacon    savable() {
1162307aaceSEvan Bacon        return {
1172307aaceSEvan Bacon            url: this._url,
1182307aaceSEvan Bacon            fileUri: this._fileUri,
1192307aaceSEvan Bacon            options: this._options,
1202307aaceSEvan Bacon            resumeData: this._resumeData,
1212307aaceSEvan Bacon        };
1222307aaceSEvan Bacon    }
1232307aaceSEvan Bacon    _addSubscription() {
1242307aaceSEvan Bacon        if (this._subscription) {
1252307aaceSEvan Bacon            return;
1262307aaceSEvan Bacon        }
1272307aaceSEvan Bacon        this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => {
1282307aaceSEvan Bacon            if (event.uuid === this._uuid) {
1292307aaceSEvan Bacon                const callback = this._callback;
1302307aaceSEvan Bacon                if (callback) {
1312307aaceSEvan Bacon                    callback(event.data);
1322307aaceSEvan Bacon                }
1332307aaceSEvan Bacon            }
1342307aaceSEvan Bacon        });
1352307aaceSEvan Bacon    }
1362307aaceSEvan Bacon    _removeSubscription() {
1372307aaceSEvan Bacon        if (!this._subscription) {
1382307aaceSEvan Bacon            return;
1392307aaceSEvan Bacon        }
1402307aaceSEvan Bacon        this._emitter.removeSubscription(this._subscription);
1412307aaceSEvan Bacon        this._subscription = null;
1422307aaceSEvan Bacon    }
1432307aaceSEvan Bacon}
1442307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map