12307aaceSEvan Baconimport UUID from 'uuid-js'; 21da2f189STomasz Sapetaimport { Platform } from 'react-native'; 3*79a5fc63SJames Ideimport { EventEmitter, UnavailabilityError } from '@unimodules/core'; 4*79a5fc63SJames Ideimport 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. 10*79a5fc63SJames Ideconst _unused = new EventEmitter(ExponentFileSystem); // eslint-disable-line 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} 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} 582307aaceSEvan Baconexport async function moveAsync(options) { 597e929553SEvan Bacon if (!ExponentFileSystem.moveAsync) { 602307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'moveAsync'); 612307aaceSEvan Bacon } 627e929553SEvan Bacon return await ExponentFileSystem.moveAsync(options); 632307aaceSEvan Bacon} 642307aaceSEvan Baconexport async function copyAsync(options) { 657e929553SEvan Bacon if (!ExponentFileSystem.copyAsync) { 662307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'copyAsync'); 672307aaceSEvan Bacon } 687e929553SEvan Bacon return await ExponentFileSystem.copyAsync(options); 692307aaceSEvan Bacon} 702307aaceSEvan Baconexport async function makeDirectoryAsync(fileUri, options = {}) { 717e929553SEvan Bacon if (!ExponentFileSystem.makeDirectoryAsync) { 722307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync'); 732307aaceSEvan Bacon } 747e929553SEvan Bacon return await ExponentFileSystem.makeDirectoryAsync(fileUri, options); 752307aaceSEvan Bacon} 762307aaceSEvan Baconexport async function readDirectoryAsync(fileUri) { 777e929553SEvan Bacon if (!ExponentFileSystem.readDirectoryAsync) { 782307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync'); 792307aaceSEvan Bacon } 807e929553SEvan Bacon return await ExponentFileSystem.readDirectoryAsync(fileUri, {}); 812307aaceSEvan Bacon} 82ae9a5f1aSTongyu Zhuexport async function getFreeDiskStorageAsync() { 83ae9a5f1aSTongyu Zhu if (!ExponentFileSystem.getFreeDiskStorageAsync) { 84ae9a5f1aSTongyu Zhu throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync'); 85ae9a5f1aSTongyu Zhu } 86ae9a5f1aSTongyu Zhu return await ExponentFileSystem.getFreeDiskStorageAsync(); 87ae9a5f1aSTongyu Zhu} 88ae9a5f1aSTongyu Zhuexport async function getTotalDiskCapacityAsync() { 89ae9a5f1aSTongyu Zhu if (!ExponentFileSystem.getTotalDiskCapacityAsync) { 90ae9a5f1aSTongyu Zhu throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync'); 91ae9a5f1aSTongyu Zhu } 92ae9a5f1aSTongyu Zhu return await ExponentFileSystem.getTotalDiskCapacityAsync(); 93ae9a5f1aSTongyu Zhu} 942307aaceSEvan Baconexport async function downloadAsync(uri, fileUri, options = {}) { 957e929553SEvan Bacon if (!ExponentFileSystem.downloadAsync) { 962307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadAsync'); 972307aaceSEvan Bacon } 987e929553SEvan Bacon return await ExponentFileSystem.downloadAsync(uri, fileUri, options); 992307aaceSEvan Bacon} 1002307aaceSEvan Baconexport function createDownloadResumable(uri, fileUri, options, callback, resumeData) { 1012307aaceSEvan Bacon return new DownloadResumable(uri, fileUri, options, callback, resumeData); 1022307aaceSEvan Bacon} 1032307aaceSEvan Baconexport class DownloadResumable { 1042307aaceSEvan Bacon constructor(url, fileUri, options = {}, callback, resumeData) { 1052307aaceSEvan Bacon this._uuid = UUID.create(4).toString(); 1062307aaceSEvan Bacon this._url = url; 1072307aaceSEvan Bacon this._fileUri = fileUri; 1082307aaceSEvan Bacon this._options = options; 1092307aaceSEvan Bacon this._resumeData = resumeData; 1102307aaceSEvan Bacon this._callback = callback; 1112307aaceSEvan Bacon this._subscription = null; 1127e929553SEvan Bacon this._emitter = new EventEmitter(ExponentFileSystem); 1132307aaceSEvan Bacon } 1142307aaceSEvan Bacon async downloadAsync() { 1157e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1162307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1172307aaceSEvan Bacon } 1182307aaceSEvan Bacon this._addSubscription(); 1197e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1202307aaceSEvan Bacon } 1212307aaceSEvan Bacon async pauseAsync() { 1227e929553SEvan Bacon if (!ExponentFileSystem.downloadResumablePauseAsync) { 1232307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync'); 1242307aaceSEvan Bacon } 1257e929553SEvan Bacon const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid); 1262307aaceSEvan Bacon this._removeSubscription(); 1272307aaceSEvan Bacon if (pauseResult) { 1282307aaceSEvan Bacon this._resumeData = pauseResult.resumeData; 1292307aaceSEvan Bacon return this.savable(); 1302307aaceSEvan Bacon } 1312307aaceSEvan Bacon else { 1322307aaceSEvan Bacon throw new Error('Unable to generate a savable pause state'); 1332307aaceSEvan Bacon } 1342307aaceSEvan Bacon } 1352307aaceSEvan Bacon async resumeAsync() { 1367e929553SEvan Bacon if (!ExponentFileSystem.downloadResumableStartAsync) { 1372307aaceSEvan Bacon throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 1382307aaceSEvan Bacon } 1392307aaceSEvan Bacon this._addSubscription(); 1407e929553SEvan Bacon return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 1412307aaceSEvan Bacon } 1422307aaceSEvan Bacon savable() { 1432307aaceSEvan Bacon return { 1442307aaceSEvan Bacon url: this._url, 1452307aaceSEvan Bacon fileUri: this._fileUri, 1462307aaceSEvan Bacon options: this._options, 1472307aaceSEvan Bacon resumeData: this._resumeData, 1482307aaceSEvan Bacon }; 1492307aaceSEvan Bacon } 1502307aaceSEvan Bacon _addSubscription() { 1512307aaceSEvan Bacon if (this._subscription) { 1522307aaceSEvan Bacon return; 1532307aaceSEvan Bacon } 1542307aaceSEvan Bacon this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => { 1552307aaceSEvan Bacon if (event.uuid === this._uuid) { 1562307aaceSEvan Bacon const callback = this._callback; 1572307aaceSEvan Bacon if (callback) { 1582307aaceSEvan Bacon callback(event.data); 1592307aaceSEvan Bacon } 1602307aaceSEvan Bacon } 1612307aaceSEvan Bacon }); 1622307aaceSEvan Bacon } 1632307aaceSEvan Bacon _removeSubscription() { 1642307aaceSEvan Bacon if (!this._subscription) { 1652307aaceSEvan Bacon return; 1662307aaceSEvan Bacon } 1672307aaceSEvan Bacon this._emitter.removeSubscription(this._subscription); 1682307aaceSEvan Bacon this._subscription = null; 1692307aaceSEvan Bacon } 1702307aaceSEvan Bacon} 1712307aaceSEvan Bacon//# sourceMappingURL=FileSystem.js.map