UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

191 lines (190 loc) 6.96 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const HttpFile_1 = __importDefault(require("./HttpFile")); const HttpStorage_1 = __importDefault(require("./HttpStorage")); const StorageUtilities_1 = __importDefault(require("./StorageUtilities")); const FolderBase_1 = __importDefault(require("./FolderBase")); const Log_1 = __importDefault(require("../core/Log")); const axios_1 = __importDefault(require("axios")); class HttpFolder extends FolderBase_1.default { _name; folders; files; _storage; _parentFolder; _pendingLoadRequests = []; _isLoading = false; get storage() { return this._storage; } get parentFolder() { return this._parentFolder; } get name() { return this._name; } get fullPath() { if (this._parentFolder === null) { return this._storage.baseUrl; } return this._parentFolder.fullPath + this.name + HttpStorage_1.default.slashFolderDelimiter; } constructor(storage, parentFolder, folderName) { super(); this._storage = storage; this._parentFolder = parentFolder; this._name = folderName; this.folders = {}; this.files = {}; } async scanForChanges() { await this.load(true); } async exists() { return true; } async ensureExists() { if (this._storage.readOnly) { return true; } try { const headers = {}; if (this._storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._storage.authToken}`; } // POST with action=mkdir to create the folder await axios_1.default.post(this.fullPath + "?action=mkdir", null, { headers }); return true; } catch (e) { Log_1.default.debug("Failed to create folder: " + e); return false; } } ensureFile(name) { Log_1.default.assert(name.indexOf("/") < 0, "Unexpected to find / in file name: " + name); const nameCanon = StorageUtilities_1.default.canonicalizeName(name); let candFile = this.files[nameCanon]; if (candFile === undefined) { candFile = new HttpFile_1.default(this, name); this.files[nameCanon] = candFile; } return candFile; } async moveTo(newStorageRelativePath) { throw new Error("Not implemented."); } _removeFile(file) { const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name); const candFile = this.files[nameCanon]; Log_1.default.assert(candFile === file, "Files don't match."); this.files[nameCanon] = undefined; } _addExistingFile(file) { const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name); this.files[nameCanon] = file; } ensureFolder(name) { Log_1.default.assert(name.indexOf("/") < 0, "Unexpected to find / in folder name: " + name); const nameCanon = StorageUtilities_1.default.canonicalizeName(name); let candFolder = this.folders[nameCanon]; if (!candFolder) { candFolder = new HttpFolder(this._storage, this, name); this.folders[nameCanon] = candFolder; } return candFolder; } async deleteFile(name) { if (this._storage.readOnly) { throw new Error("Deletion of file not supported in read-only mode"); } const nameCanon = StorageUtilities_1.default.canonicalizeName(name); const file = this.files[nameCanon]; if (file) { return await file.deleteThisFile(); } return false; } async createFile(name) { const file = this.ensureFile(name); return file; } /** * Remove a file from the folder's file list (used after deletion). */ removeFile(name) { const nameCanon = StorageUtilities_1.default.canonicalizeName(name); this.files[nameCanon] = undefined; } async deleteThisFolder() { throw new Error("Deletion of this folder " + this.fullPath + " is not supported."); } async deleteAllFolderContents() { throw new Error("Deletion of all folder contents at " + this.fullPath + " is not supported."); } async load(force) { if (this.lastLoadedOrSaved != null && !force) { return this.lastLoadedOrSaved; } if (this._isLoading) { const pendingLoad = this._pendingLoadRequests; const prom = (resolve, reject) => { pendingLoad.push(resolve); }; await new Promise(prom); if (this.lastLoadedOrSaved === null) { throw new Error(); } return this.lastLoadedOrSaved; } else { this._isLoading = true; let response = undefined; try { // Include Authorization header if the storage has an auth token const headers = {}; if (this._storage.authToken) { headers["Authorization"] = `Bearer mctauth=${this._storage.authToken}`; } const requestUrl = this.fullPath + "index.json"; response = await axios_1.default.get(requestUrl, { headers, }); } catch (e) { Log_1.default.debug("HttpFolder: Could not load index for " + this.fullPath + " - " + (e?.message || e)); } if (response) { const index = response.data; if (index.files !== null && index.files !== undefined) { for (let i = 0; i < index.files.length; i++) { const file = index.files[i]; if (StorageUtilities_1.default.isUsableFile(file)) { this.ensureFile(file); } } } if (index.folders !== null && index.folders !== undefined) { for (let i = 0; i < index.folders.length; i++) { const folder = index.folders[i]; this.ensureFolder(folder); } } } this.updateLastLoadedOrSaved(); this._isLoading = false; const pendingLoad = this._pendingLoadRequests; this._pendingLoadRequests = []; for (const prom of pendingLoad) { prom(undefined); } } return this.lastLoadedOrSaved; } } exports.default = HttpFolder;