diff --git a/backend/config/Config.ts b/backend/config/Config.ts new file mode 100644 index 00000000..e97050c4 --- /dev/null +++ b/backend/config/Config.ts @@ -0,0 +1,6 @@ + +export class Config{ + public static thumbnailSizes = [200]; + public static imagesFolder = "/demo/images"; + public static thumbnailFolder = "/demo/TEMP"; +} \ No newline at end of file diff --git a/backend/middlewares/GalleryMWs.ts b/backend/middlewares/GalleryMWs.ts index 604cd48c..4ab5dee1 100644 --- a/backend/middlewares/GalleryMWs.ts +++ b/backend/middlewares/GalleryMWs.ts @@ -4,14 +4,19 @@ import * as fs from 'fs'; import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../common/entities/Error"; import {GalleryManager} from "../model/GalleryManager"; -import {Directory} from "../../common/entities/Directory"; +import {Directory} from "../../common/entities/Directory"; +import {Config} from "../config/Config"; export class GalleryMWs { + private static getImageFolder(){ + return path.join(__dirname,"/../../",Config.imagesFolder); + } + public static listDirectory(req:Request, res:Response, next:NextFunction){ let directoryName = req.params.directory || "/"; - let absoluteDirectoryName = path.join(__dirname,"/../../demo/images", directoryName); + let absoluteDirectoryName = path.join(GalleryMWs.getImageFolder(), directoryName); if(!fs.statSync(absoluteDirectoryName).isDirectory()){ return next(); @@ -32,7 +37,7 @@ export class GalleryMWs { return next(); } - let fullImagePath = path.join(__dirname,"/../../demo/images", req.params.imagePath); + let fullImagePath = path.join(GalleryMWs.getImageFolder(), req.params.imagePath); if(fs.statSync(fullImagePath).isDirectory()){ return next(); } diff --git a/backend/middlewares/ThumbnailGeneratorMWs.ts b/backend/middlewares/ThumbnailGeneratorMWs.ts index 71593ecb..b137b4b8 100644 --- a/backend/middlewares/ThumbnailGeneratorMWs.ts +++ b/backend/middlewares/ThumbnailGeneratorMWs.ts @@ -7,29 +7,39 @@ import * as crypto from 'crypto'; import * as fs from 'fs'; import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../common/entities/Error"; +import {Config} from "../config/Config"; export class ThumbnailGeneratorMWs { + private static getThumbnailFolder(){ + return path.join(__dirname,"/../../",Config.thumbnailFolder); + } + public static generateThumbnail(req:Request, res:Response, next:NextFunction){ if(!req.resultPipe) return next(); let imagePath = req.resultPipe; - let size:number = parseInt(req.params.size) || 200; //TODO:put to config + let size:number = parseInt(req.params.size) || Config.thumbnailSizes[0]; + let thumbnailFolder = ThumbnailGeneratorMWs.getThumbnailFolder(); + if(Config.thumbnailSizes.indexOf(size) === -1){ + size = Config.thumbnailSizes[0]; + } - let thPath = path.join(__dirname,"/../../demo/TEMP",ThumbnailGeneratorMWs.generateThumbnailName(imagePath,size)); + let thPath = path.join(thumbnailFolder,ThumbnailGeneratorMWs.generateThumbnailName(imagePath,size)); + + req.resultPipe = thPath; if(fs.existsSync(thPath) === true){ return next(); } - - let tmpDir = path.join(__dirname,"/../../demo/TEMP"); - if (!fs.existsSync(tmpDir)){ - fs.mkdirSync(tmpDir); + + if (!fs.existsSync(thumbnailFolder)){ + fs.mkdirSync(thumbnailFolder); } Jimp.read(imagePath).then( (image) => {