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'; 51da2f189STomasz 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) { 351da2f189STomasz 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 } 411da2f189STomasz Sapeta else { 421da2f189STomasz Sapeta return new Promise(function (resolve, reject) { 431da2f189STomasz Sapeta resolve(fileUri); 441da2f189STomasz Sapeta }); 451da2f189STomasz Sapeta } 461da2f189STomasz 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} 83*ae9a5f1aSTongyu Zhuexport async function getFreeDiskStorageAsync() { 84*ae9a5f1aSTongyu Zhu if (!ExponentFileSystem.getFreeDiskStorageAsync) { 85*ae9a5f1aSTongyu Zhu throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync'); 86*ae9a5f1aSTongyu Zhu } 87*ae9a5f1aSTongyu Zhu return await ExponentFileSystem.getFreeDiskStorageAsync(); 88*ae9a5f1aSTongyu Zhu} 89*ae9a5f1aSTongyu Zhuexport async function getTotalDiskCapacityAsync() { 90*ae9a5f1aSTongyu Zhu if (!ExponentFileSystem.getTotalDiskCapacityAsync) { 91*ae9a5f1aSTongyu Zhu throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync'); 92*ae9a5f1aSTongyu Zhu } 93*ae9a5f1aSTongyu Zhu return await ExponentFileSystem.getTotalDiskCapacityAsync(); 94*ae9a5f1aSTongyu Zhu} 952307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) { 967e929553SEvan Bacon if (!ExponentFileSystem.downloadAsync) { 972307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadAsync'); 982307aaceSEvan Bacon } 997e929553SEvan Bacon return await ExponentFileSystem.downloadAsync(uri, fileUri, options); 1002307aaceSEvan Bacon} 1012307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) { 1022307aaceSEvan Bacon return new DownloadResumable(uri, fileUri, options, callback, resumeData); 1032307aaceSEvan Bacon} 1042307aaceSEvan Baconexport class DownloadResumable { 1052307aaceSEvan Bacon constructor(url, fileUri, options = {}, callback, resumeData) { 1062307aaceSEvan Bacon this._uuid = UUID.create(4).toString(); 1072307aaceSEvan Bacon this._url = url; 1082307aaceSEvan Bacon this._fileUri = fileUri; 1092307aaceSEvan Bacon this._options = options; 1102307aaceSEvan Bacon this._resumeData = resumeData; 1112307aaceSEvan Bacon this._callback = callback; 1122307aaceSEvan Bacon this._subscription = null; 1137e929553SEvan Bacon this._emitter = new EventEmitter(ExponentFileSystem); 1142307aaceSEvan Bacon } 1152307aaceSEvan Bacon async downloadAsync() { 1167e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1172307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1182307aaceSEvan Bacon } 1192307aaceSEvan Bacon this._addSubscription(); 1207e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1212307aaceSEvan Bacon } 1222307aaceSEvan Bacon async pauseAsync() { 1237e929553SEvan Bacon if (!ExponentFileSystem.downloadResumablePauseAsync) { 1242307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync'); 1252307aaceSEvan Bacon } 1267e929553SEvan Bacon const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid); 1272307aaceSEvan Bacon this._removeSubscription(); 1282307aaceSEvan Bacon if (pauseResult) { 1292307aaceSEvan Bacon this._resumeData = pauseResult.resumeData; 1302307aaceSEvan Bacon return this.savable(); 1312307aaceSEvan Bacon } 1322307aaceSEvan Bacon else { 1332307aaceSEvan Bacon throw new Error('Unable to generate a savable pause state'); 1342307aaceSEvan Bacon } 1352307aaceSEvan Bacon } 1362307aaceSEvan Bacon async resumeAsync() { 1377e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1382307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1392307aaceSEvan Bacon } 1402307aaceSEvan Bacon this._addSubscription(); 1417e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1422307aaceSEvan Bacon } 1432307aaceSEvan Bacon savable() { 1442307aaceSEvan Bacon return { 1452307aaceSEvan Bacon url: this._url, 1462307aaceSEvan Bacon fileUri: this._fileUri, 1472307aaceSEvan Bacon options: this._options, 1482307aaceSEvan Bacon resumeData: this._resumeData, 1492307aaceSEvan Bacon }; 1502307aaceSEvan Bacon } 1512307aaceSEvan Bacon _addSubscription() { 1522307aaceSEvan Bacon if (this._subscription) { 1532307aaceSEvan Bacon return; 1542307aaceSEvan Bacon } 1552307aaceSEvan Bacon this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => { 1562307aaceSEvan Bacon if (event.uuid === this._uuid) { 1572307aaceSEvan Bacon const callback = this._callback; 1582307aaceSEvan Bacon if (callback) { 1592307aaceSEvan Bacon callback(event.data); 1602307aaceSEvan Bacon } 1612307aaceSEvan Bacon } 1622307aaceSEvan Bacon }); 1632307aaceSEvan Bacon } 1642307aaceSEvan Bacon _removeSubscription() { 1652307aaceSEvan Bacon if (!this._subscription) { 1662307aaceSEvan Bacon return; 1672307aaceSEvan Bacon } 1682307aaceSEvan Bacon this._emitter.removeSubscription(this._subscription); 1692307aaceSEvan Bacon this._subscription = null; 1702307aaceSEvan Bacon } 1712307aaceSEvan Bacon} 1722307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map