erfan-flmngr-server-fixed
Version:
> Node.js Backend for Flmngr file manager
173 lines • 6.34 kB
JavaScript
"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.AFile = void 0;
const Message_1 = require("../action/resp/Message");
const FileData_1 = require("../action/resp/FileData");
const fsx = __importStar(require("fs-extra"));
const MessageException_1 = require("../MessageException");
const imageSize = require("image-size");
// import * as imageSize from "image-size"
const Utils_1 = require("../file/Utils");
class AFile {
constructor(config, dir, name) {
this.m_name = '';
this.m_dir = '';
this.m_commonErrors = [];
this.m_config = config;
this.m_dir = dir;
this.m_name = name;
}
getData() {
let data = new FileData_1.FileData();
data["isCommited"] = this.isCommited();
data["name"] = this.getName();
data["dir"] = this.getDir();
data["bytes"] = this.getSize();
data["errors"] = this.getErrors();
data["isImage"] = this.isImage();
if (data["isImage"]) {
try {
data["width"] = this.getImageWidth();
data["height"] = this.getImageHeight();
}
catch (e) {
data["width"] = 0;
data["height"] = 0;
}
if (data["isCommited"]) {
data["sizes"] = {};
if (this.m_mainFile == null) {
let modifications = this.getModifications();
for (let i = 0; i < modifications.length; i++)
data["sizes"][modifications[i].getModificationName()] = modifications[i].getData();
}
}
}
return data;
}
getModifications() { return []; }
getModificationName() { return ''; }
getSize() {
let f = this.getFile();
if (fsx.existsSync(f))
return fsx.lstatSync(f).size;
return 0;
}
getErrors() {
let result = [];
for (const item of this.m_commonErrors)
result.push(item);
return result;
}
// Returns do we need to continue check or not
checkForErrors(checkForExist) {
this.m_commonErrors = [];
if (!Utils_1.Utils.isFileNameSyntaxOk(this.getName())) {
this.m_commonErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ERROR_SYNTAX, this.getName()));
return false; // do not do any other checks by security reasons
}
if (checkForExist && !this.exists())
this.m_commonErrors.push(Message_1.Message.createMessage(Message_1.Message.FILE_ERROR_DOES_NOT_EXIST));
return true;
}
setName(name) { this.m_name = name; }
setDir(dir) { this.m_dir = dir; }
getName() { return this.m_name; }
getDir() {
if (this.m_dir.length !== 0 && !(this.m_dir.substr(this.m_dir.length - 1) === "/"))
return this.m_dir + "/";
return this.m_dir;
}
getUrlUploader() {
let dir = this.getDir() + this.getName();
if (dir.startsWith("/"))
dir = dir.substr(1);
return dir;
}
getFullPath() { return this.getBaseDir() + this.getUrlUploader(); }
getExt() {
return Utils_1.Utils.getExt(this.m_name);
}
getNameWithoutExt() {
return Utils_1.Utils.getNameWithoutExt(this.m_name);
}
getFile() { return this.getFullPath(); } // no File object in TypeScript, return string
exists() {
try {
return fsx.existsSync(this.getFile());
}
catch (e) {
// SecurityException
return false;
}
}
delete() {
console.log('im called');
try {
fsx.unlinkSync(this.getFile());
}
catch (e) {
console.log(e);
throw new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.UNABLE_TO_DELETE_FILE, this.getName()));
}
}
isImage() {
return Utils_1.Utils.isImage(this.getName());
}
getImageWidth() {
try {
return imageSize(this.getImage()).width;
}
catch (e) {
console.log(e);
throw new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.IMAGE_PROCESS_ERROR));
}
}
getImageHeight() {
try {
return imageSize(this.getImage()).height;
}
catch (e) {
console.log(e);
throw new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.IMAGE_PROCESS_ERROR));
}
}
getImage() {
return this.getFile(); // no library both for getting dimensions and resizing
}
setFreeFileName() {
let name = Utils_1.Utils.getFreeFileName(this.getBaseDir() + this.getDir(), this.getName(), false);
this.setName(name);
}
copyTo(dstFile) {
try {
fsx.copySync(this.getFile(), dstFile.getFile());
}
catch (e) {
// IOException
console.log(e);
throw new MessageException_1.MessageException(Message_1.Message.createMessage(Message_1.Message.UNABLE_TO_COPY_FILE, this.getName(), dstFile.getName()));
}
}
}
exports.AFile = AFile;
//# sourceMappingURL=AFile.js.map