1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

implementing config

This commit is contained in:
Braun Patrik 2016-04-09 23:09:28 +02:00
parent 65d88fe061
commit 8ad13e3e78
3 changed files with 30 additions and 9 deletions

6
backend/config/Config.ts Normal file
View File

@ -0,0 +1,6 @@
export class Config{
public static thumbnailSizes = [200];
public static imagesFolder = "/demo/images";
public static thumbnailFolder = "/demo/TEMP";
}

View File

@ -5,13 +5,18 @@ import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error"; import {Error, ErrorCodes} from "../../common/entities/Error";
import {GalleryManager} from "../model/GalleryManager"; 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 { export class GalleryMWs {
private static getImageFolder(){
return path.join(__dirname,"/../../",Config.imagesFolder);
}
public static listDirectory(req:Request, res:Response, next:NextFunction){ public static listDirectory(req:Request, res:Response, next:NextFunction){
let directoryName = req.params.directory || "/"; 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()){ if(!fs.statSync(absoluteDirectoryName).isDirectory()){
return next(); return next();
@ -32,7 +37,7 @@ export class GalleryMWs {
return next(); 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()){ if(fs.statSync(fullImagePath).isDirectory()){
return next(); return next();
} }

View File

@ -7,29 +7,39 @@ import * as crypto from 'crypto';
import * as fs from 'fs'; import * as fs from 'fs';
import {NextFunction, Request, Response} from "express"; import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error"; import {Error, ErrorCodes} from "../../common/entities/Error";
import {Config} from "../config/Config";
export class ThumbnailGeneratorMWs { export class ThumbnailGeneratorMWs {
private static getThumbnailFolder(){
return path.join(__dirname,"/../../",Config.thumbnailFolder);
}
public static generateThumbnail(req:Request, res:Response, next:NextFunction){ public static generateThumbnail(req:Request, res:Response, next:NextFunction){
if(!req.resultPipe) if(!req.resultPipe)
return next(); return next();
let imagePath = req.resultPipe; 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(thumbnailFolder,ThumbnailGeneratorMWs.generateThumbnailName(imagePath,size));
let thPath = path.join(__dirname,"/../../demo/TEMP",ThumbnailGeneratorMWs.generateThumbnailName(imagePath,size));
req.resultPipe = thPath; req.resultPipe = thPath;
if(fs.existsSync(thPath) === true){ if(fs.existsSync(thPath) === true){
return next(); return next();
} }
let tmpDir = path.join(__dirname,"/../../demo/TEMP"); if (!fs.existsSync(thumbnailFolder)){
if (!fs.existsSync(tmpDir)){ fs.mkdirSync(thumbnailFolder);
fs.mkdirSync(tmpDir);
} }
Jimp.read(imagePath).then( (image) => { Jimp.read(imagePath).then( (image) => {