UNPKG

ali-flmngr-server-fixed

Version:

> Node.js Backend for Flmngr file manager

111 lines 4.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileUploaded = void 0; const AFile_1 = require("../file/AFile"); const Message_1 = require("../action/resp/Message"); const Utils_1 = require("../file/Utils"); const fsx = __importStar(require("fs-extra")); const MessageException_1 = require("../MessageException"); const FileCommited_1 = require("../file/FileCommited"); const URLDownloader_1 = require("../file/URLDownloader"); class FileUploaded extends AFile_1.AFile { constructor(config, dir, name, newName) { super(config, dir, name); this.m_conflictsErrors = []; this.m_customErrors = []; this.m_newName = newName; } getBaseDir() { return this.m_config.getTmpDir(); } getNewName() { return this.m_newName; } checkForErrors(checkForExist) { if (!super.checkForErrors(checkForExist)) return false; if ((this.m_newName !== this.getName()) && !Utils_1.Utils.isFileNameSyntaxOk(this.m_newName)) this.m_commonErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ERROR_SYNTAX, this.m_newName)); if (Utils_1.Utils.isImage(this.getName())) { let ext = this.getExt(); let newExt = Utils_1.Utils.getExt(this.m_newName); if (ext !== newExt) if (!(ext === "jpg" && newExt === "jpeg") && !(ext === "jpeg" && newExt === "jpg")) this.m_commonErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ERROR_INCORRECT_IMAGE_EXT_CHANGE, ext, newExt)); } return true; } addCustomError(message) { this.m_customErrors.push(message); } getErrors() { let errors = super.getErrors(); for (const err of this.m_conflictsErrors) errors.push(err); for (const err of this.m_customErrors) errors.push(err); return errors; } getCommitedFile(dir) { return new FileCommited_1.FileCommited(this.m_config, dir, this.m_newName); } checkForConflicts(dir) { this.m_conflictsErrors = []; let file = this.getCommitedFile(dir); if (file.exists()) this.m_conflictsErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ALREADY_EXISTS, file.getName())); if (file.isImage()) { let fileOriginal = file.getFileOriginal(); if (fileOriginal.exists()) this.m_conflictsErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ALREADY_EXISTS, fileOriginal.getName())); let filePreview = file.getFilePreview(); if (filePreview.exists()) this.m_conflictsErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ALREADY_EXISTS, filePreview.getName())); } } uploadAndCommit(is) { let initName = this.getName(); this.setFreeFileName(); try { let os = this.getFile(); fsx.writeFileSync(os, is); } catch (e) { new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.WRITING_FILE_ERROR, initName)); } } rehost(url, onFinish) { URLDownloader_1.URLDownloader.download(url, this.getBaseDir() + "/" + this.getDir(), (result) => { if (result instanceof URLDownloader_1.DownloadedURL) this.setName(result.fileName); onFinish(result); }); } commit(dir, autoRename) { let file = this.getCommitedFile(dir); if (autoRename) file.setFreeFileName(); this.copyTo(file); return file; } isCommited() { return false; } } exports.FileUploaded = FileUploaded; //# sourceMappingURL=FileUploaded.js.map