UNPKG

kura-expo-fs

Version:

The FileSystem API abstraction library, Expo Filesystem Plugin

158 lines 5.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpoFsAccessor = void 0; const expo_file_system_1 = require("expo-file-system"); const kura_1 = require("kura"); const ExpoFsFileSystem_1 = require("./ExpoFsFileSystem"); const CHUNK_SIZE = 96 * 1024; class ExpoFsAccessor extends kura_1.AbstractAccessor { constructor(rootDir, options) { super(options); this.filesystem = new ExpoFsFileSystem_1.ExpoFsFileSystem(this); this.name = rootDir; this.rootUri = expo_file_system_1.documentDirectory.replace(kura_1.LAST_DIR_SEPARATORS, "") + rootDir; (0, expo_file_system_1.makeDirectoryAsync)(this.rootUri).catch((e) => { console.debug(e); }); console.info(this.rootUri); } async doDelete(fullPath, isFile) { const uri = this.toURL(fullPath); try { await (0, expo_file_system_1.deleteAsync)(uri); } catch (e) { try { const info = await (0, expo_file_system_1.getInfoAsync)(uri); if (!info.exists) { return; } } catch { } this.log("deleteAsync", uri, e); throw new kura_1.InvalidModificationError(this.name, fullPath, e); } } async doGetObject(fullPath, _isFile) { const info = await this.doGetInfo(fullPath); return { fullPath, name: fullPath.split(kura_1.DIR_SEPARATOR).pop(), lastModified: Math.floor(info.modificationTime * 1000), size: info.isDirectory ? undefined : info.size, }; } async doGetObjects(dirPath) { const dirInfo = await this.doGetInfo(dirPath); try { var names = await (0, expo_file_system_1.readDirectoryAsync)(dirInfo.uri); } catch (e) { this.log("readDirectoryAsync", dirInfo.uri, e); throw new kura_1.NotReadableError(this.name, dirPath, e); } const objects = []; for (const name of names) { const fullPath = (0, kura_1.normalizePath)(dirPath + kura_1.DIR_SEPARATOR + name); try { var info = await this.doGetInfo(fullPath); } catch (e) { if (e instanceof kura_1.NotFoundError) { console.warn(e); continue; } throw e; } objects.push({ fullPath: fullPath, name: name, lastModified: Math.floor(info.modificationTime * 1000), size: info.isDirectory ? undefined : info.size, }); } return objects; } async doMakeDirectory(fullPath) { const uri = this.toURL(fullPath); try { await (0, expo_file_system_1.makeDirectoryAsync)(uri); } catch (e) { try { await (0, expo_file_system_1.getInfoAsync)(uri); return; } catch { } this.log("makeDirectoryAsync", uri, e); throw new kura_1.InvalidModificationError(this.name, fullPath, e); } } async doReadContent(fullPath) { const info = await this.doGetInfo(fullPath); const chunks = []; for (let position = 0, end = info.size; position < end; position += CHUNK_SIZE) { const chunk = await (0, expo_file_system_1.readAsStringAsync)(info.uri, { encoding: "base64", position, length: CHUNK_SIZE, }); chunks.push(chunk); } const base64 = chunks.join(""); return base64; } async getURL(fullPath) { return this.toURL(fullPath); } async doWriteArrayBuffer(fullPath, buffer) { const base64 = await (0, kura_1.toBase64)(buffer); await this.doWriteBase64(fullPath, base64); } async doWriteBase64(fullPath, base64) { const uri = this.toURL(fullPath); await (0, expo_file_system_1.writeAsStringAsync)(uri, base64, { encoding: "base64" }); } async doWriteBlob(fullPath, blob) { const base64 = await (0, kura_1.toBase64)(blob); await this.doWriteBase64(fullPath, base64); } async doWriteBuffer(fullPath, buffer) { const base64 = await (0, kura_1.toBase64)(buffer); await this.doWriteBase64(fullPath, base64); } async doWriteUint8Array(fullPath, view) { const base64 = await (0, kura_1.toBase64)(view); await this.doWriteBase64(fullPath, base64); } async doGetInfo(fullPath) { const uri = this.toURL(fullPath); try { var info = await (0, expo_file_system_1.getInfoAsync)(uri, { size: true }); } catch (e) { this.log("getInfoAsync", uri, e); throw new kura_1.NotReadableError(this.name, fullPath, e); } if (!info.exists) { throw new kura_1.NotFoundError(this.name, fullPath); } return info; } log(message, uri, e) { if (!this.options.verbose) { return; } if (!e) { console.log(`${message} - ${uri}`); } else { console.trace(`${message} - ${uri}:`, e); } } toURL(fullPath) { return `${this.rootUri}${fullPath}`; } } exports.ExpoFsAccessor = ExpoFsAccessor; //# sourceMappingURL=ExpoFsAccessor.js.map