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'; 5*1da2f189STomasz Sapetaimport { Platform } from 'react-native'; 62307aaceSEvan Baconimport { EncodingType, } from './FileSystem.types'; 77e929553SEvan Baconif (!ExponentFileSystem) { 82307aaceSEvan Bacon console.warn("No native ExponentFileSystem module found, are you sure the expo-file-system's module is linked properly?"); 92307aaceSEvan Bacon} 108f6e1365SEvan Bacon// Prevent webpack from pruning this. 118d441c7eSStanisław Chmielaconst _unused = new EventEmitter(ExponentFileSystem); 122307aaceSEvan Baconexport { EncodingType, }; 132307aaceSEvan Baconfunction normalizeEndingSlash(p) { 142307aaceSEvan Bacon if (p != null) { 152307aaceSEvan Bacon return p.replace(/\/*$/, '') + '/'; 162307aaceSEvan Bacon } 172307aaceSEvan Bacon return null; 182307aaceSEvan Bacon} 197e929553SEvan Baconexport const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory); 207e929553SEvan Baconexport const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory); 217e929553SEvan Baconexport const { bundledAssets, bundleDirectory } = ExponentFileSystem; 222307aaceSEvan Baconexport async function getInfoAsync(fileUri, options = {}) { 237e929553SEvan Bacon if (!ExponentFileSystem.getInfoAsync) { 242307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'getInfoAsync'); 252307aaceSEvan Bacon } 267e929553SEvan Bacon return await ExponentFileSystem.getInfoAsync(fileUri, options); 272307aaceSEvan Bacon} 282307aaceSEvan Baconexport async function readAsStringAsync(fileUri, options) { 297e929553SEvan Bacon if (!ExponentFileSystem.readAsStringAsync) { 302307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'readAsStringAsync'); 312307aaceSEvan Bacon } 327e929553SEvan Bacon return await ExponentFileSystem.readAsStringAsync(fileUri, options || {}); 332307aaceSEvan Bacon} 34e4897761STongyu Zhuexport async function getContentUriAsync(fileUri) { 35*1da2f189STomasz Sapeta if (Platform.OS === 'android') { 36e4897761STongyu Zhu if (!ExponentFileSystem.getContentUriAsync) { 37e4897761STongyu Zhu throw new UnavailabilityError('expo-file-system', 'getContentUriAsync'); 38e4897761STongyu Zhu } 39e4897761STongyu Zhu return await ExponentFileSystem.getContentUriAsync(fileUri); 40e4897761STongyu Zhu } 41*1da2f189STomasz Sapeta else { 42*1da2f189STomasz Sapeta return new Promise(function (resolve, reject) { 43*1da2f189STomasz Sapeta resolve(fileUri); 44*1da2f189STomasz Sapeta }); 45*1da2f189STomasz Sapeta } 46*1da2f189STomasz Sapeta} 472307aaceSEvan Baconexport async function writeAsStringAsync(fileUri, contents, options = {}) { 487e929553SEvan Bacon if (!ExponentFileSystem.writeAsStringAsync) { 492307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync'); 502307aaceSEvan Bacon } 517e929553SEvan Bacon return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options); 522307aaceSEvan Bacon} 532307aaceSEvan Baconexport async function deleteAsync(fileUri, options = {}) { 547e929553SEvan Bacon if (!ExponentFileSystem.deleteAsync) { 552307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'deleteAsync'); 562307aaceSEvan Bacon } 577e929553SEvan Bacon return await ExponentFileSystem.deleteAsync(fileUri, options); 582307aaceSEvan Bacon} 592307aaceSEvan Baconexport async function moveAsync(options) { 607e929553SEvan Bacon if (!ExponentFileSystem.moveAsync) { 612307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'moveAsync'); 622307aaceSEvan Bacon } 637e929553SEvan Bacon return await ExponentFileSystem.moveAsync(options); 642307aaceSEvan Bacon} 652307aaceSEvan Baconexport async function copyAsync(options) { 667e929553SEvan Bacon if (!ExponentFileSystem.copyAsync) { 672307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'copyAsync'); 682307aaceSEvan Bacon } 697e929553SEvan Bacon return await ExponentFileSystem.copyAsync(options); 702307aaceSEvan Bacon} 712307aaceSEvan Baconexport async function makeDirectoryAsync(fileUri, options = {}) { 727e929553SEvan Bacon if (!ExponentFileSystem.makeDirectoryAsync) { 732307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync'); 742307aaceSEvan Bacon } 757e929553SEvan Bacon return await ExponentFileSystem.makeDirectoryAsync(fileUri, options); 762307aaceSEvan Bacon} 772307aaceSEvan Baconexport async function readDirectoryAsync(fileUri) { 787e929553SEvan Bacon if (!ExponentFileSystem.readDirectoryAsync) { 792307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync'); 802307aaceSEvan Bacon } 817e929553SEvan Bacon return await ExponentFileSystem.readDirectoryAsync(fileUri, {}); 822307aaceSEvan Bacon} 832307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) { 847e929553SEvan Bacon if (!ExponentFileSystem.downloadAsync) { 852307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadAsync'); 862307aaceSEvan Bacon } 877e929553SEvan Bacon return await ExponentFileSystem.downloadAsync(uri, fileUri, options); 882307aaceSEvan Bacon} 892307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) { 902307aaceSEvan Bacon return new DownloadResumable(uri, fileUri, options, callback, resumeData); 912307aaceSEvan Bacon} 922307aaceSEvan Baconexport class DownloadResumable { 932307aaceSEvan Bacon constructor(url, fileUri, options = {}, callback, resumeData) { 942307aaceSEvan Bacon this._uuid = UUID.create(4).toString(); 952307aaceSEvan Bacon this._url = url; 962307aaceSEvan Bacon this._fileUri = fileUri; 972307aaceSEvan Bacon this._options = options; 982307aaceSEvan Bacon this._resumeData = resumeData; 992307aaceSEvan Bacon this._callback = callback; 1002307aaceSEvan Bacon this._subscription = null; 1017e929553SEvan Bacon this._emitter = new EventEmitter(ExponentFileSystem); 1022307aaceSEvan Bacon } 1032307aaceSEvan Bacon async downloadAsync() { 1047e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1052307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1062307aaceSEvan Bacon } 1072307aaceSEvan Bacon this._addSubscription(); 1087e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1092307aaceSEvan Bacon } 1102307aaceSEvan Bacon async pauseAsync() { 1117e929553SEvan Bacon if (!ExponentFileSystem.downloadResumablePauseAsync) { 1122307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync'); 1132307aaceSEvan Bacon } 1147e929553SEvan Bacon const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid); 1152307aaceSEvan Bacon this._removeSubscription(); 1162307aaceSEvan Bacon if (pauseResult) { 1172307aaceSEvan Bacon this._resumeData = pauseResult.resumeData; 1182307aaceSEvan Bacon return this.savable(); 1192307aaceSEvan Bacon } 1202307aaceSEvan Bacon else { 1212307aaceSEvan Bacon throw new Error('Unable to generate a savable pause state'); 1222307aaceSEvan Bacon } 1232307aaceSEvan Bacon } 1242307aaceSEvan Bacon async resumeAsync() { 1257e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1262307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1272307aaceSEvan Bacon } 1282307aaceSEvan Bacon this._addSubscription(); 1297e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1302307aaceSEvan Bacon } 1312307aaceSEvan Bacon savable() { 1322307aaceSEvan Bacon return { 1332307aaceSEvan Bacon url: this._url, 1342307aaceSEvan Bacon fileUri: this._fileUri, 1352307aaceSEvan Bacon options: this._options, 1362307aaceSEvan Bacon resumeData: this._resumeData, 1372307aaceSEvan Bacon }; 1382307aaceSEvan Bacon } 1392307aaceSEvan Bacon _addSubscription() { 1402307aaceSEvan Bacon if (this._subscription) { 1412307aaceSEvan Bacon return; 1422307aaceSEvan Bacon } 1432307aaceSEvan Bacon this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => { 1442307aaceSEvan Bacon if (event.uuid === this._uuid) { 1452307aaceSEvan Bacon const callback = this._callback; 1462307aaceSEvan Bacon if (callback) { 1472307aaceSEvan Bacon callback(event.data); 1482307aaceSEvan Bacon } 1492307aaceSEvan Bacon } 1502307aaceSEvan Bacon }); 1512307aaceSEvan Bacon } 1522307aaceSEvan Bacon _removeSubscription() { 1532307aaceSEvan Bacon if (!this._subscription) { 1542307aaceSEvan Bacon return; 1552307aaceSEvan Bacon } 1562307aaceSEvan Bacon this._emitter.removeSubscription(this._subscription); 1572307aaceSEvan Bacon this._subscription = null; 1582307aaceSEvan Bacon } 1592307aaceSEvan Bacon} 1602307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map