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

118 lines
3.9 KiB
TypeScript
Raw Normal View History

2016-05-26 02:17:42 +08:00
///<reference path="customtypings/jimp.d.ts"/>
2016-03-30 03:57:41 +08:00
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";
import {ContentWrapper} from "../../common/entities/ConentWrapper";
import {Directory} from "../../common/entities/Directory";
import {ProjectPath} from "../ProjectPath";
import {Photo} from "../../common/entities/Photo";
2016-03-30 03:57:41 +08:00
2016-03-30 03:46:44 +08:00
export class ThumbnailGeneratorMWs {
private static addThInfoTODir(directory:Directory) {
ThumbnailGeneratorMWs.addThInfoToPhotos(directory.photos);
for (let i = 0; i < directory.directories.length; i++) {
ThumbnailGeneratorMWs.addThInfoTODir(directory.directories[i]);
}
}
private static addThInfoToPhotos(photos:Array<Photo>) {
let thumbnailFolder = ProjectPath.ThumbnailFolder;
for (let j = 0; j < Config.Client.thumbnailSizes.length; j++) {
let size = Config.Client.thumbnailSizes[j];
for (let i = 0; i < photos.length; i++) {
let fullImagePath = path.join(ProjectPath.ImageFolder, photos[i].directory.path, photos[i].directory.name, photos[i].name);
let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullImagePath, size));
if (fs.existsSync(thPath) === true) {
photos[i].readyThumbnails.push(size);
}
}
}
}
public static addThumbnailInformation(req:Request, res:Response, next:NextFunction) {
if (!req.resultPipe)
return next();
let cw:ContentWrapper = req.resultPipe;
if (cw.directory) {
ThumbnailGeneratorMWs.addThInfoTODir(cw.directory);
}
if (cw.searchResult) {
ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.photos);
}
return next();
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();
2016-05-12 17:00:46 +08:00
//load parameters
2016-03-30 03:46:44 +08:00
let imagePath = req.resultPipe;
let size:number = parseInt(req.params.size) || Config.Client.thumbnailSizes[0];
2016-04-10 05:09:28 +08:00
2016-05-12 17:00:46 +08:00
//validate size
if (Config.Client.thumbnailSizes.indexOf(size) === -1) {
size = Config.Client.thumbnailSizes[0];
2016-04-10 05:09:28 +08:00
}
2016-03-30 03:46:44 +08:00
2016-05-12 17:00:46 +08:00
//generate thumbnail path
let thPath = path.join(ProjectPath.ThumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(imagePath, size));
2016-05-09 23:04:56 +08:00
2016-03-30 03:46:44 +08:00
req.resultPipe = thPath;
2016-05-12 17:00:46 +08:00
//check if thumbnail already exist
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
2016-05-12 17:00:46 +08:00
//create thumbnail folder if not exist
if (!fs.existsSync(ProjectPath.ThumbnailFolder)) {
fs.mkdirSync(ProjectPath.ThumbnailFolder);
}
2016-05-12 17:00:46 +08:00
//generate thumbnail
2016-05-09 23:04:56 +08:00
Jimp.read(imagePath).then((image) => {
2016-05-12 17:00:46 +08:00
/**
* newWidth * newHeight = size*size
* newHeight/newWidth = height/width
*
* newHeight = (height/width)*newWidth
* newWidth * newWidth = (size*size) / (height/width)
*
* @type {number}
*/
let ratio = image.bitmap.height / image.bitmap.width;
let newWidth = Math.sqrt((size * size) / ratio);
image.resize(newWidth, Jimp.AUTO, Jimp.RESIZE_BEZIER);
2016-03-30 03:46:44 +08:00
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-12 17:00:46 +08:00
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
}
}