@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
131 lines (128 loc) • 5.17 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const FileBase_1 = require("./FileBase");
const BrowserStorage_1 = require("./BrowserStorage");
const StorageUtilities_1 = require("./StorageUtilities");
const localforage_1 = require("localforage");
const Log_1 = require("../core/Log");
class BrowserFile extends FileBase_1.default {
get name() {
return this._name;
}
get parentFolder() {
return this._parentFolder;
}
get fullPath() {
return this._parentFolder.fullPath + BrowserStorage_1.default.slashFolderDelimiter + this.name;
}
get size() {
if (this.content == null) {
return -1;
}
return this.content.length;
}
constructor(parentFolder, fileName) {
super();
this.sizeAtLoad = undefined;
this._parentFolder = parentFolder;
this._name = fileName;
}
get isContentLoaded() {
return this.lastLoadedOrSaved != null || this.modified != null;
}
async deleteThisFile(skipRemoveFromParent) {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (skipRemoveFromParent !== true) {
this._parentFolder._removeFile(this);
await this._parentFolder.save(false);
}
await localforage_1.default.removeItem(this.fullPath);
return true;
}
async moveTo(newStorageRelativePath) {
const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
const newFileName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
if (newFileName.length < 2) {
throw new Error("New path is not correct.");
}
const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
if (newParentFolder.files[newFileName] !== undefined) {
throw new Error("File exists at specified path.");
}
await this.loadContent();
const originalPath = this.fullPath;
this._name = newFileName;
this._parentFolder = newParentFolder;
this.modified = new Date();
newParentFolder._addExistingFile(this);
await localforage_1.default.removeItem(originalPath);
return true;
}
async loadContent(force) {
if (force || !this.lastLoadedOrSaved) {
this._lastLoadedPath = this.fullPath;
this._content = await localforage_1.default.getItem(this.fullPath);
this.lastLoadedOrSaved = new Date();
}
return this.lastLoadedOrSaved;
}
async resaveAfterMove() {
if (this._lastLoadedPath === undefined) {
return;
}
if (this._lastLoadedPath !== this.fullPath) {
// store old path because saving will change _lastLoadedPath
const oldPath = this._lastLoadedPath;
await this.saveContent(true, true);
await localforage_1.default.removeItem(oldPath);
}
}
setContent(newContent) {
const areEqual = StorageUtilities_1.default.contentsAreEqual(this._content, newContent);
if (!areEqual) {
if (!this.lastLoadedOrSaved) {
this.lastLoadedOrSaved = new Date();
this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1);
// Log.debugAlert("Setting a file without loading it first.");
}
this._content = newContent;
if (this.isInErrorState &&
typeof newContent === "string" &&
StorageUtilities_1.default.getMimeType(this) === "application/json") {
StorageUtilities_1.default.getJsonObject(this);
}
this.contentWasModified();
}
}
async saveContent(force, skipParentFolderSave) {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (this.needsSave || force) {
/*let contentDescript = "null";
if (this.content instanceof Uint8Array) {
contentDescript = this.content.length + " bytes";
} else if (typeof this.content === "string") {
contentDescript = this.content.length + " text";
}*/
Log_1.default.assert(this.content !== null, "Null content found.");
// Log.debug("Saving file " + contentDescript + " to '" + this.fullPath + "'");
this._lastLoadedPath = this.fullPath;
await localforage_1.default.setItem(this.fullPath, this.content);
this.lastLoadedOrSaved = new Date();
if (skipParentFolderSave !== true) {
await this._parentFolder.save(false);
}
}
if (this.lastLoadedOrSaved === null) {
this.lastLoadedOrSaved = new Date();
}
return this.lastLoadedOrSaved;
}
}
exports.default = BrowserFile;
//# sourceMappingURL=../maps/storage/BrowserFile.js.map