1import { EventEmitter, UnavailabilityError } from '@unimodules/core'; 2import { Platform } from 'react-native'; 3import uuidv4 from 'uuid/v4'; 4import ExponentFileSystem from './ExponentFileSystem'; 5import { EncodingType, FileSystemSessionType, FileSystemUploadType, } 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, FileSystemSessionType, FileSystemUploadType, }; 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 deleteLegacyDocumentDirectoryAndroid() { 59 if (Platform.OS !== 'android' || documentDirectory == null) { 60 return; 61 } 62 const legacyDocumentDirectory = `${documentDirectory}ExperienceData/`; 63 return await deleteAsync(legacyDocumentDirectory, { idempotent: true }); 64} 65export async function moveAsync(options) { 66 if (!ExponentFileSystem.moveAsync) { 67 throw new UnavailabilityError('expo-file-system', 'moveAsync'); 68 } 69 return await ExponentFileSystem.moveAsync(options); 70} 71export async function copyAsync(options) { 72 if (!ExponentFileSystem.copyAsync) { 73 throw new UnavailabilityError('expo-file-system', 'copyAsync'); 74 } 75 return await ExponentFileSystem.copyAsync(options); 76} 77export async function makeDirectoryAsync(fileUri, options = {}) { 78 if (!ExponentFileSystem.makeDirectoryAsync) { 79 throw new UnavailabilityError('expo-file-system', 'makeDirectoryAsync'); 80 } 81 return await ExponentFileSystem.makeDirectoryAsync(fileUri, options); 82} 83export async function readDirectoryAsync(fileUri) { 84 if (!ExponentFileSystem.readDirectoryAsync) { 85 throw new UnavailabilityError('expo-file-system', 'readDirectoryAsync'); 86 } 87 return await ExponentFileSystem.readDirectoryAsync(fileUri, {}); 88} 89export async function getFreeDiskStorageAsync() { 90 if (!ExponentFileSystem.getFreeDiskStorageAsync) { 91 throw new UnavailabilityError('expo-file-system', 'getFreeDiskStorageAsync'); 92 } 93 return await ExponentFileSystem.getFreeDiskStorageAsync(); 94} 95export async function getTotalDiskCapacityAsync() { 96 if (!ExponentFileSystem.getTotalDiskCapacityAsync) { 97 throw new UnavailabilityError('expo-file-system', 'getTotalDiskCapacityAsync'); 98 } 99 return await ExponentFileSystem.getTotalDiskCapacityAsync(); 100} 101export async function downloadAsync(uri, fileUri, options = {}) { 102 if (!ExponentFileSystem.downloadAsync) { 103 throw new UnavailabilityError('expo-file-system', 'downloadAsync'); 104 } 105 return await ExponentFileSystem.downloadAsync(uri, fileUri, { 106 sessionType: FileSystemSessionType.BACKGROUND, 107 ...options, 108 }); 109} 110export async function uploadAsync(url, fileUri, options = {}) { 111 if (!ExponentFileSystem.uploadAsync) { 112 throw new UnavailabilityError('expo-file-system', 'uploadAsync'); 113 } 114 return await ExponentFileSystem.uploadAsync(url, fileUri, { 115 sessionType: FileSystemSessionType.BACKGROUND, 116 uploadType: FileSystemUploadType.BINARY_CONTENT, 117 ...options, 118 httpMethod: (options.httpMethod || 'POST').toUpperCase(), 119 }); 120} 121export function createDownloadResumable(uri, fileUri, options, callback, resumeData) { 122 return new DownloadResumable(uri, fileUri, options, callback, resumeData); 123} 124export class DownloadResumable { 125 constructor(url, fileUri, options = {}, callback, resumeData) { 126 this._uuid = uuidv4(); 127 this._url = url; 128 this._fileUri = fileUri; 129 this._options = options; 130 this._resumeData = resumeData; 131 this._callback = callback; 132 this._subscription = null; 133 this._emitter = new EventEmitter(ExponentFileSystem); 134 } 135 async downloadAsync() { 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 async pauseAsync() { 143 if (!ExponentFileSystem.downloadResumablePauseAsync) { 144 throw new UnavailabilityError('expo-file-system', 'downloadResumablePauseAsync'); 145 } 146 const pauseResult = await ExponentFileSystem.downloadResumablePauseAsync(this._uuid); 147 this._removeSubscription(); 148 if (pauseResult) { 149 this._resumeData = pauseResult.resumeData; 150 return this.savable(); 151 } 152 else { 153 throw new Error('Unable to generate a savable pause state'); 154 } 155 } 156 async resumeAsync() { 157 if (!ExponentFileSystem.downloadResumableStartAsync) { 158 throw new UnavailabilityError('expo-file-system', 'downloadResumableStartAsync'); 159 } 160 this._addSubscription(); 161 return await ExponentFileSystem.downloadResumableStartAsync(this._url, this._fileUri, this._uuid, this._options, this._resumeData); 162 } 163 savable() { 164 return { 165 url: this._url, 166 fileUri: this._fileUri, 167 options: this._options, 168 resumeData: this._resumeData, 169 }; 170 } 171 _addSubscription() { 172 if (this._subscription) { 173 return; 174 } 175 this._subscription = this._emitter.addListener('expo-file-system.downloadProgress', (event) => { 176 if (event.uuid === this._uuid) { 177 const callback = this._callback; 178 if (callback) { 179 callback(event.data); 180 } 181 } 182 }); 183 } 184 _removeSubscription() { 185 if (!this._subscription) { 186 return; 187 } 188 this._emitter.removeSubscription(this._subscription); 189 this._subscription = null; 190 } 191} 192//# sourceMappingURL=FileSystem.js.map