UNPKG

kura-expo-fs

Version:

The FileSystem API abstraction library, Expo Filesystem Plugin

104 lines 4.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpoFsTransferer = void 0; const expo_file_system_1 = require("expo-file-system"); const kura_1 = require("kura"); class ExpoFsTransferer extends kura_1.Transferer { constructor(options) { super(); this.options = options; if (!options) { options = {}; } if (options.getOnly == null) { options.getOnly = false; } if (options.background == null) { options.background = false; } } async transfer(fromAccessor, fromObj, toAccessor, toObj) { if (this.options.threshold != null && fromObj.size < this.options.threshold) { await super.transfer(fromAccessor, fromObj, toAccessor, toObj); return; } const fromUrl = await fromAccessor.getURL(fromObj.fullPath, "GET"); const toUrl = await toAccessor.getURL(toObj.fullPath, "PUT"); if (!fromUrl || !toUrl || (this.options.getOnly && !toUrl.startsWith("file:"))) { await super.transfer(fromAccessor, fromObj, toAccessor, toObj); return; } if (fromUrl.startsWith("file:")) { if (toUrl.startsWith("file:")) { await (0, expo_file_system_1.copyAsync)({ from: fromUrl, to: toUrl }); } else { const uRes = await (0, expo_file_system_1.uploadAsync)(toUrl, fromUrl, { httpMethod: "PUT", headers: { "Content-Length": fromObj.size + "", }, sessionType: this.options.background ? expo_file_system_1.FileSystemSessionType.BACKGROUND : expo_file_system_1.FileSystemSessionType.FOREGROUND, uploadType: expo_file_system_1.FileSystemUploadType.BINARY_CONTENT, }); if (!(uRes.status + "").startsWith("2")) { throw uRes; } } } else { if (toUrl.startsWith("file:")) { const result = await (0, expo_file_system_1.downloadAsync)(fromUrl, toUrl, { sessionType: this.options.background ? expo_file_system_1.FileSystemSessionType.BACKGROUND : expo_file_system_1.FileSystemSessionType.FOREGROUND, }); if (!(result.status + "").startsWith("2")) { throw result; } } else { const tempUri = expo_file_system_1.cacheDirectory.replace(kura_1.LAST_DIR_SEPARATORS, "") + kura_1.DIR_SEPARATOR + Date.now(); try { const dRes = await (0, expo_file_system_1.downloadAsync)(fromUrl, tempUri, { sessionType: this.options.background ? expo_file_system_1.FileSystemSessionType.BACKGROUND : expo_file_system_1.FileSystemSessionType.FOREGROUND, }); if (!(dRes.status + "").startsWith("2")) { throw dRes; } const info = await (0, expo_file_system_1.getInfoAsync)(tempUri); const uRes = await (0, expo_file_system_1.uploadAsync)(toUrl, tempUri, { httpMethod: "PUT", headers: { "Content-Length": info.size + "", }, sessionType: this.options.background ? expo_file_system_1.FileSystemSessionType.BACKGROUND : expo_file_system_1.FileSystemSessionType.FOREGROUND, uploadType: expo_file_system_1.FileSystemUploadType.BINARY_CONTENT, }); if (!(uRes.status + "").startsWith("2")) { throw uRes; } } finally { try { (0, expo_file_system_1.deleteAsync)(tempUri); } catch { } } } } } } exports.ExpoFsTransferer = ExpoFsTransferer; //# sourceMappingURL=ExpoFsTransferer.js.map