UNPKG

ali-flmngr-server-fixed

Version:

> Node.js Backend for Flmngr file manager

287 lines 13.2 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.FileCommited = void 0; const AFile_1 = require("../file/AFile"); const fsx = __importStar(require("fs-extra")); const Utils_1 = require("../file/Utils"); const Message_1 = require("../action/resp/Message"); const MessageException_1 = require("../MessageException"); const gm = __importStar(require("gm")); class FileCommited extends AFile_1.AFile { constructor(config, dir, name) { super(config, dir, name); this.m_modificationName = ''; } getBaseDir() { return this.m_config.getBaseDir(); } getFileModification(modificationName) { if (!this.isImage() || this.m_mainFile != null) throw new Error("Illegal argument"); let name = this.getNameWithoutExt() + "-" + modificationName + "." + this.getExt(); let file = new FileCommited(this.m_config, this.getDir(), name); file.m_modificationName = modificationName; file.m_mainFile = this; return file; } getFileOriginal() { return this.getFileModification("original"); } getFilePreview() { return this.getFileModification("preview"); } getModificationName() { return this.m_modificationName; } getModifications() { let modifications = []; let f = this.getFilePreview(); if (f.exists()) modifications.push(f); f = this.getFileOriginal(); if (f.exists()) modifications.push(f); return modifications; } applySizes(sizes, onFinish) { try { if (!this.isImage()) { onFinish(null); return; } let currPreviewWidth = -1; let currPreviewHeight = -1; let filePreview = this.getFilePreview(); if (filePreview.exists()) { currPreviewWidth = filePreview.getImageWidth(); currPreviewHeight = filePreview.getImageHeight(); } let currFullWidth = this.getImageWidth(); let currFullHeight = this.getImageHeight(); let fileOriginal = this.getFileOriginal(); let fileOriginalOrFull = this; if (fileOriginal.exists()) fileOriginalOrFull = fileOriginal; let currOriginalWidth = fileOriginalOrFull.getImageWidth(); let currOriginalHeight = fileOriginalOrFull.getImageHeight(); this.applySizePreview(filePreview, fileOriginalOrFull, currPreviewWidth, currPreviewHeight, currOriginalWidth, currOriginalHeight, sizes, (e) => { if (e != null && e !== -1) { onFinish(e); return; } this.applySizeFull(fileOriginal, currFullWidth, currFullHeight, currOriginalWidth, currOriginalHeight, sizes, (e) => { if (e != null) { onFinish(e); return; } onFinish(null); }); }); } catch (e) { onFinish(e); } } applySizeFull(fileOriginal, currFullWidth, currFullHeight, currOriginalWidth, currOriginalHeight, sizes, onFinish) { let doResizeFull = false; let targetSize = sizes[FileCommited.SIZE_FULL]; if (FileCommited.SIZE_FULL in sizes) { if (targetSize["width"] !== currFullWidth || targetSize["height"] !== currFullHeight) if (targetSize["width"] > 0 || targetSize["height"] > 0) if ((targetSize["width"] < currOriginalWidth || targetSize["height"] < currOriginalHeight) || targetSize["enlarge"]) doResizeFull = true; } if (doResizeFull) { try { let originalExisted = fileOriginal.exists(); if (!originalExisted) this.copyTo(fileOriginal); this.resizeImage(targetSize, (function () { let _fileOriginal = fileOriginal; let _originalExisted = originalExisted; return (e) => { if (e) // error or -1 if (!originalExisted && _fileOriginal.exists()) _fileOriginal.delete(); onFinish(e === -1 ? null : e); }; })()); } catch (e) { onFinish(e); } } else { onFinish(null); } } applySizePreview(filePreview, fileOriginalOrFull, currPreviewWidth, currPreviewHeight, currOriginalWidth, currOriginalHeight, sizes, onFinish) { let doResizePreview = false; let targetSizePreview = sizes[FileCommited.SIZE_PREVIEW]; if (FileCommited.SIZE_PREVIEW in sizes) { if (!filePreview.exists()) fileOriginalOrFull.copyTo(filePreview); if (targetSizePreview["width"] !== currPreviewWidth || targetSizePreview["height"] !== currPreviewHeight) // Target size differs from current if (targetSizePreview["width"] > 0 || targetSizePreview["height"] > 0) // not fully auto if ((targetSizePreview["width"] < currOriginalWidth || targetSizePreview["height"] < currOriginalHeight) || targetSizePreview["enlarge"]) // We reduce size of image or have enlarge allowed doResizePreview = true; } if (doResizePreview) { try { filePreview.resizeImage(targetSizePreview, (e) => { onFinish(e === -1 ? null : e); }); } catch (e) { onFinish(e); } } else { onFinish(null); } } getSizes() { let thisFile = this.getFile(); let thisName = this.getNameWithoutExt(); let dir = this.getBaseDir() + "/" + this.getDir(); let files = fsx.readdirSync(dir); let sizes = []; for (let i = 0; i < files.length; i++) { let file = files[i]; let name = Utils_1.Utils.getNameWithoutExt(file); if ((thisFile !== file) && name.indexOf(thisName + "-") === 0) sizes.push(name.substr(thisName.length + 1)); } return sizes; } resizeImage(targetSize, onFinish) { if (this.m_config.getMaxImageResizeWidth() > 0 && targetSize["width"] > this.m_config.getMaxImageResizeWidth()) { onFinish(new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.MAX_RESIZE_WIDTH_EXCEEDED, targetSize["width"].toString(), this.getName(), this.m_config.getMaxImageResizeWidth().toString()))); return; } if (this.m_config.getMaxImageResizeHeight() > 0 && targetSize["height"] > this.m_config.getMaxImageResizeHeight()) { onFinish(new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.MAX_RESIZE_HEIGHT_EXCEEDED, targetSize["height"].toString(), this.getName(), this.m_config.getMaxImageResizeHeight().toString()))); return; } let fileSrc = this; if (this.m_mainFile != null) // if this is just a size of main file fileSrc = this.m_mainFile; let fileOriginal = fileSrc.getFileOriginal(); if (fileOriginal.exists()) fileSrc = fileOriginal; let imageWidth = this.getImageWidth(); let imageHeight = this.getImageHeight(); if (targetSize["width"] === 0 && targetSize["height"] === 0) { onFinish(null); return; } if (targetSize["width"] === 0 && targetSize["height"] === imageHeight) { onFinish(null); return; } if (targetSize["height"] === 0 && targetSize["width"] === imageWidth) { onFinish(null); return; } if (targetSize["width"] > 0 && targetSize["height"] > 0 && targetSize["width"] === imageWidth && targetSize["height"] === imageHeight) { onFinish(null); return; } // Calc full target size of image (with paddings) let scaleWWithPadding = -1; let scaleHWithPadding = -1; if (targetSize["width"] > 0 && targetSize["height"] > 0) { scaleWWithPadding = targetSize["width"]; scaleHWithPadding = targetSize["height"]; } else if (targetSize["width"] > 0) { scaleWWithPadding = targetSize["width"]; scaleHWithPadding = Math.floor((scaleWWithPadding / imageWidth) * imageHeight); } else if (targetSize["height"] > 0) { scaleHWithPadding = targetSize["height"]; scaleWWithPadding = Math.floor((scaleHWithPadding / imageHeight) * imageWidth); } if ((scaleWWithPadding > imageWidth || scaleHWithPadding > imageHeight) && !targetSize["enlarge"]) { scaleWWithPadding = imageWidth; scaleHWithPadding = imageHeight; } // Check we have not exceeded max width/height if ((this.m_config.getMaxImageResizeWidth() > 0 && scaleWWithPadding > this.m_config.getMaxImageResizeWidth()) || (this.m_config.getMaxImageResizeHeight() > 0 && scaleHWithPadding > this.m_config.getMaxImageResizeHeight())) { let coef = Math.max(scaleWWithPadding / this.m_config.getMaxImageResizeWidth(), scaleHWithPadding / this.m_config.getMaxImageResizeHeight()); scaleWWithPadding = Math.floor(scaleWWithPadding / coef); scaleHWithPadding = Math.floor(scaleHWithPadding / coef); } // Calc actual size of image (without paddings) let scaleW = -1; let scaleH = -1; if (scaleWWithPadding / imageWidth < scaleHWithPadding / imageHeight) { scaleW = scaleWWithPadding; scaleH = Math.floor((scaleW / imageWidth) * imageHeight); } else { scaleH = scaleHWithPadding; scaleW = Math.floor((scaleH / imageHeight) * imageWidth); } if (scaleWWithPadding === imageWidth && scaleW === imageWidth && scaleHWithPadding === imageHeight && scaleH === imageHeight) { onFinish(-1); // no resize is needed return; } let fitMode = FileCommited.FIT_EXACT; if (targetSize["width"] === 0) fitMode = FileCommited.FIT_TO_HEIGHT; else if (targetSize["height"] === 0) fitMode = FileCommited.FIT_TO_WIDTH; if (scaleWWithPadding > scaleW || scaleHWithPadding > scaleH) { scaleW = scaleWWithPadding; scaleH = scaleHWithPadding; } this .resizeImageNative(this.getImage(), scaleW, scaleH, fitMode) .write(this.getImage(), (err, stdout, stderr, cmd) => { if (err) { onFinish(new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.UNABLE_TO_WRITE_IMAGE_TO_FILE, this.getName()))); } else { onFinish(null); } }); } resizeImageNative(image, scaleW, scaleH, fitMode) { let newW = scaleW; let newH = scaleH; if (fitMode === FileCommited.FIT_TO_WIDTH) { newH = Math.round(newW * scaleH / scaleW); } else if (fitMode === FileCommited.FIT_TO_HEIGHT) { newW = Math.round(newH * scaleW / scaleH); } let newImage = gm.subClass({ "imageMagick": true })(image); newImage.resize(newW, newH).gravity('Center').background('none').extent(newW, newH); return newImage; } isCommited() { return true; } } exports.FileCommited = FileCommited; FileCommited.SIZE_PREVIEW = "preview"; FileCommited.SIZE_FULL = "full"; // fitMode: 0 - fit exact, 1 - fit to width, 2 - fit to height FileCommited.FIT_EXACT = 0; FileCommited.FIT_TO_WIDTH = 1; FileCommited.FIT_TO_HEIGHT = 2; //# sourceMappingURL=FileCommited.js.map