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

172 lines
5.6 KiB
TypeScript
Raw Normal View History

///<reference path="../customtypings/jimp.d.ts"/>
2016-05-09 23:04:56 +08:00
import * as path from "path";
import * as crypto from "crypto";
import * as fs from "fs";
import * as os from "os";
2016-03-30 03:46:44 +08:00
import {NextFunction, Request, Response} from "express";
2017-07-15 18:47:11 +08:00
import {ErrorCodes, ErrorDTO} from "../../../common/entities/Error";
import {ContentWrapper} from "../../../common/entities/ConentWrapper";
import {DirectoryDTO} from "../../../common/entities/DirectoryDTO";
import {ProjectPath} from "../../ProjectPath";
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
2017-06-04 21:25:08 +08:00
import {Config} from "../../../common/config/private/Config";
import {ThumbnailProcessingLib} from "../../../common/config/private/IPrivateConfig";
import {ThumbnailTH} from "../../model/threading/ThreadPool";
2017-07-24 04:37:29 +08:00
import {RendererInput} from "../../model/threading/ThumbnailWoker";
import {ITaskQue, TaskQue} from "../../model/threading/TaskQue";
2016-03-30 03:57:41 +08:00
2016-03-30 03:46:44 +08:00
export class ThumbnailGeneratorMWs {
private static initDone = false;
2017-07-24 04:37:29 +08:00
private static taskQue: ITaskQue = null;
2016-03-30 03:46:44 +08:00
public static init() {
if (this.initDone == true) {
return
2017-06-04 21:25:08 +08:00
}
if (Config.Server.enableThreading == true ||
Config.Server.thumbnail.processingLibrary != ThumbnailProcessingLib.Jimp) {
Config.Client.concurrentThumbnailGenerations = Math.max(1, os.cpus().length - 1);
} else {
Config.Client.concurrentThumbnailGenerations = 1;
}
if (Config.Server.enableThreading == true &&
Config.Server.thumbnail.processingLibrary == ThumbnailProcessingLib.Jimp) {
2017-07-24 04:37:29 +08:00
this.taskQue = new ThumbnailTH(Config.Client.concurrentThumbnailGenerations);
} else {
this.taskQue = new TaskQue(Config.Client.concurrentThumbnailGenerations);
}
this.initDone = true;
}
private static addThInfoTODir(directory: DirectoryDTO) {
if (typeof directory.photos == "undefined") {
directory.photos = [];
}
if (typeof directory.directories == "undefined") {
directory.directories = [];
}
ThumbnailGeneratorMWs.addThInfoToPhotos(directory.photos);
for (let i = 0; i < directory.directories.length; i++) {
ThumbnailGeneratorMWs.addThInfoTODir(directory.directories[i]);
}
}
private static addThInfoToPhotos(photos: Array<PhotoDTO>) {
let thumbnailFolder = ProjectPath.ThumbnailFolder;
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);
2017-07-15 18:47:11 +08:00
for (let j = 0; j < Config.Client.Thumbnail.thumbnailSizes.length; j++) {
let size = Config.Client.Thumbnail.thumbnailSizes[j];
let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullImagePath, size));
if (fs.existsSync(thPath) === true) {
if (typeof photos[i].readyThumbnails == "undefined") {
photos[i].readyThumbnails = [];
}
photos[i].readyThumbnails.push(size);
}
}
2017-07-15 18:47:11 +08:00
let iconPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullImagePath, Config.Client.Thumbnail.iconSize));
if (fs.existsSync(iconPath) === true) {
photos[i].readyIcon = true;
}
}
}
public static addThumbnailInformation(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
2016-12-28 03:55:51 +08:00
let cw: ContentWrapper = req.resultPipe;
if (cw.notModified == true) {
2017-07-20 02:47:09 +08:00
return next();
}
if (cw.directory) {
2017-07-20 02:47:09 +08:00
ThumbnailGeneratorMWs.addThInfoTODir(<DirectoryDTO>cw.directory);
}
if (cw.searchResult) {
ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.photos);
2016-04-10 05:09:28 +08:00
}
2016-05-09 23:04:56 +08:00
2016-03-30 03:46:44 +08:00
return next();
2016-04-10 05:09:28 +08:00
}
2016-03-30 03:46:44 +08:00
public static generateThumbnail(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
//load parameters
let imagePath = req.resultPipe;
2017-07-15 18:47:11 +08:00
let size: number = parseInt(req.params.size) || Config.Client.Thumbnail.thumbnailSizes[0];
//validate size
2017-07-15 18:47:11 +08:00
if (Config.Client.Thumbnail.thumbnailSizes.indexOf(size) === -1) {
size = Config.Client.Thumbnail.thumbnailSizes[0];
}
ThumbnailGeneratorMWs.generateImage(imagePath, size, false, req, res, next);
}
public static generateIcon(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
//load parameters
let imagePath = req.resultPipe;
2017-07-15 18:47:11 +08:00
let size: number = Config.Client.Thumbnail.iconSize;
ThumbnailGeneratorMWs.generateImage(imagePath, size, true, req, res, next);
2017-06-04 21:25:08 +08:00
}
2016-05-09 23:04:56 +08:00
2016-03-30 03:46:44 +08:00
private static async generateImage(imagePath: string, size: number, makeSquare: boolean, req: Request, res: Response, next: NextFunction) {
//generate thumbnail path
let thPath = path.join(ProjectPath.ThumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(imagePath, size));
2016-05-09 23:04:56 +08:00
req.resultPipe = thPath;
//check if thumbnail already exist
if (fs.existsSync(thPath) === true) {
return next();
2016-03-30 03:46:44 +08:00
}
//create thumbnail folder if not exist
if (!fs.existsSync(ProjectPath.ThumbnailFolder)) {
fs.mkdirSync(ProjectPath.ThumbnailFolder);
2016-03-30 03:46:44 +08:00
}
//run on other thread
let input = <RendererInput>{
imagePath: imagePath,
size: size,
thPath: thPath,
makeSquare: makeSquare,
qualityPriority: Config.Server.thumbnail.qualityPriority
};
try {
2017-07-24 04:37:29 +08:00
await this.taskQue.execute(input);
return next();
} catch (error) {
2017-07-15 23:29:40 +08:00
return next(new ErrorDTO(ErrorCodes.THUMBNAIL_GENERATION_ERROR, "Error during generating thumbnail", error));
}
}
private static generateThumbnailName(imagePath: string, size: number): string {
return crypto.createHash('md5').update(imagePath).digest('hex') + "_" + size + ".jpg";
}
2017-06-11 04:56:23 +08:00
}