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

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2016-03-30 03:57:41 +08:00
///<reference path="jimp.d.ts"/>
2016-03-30 03:46:44 +08:00
import * as path from 'path';
import * as Jimp from 'jimp';
import * as crypto from 'crypto';
import * as fs from 'fs';
import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error";
2016-03-30 03:57:41 +08:00
2016-03-30 03:46:44 +08:00
export class ThumbnailGeneratorMWs {
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 thPath = path.join(__dirname,"/../../demo/TEMP",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);
}
2016-03-30 03:46:44 +08:00
Jimp.read(imagePath).then( (image) => {
if (image.bitmap.with < image.bitmap.height) {
image.resize(size, Jimp.AUTO); // resize
}else{
image.resize(Jimp.AUTO, size); // resize
}
image.quality(60); // set JPEG quality
image.write(thPath, () =>{ // save
return next();
});
}).catch(function (err) {
return next(new Error(ErrorCodes.GENERAL_ERROR));
});
}
private static generateThumbnailName(imagePath:string,size:number):string{
return crypto.createHash('md5').update(imagePath).digest('hex')+"_"+size+".jpg";
}
}