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