2016-03-30 03:57:41 +08:00
|
|
|
///<reference path="jimp.d.ts"/>
|
|
|
|
|
2016-03-30 03:46:44 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
import * as path from "path";
|
|
|
|
import * as Jimp from "jimp";
|
|
|
|
import * as crypto from "crypto";
|
|
|
|
import * as fs from "fs";
|
2016-03-30 03:46:44 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
|
|
|
import {Error, ErrorCodes} from "../../common/entities/Error";
|
2016-05-09 23:04:56 +08:00
|
|
|
import {Config} from "../config/Config";
|
2016-03-30 03:57:41 +08:00
|
|
|
|
|
|
|
|
2016-03-30 03:46:44 +08:00
|
|
|
export class ThumbnailGeneratorMWs {
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private static getThumbnailFolder() {
|
2016-05-11 03:33:58 +08:00
|
|
|
return path.join(__dirname, "/../../", Config.Server.thumbnailFolder);
|
2016-04-10 05:09:28 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public static generateThumbnail(req:Request, res:Response, next:NextFunction) {
|
|
|
|
if (!req.resultPipe)
|
2016-03-30 03:46:44 +08:00
|
|
|
return next();
|
|
|
|
|
|
|
|
let imagePath = req.resultPipe;
|
2016-05-11 03:33:58 +08:00
|
|
|
let size:number = parseInt(req.params.size) || Config.Server.thumbnailSizes[0];
|
2016-04-10 05:09:28 +08:00
|
|
|
let thumbnailFolder = ThumbnailGeneratorMWs.getThumbnailFolder();
|
|
|
|
|
2016-05-11 03:33:58 +08:00
|
|
|
if (Config.Server.thumbnailSizes.indexOf(size) === -1) {
|
|
|
|
size = Config.Server.thumbnailSizes[0];
|
2016-04-10 05:09:28 +08:00
|
|
|
}
|
2016-03-30 03:46:44 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(imagePath, size));
|
|
|
|
|
2016-03-30 03:46:44 +08:00
|
|
|
|
|
|
|
req.resultPipe = thPath;
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
if (fs.existsSync(thPath) === true) {
|
2016-03-30 03:46:44 +08:00
|
|
|
return next();
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
if (!fs.existsSync(thumbnailFolder)) {
|
2016-04-10 05:09:28 +08:00
|
|
|
fs.mkdirSync(thumbnailFolder);
|
2016-03-30 04:09:02 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
Jimp.read(imagePath).then((image) => {
|
2016-03-30 03:46:44 +08:00
|
|
|
if (image.bitmap.with < image.bitmap.height) {
|
|
|
|
image.resize(size, Jimp.AUTO); // resize
|
2016-05-09 23:04:56 +08:00
|
|
|
} else {
|
2016-03-30 03:46:44 +08:00
|
|
|
image.resize(Jimp.AUTO, size); // resize
|
|
|
|
}
|
|
|
|
|
|
|
|
image.quality(60); // set JPEG quality
|
2016-05-09 23:04:56 +08:00
|
|
|
image.write(thPath, () => { // save
|
2016-03-30 03:46:44 +08:00
|
|
|
return next();
|
2016-05-09 23:04:56 +08:00
|
|
|
});
|
2016-03-30 03:46:44 +08:00
|
|
|
}).catch(function (err) {
|
2016-05-09 23:04:56 +08:00
|
|
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
2016-03-30 03:46:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private static generateThumbnailName(imagePath:string, size:number):string {
|
|
|
|
return crypto.createHash('md5').update(imagePath).digest('hex') + "_" + size + ".jpg";
|
2016-03-30 03:46:44 +08:00
|
|
|
}
|
|
|
|
}
|