1import UUID from 'uuid-js'; 2import { Platform } from 'react-native'; 3import { EventEmitter, UnavailabilityError } from '@unimodules/core'; 4import ExponentFileSystem from './ExponentFileSystem'; 5import { EncodingType, } from './FileSystem.types'; 6if (!ExponentFileSystem) { 7 console.warn("No native ExponentFileSystem module found, are you sure the expo-file-system's module is linked properly?"); 8} 9// Prevent webpack from pruning this. 10const _unused = new EventEmitter(ExponentFileSystem); // eslint-disable-line 11export { EncodingType, }; 12function normalizeEndingSlash(p) { 13 if (p != null) { 14 return p.replace(/\/*$/, '') + '/'; 15 } 16 return null; 17} 18export const documentDirectory = normalizeEndingSlash(ExponentFileSystem.documentDirectory); 19export const cacheDirectory = normalizeEndingSlash(ExponentFileSystem.cacheDirectory); 20export const { bundledAssets, bundleDirectory } = ExponentFileSystem; 21export async function getInfoAsync(fileUri, options = {}) { 22 if (!ExponentFileSystem.getInfoAsync) { 23 throw new UnavailabilityError('expo-file-system', 'getInfoAsync'); 24 } 25 return await ExponentFileSystem.getInfoAsync(fileUri, options); 26} 27export async function readAsStringAsync(fileUri, options) { 28 if (!ExponentFileSystem.readAsStringAsync) { 29 throw new UnavailabilityError('expo-file-system', 'readAsStringAsync'); 30 } 31 return await ExponentFileSystem.readAsStringAsync(fileUri, options || {}); 32} 33export async function getContentUriAsync(fileUri) { 34 if (Platform.OS === 'android') { 35 if (!ExponentFileSystem.getContentUriAsync) { 36 throw new UnavailabilityError('expo-file-system', 'getContentUriAsync'); 37 } 38 return await ExponentFileSystem.getContentUriAsync(fileUri); 39 } 40 else { 41 return new Promise(function (resolve, reject) { 42 resolve(fileUri); 43 }); 44 } 45} 46export async function writeAsStringAsync(fileUri, contents, options = {}) { 47 if (!ExponentFileSystem.writeAsStringAsync) { 48 throw new UnavailabilityError('expo-file-system', 'writeAsStringAsync'); 49 } 50 return await ExponentFileSystem.writeAsStringAsync(fileUri, contents, options); 51} 52export async function deleteAsync(fileUri, options = {}) { 53 if (!ExponentFileSystem.deleteAsync) { 54 throw new UnavailabilityError('expo-file-system', 'deleteAsync'); 55 } 56 return await ExponentFileSystem.deleteAsync(fileUri, options); 57} 58export async function moveAsync(options) { 59 if (!ExponentFileSystem.moveAsync) { 60 throw new UnavailabilityError('expo-file-system', 'moveAsync'); 61 } 62 return await ExponentFileSystem.moveAsync(options); 63} 64export async function copyAsync(options) { 65 if (!ExponentFileSystem.copyAsync) { 66 throw new UnavailabilityError('expo-file-system', 'copyAsync'); 67 } 68 return await ExponentFileSystem.copyAsync(options); 69} 70export async function makeDirectoryAsync(fileUri, options = {}) { 71 if (!ExponentFileSystem.makeDirectoryAsync) { 72 throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync'); 73 } 74 return await ExponentFileSystem.makeDirectoryAsync(fileUri, options); 75} 76export async function readDirectoryAsync(fileUri) { 77 if (!ExponentFileSystem.readDirectoryAsync) { 78 throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync'); 79 } 80 return await ExponentFileSystem.readDirectoryAsync(fileUri, {}); 81} 82export async function getFreeDiskStorageAsync() { 83 if (!ExponentFileSystem.getFreeDiskStorageAsync) { 84 throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync'); 85 } 86 return await ExponentFileSystem.getFreeDiskStorageAsync(); 87} 88export async function getTotalDiskCapacityAsync() { 89 if (!ExponentFileSystem.getTotalDiskCapacityAsync) { 90 throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync'); 91 } 92 return await ExponentFileSystem.getTotalDiskCapacityAsync(); 93} 94export async function downloadAsync(uri, fileUri, options = {}) { 95 if (!ExponentFileSystem.downloadAsync) { 96 throw new UnavailabilityError('expo-file-system', 'downloadAsync'); 97 } 98 return await ExponentFileSystem.downloadAsync(uri, fileUri, options); 99} 100export function createDownloadResumable(uri, fileUri, options, callback, resumeData) { 101 return new DownloadResumable(uri, fileUri, options, callback, resumeData); 102} 103export class DownloadResumable { 104 constructor(url, fileUri, options = {}, callback, resumeData) { 105 this._uuid = UUID.create(4).toString(); 106 this._url = url; 107 this._fileUri = fileUri; 108 this._options = options; 109 this._resumeData = resumeData; 110 this._callback = callback; 111 this._subscription = null; 112 this._emitter = new EventEmitter(ExponentFileSystem); 113 } 114 async downloadAsync() { 115 if (!ExponentFileSystem.downloadResumableStartAsync) { 116 throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 117 } 118 this._addSubscription(); 119 return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 120 } 121 async pauseAsync() { 122 if (!ExponentFileSystem.downloadResumablePauseAsync) { 123 throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync'); 124 } 125 const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid); 126 this._removeSubscription(); 127 if (pauseResult) { 128 this._resumeData = pauseResult.resumeData; 129 return this.savable(); 130 } 131 else { 132 throw new Error('Unable to generate a savable pause state'); 133 } 134 } 135 async resumeAsync() { 136 if (!ExponentFileSystem.downloadResumableStartAsync) { 137 throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 138 } 139 this._addSubscription(); 140 return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 141 } 142 savable() { 143 return { 144 url: this._url, 145 fileUri: this._fileUri, 146 options: this._options, 147 resumeData: this._resumeData, 148 }; 149 } 150 _addSubscription() { 151 if (this._subscription) { 152 return; 153 } 154 this._subscription = this._emitter.addListener('Exponent.downloadProgress', (event) => { 155 if (event.uuid === this._uuid) { 156 const callback = this._callback; 157 if (callback) { 158 callback(event.data); 159 } 160 } 161 }); 162 } 163 _removeSubscription() { 164 if (!this._subscription) { 165 return; 166 } 167 this._emitter.removeSubscription(this._subscription); 168 this._subscription = null; 169 } 170} 171//# sourceMappingURL=FileSystem.js.map