2017-05-28 18:33:18 +08:00
|
|
|
///<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";
|
2017-01-23 03:30:23 +08:00
|
|
|
import * as os from "os";
|
2016-03-30 03:46:44 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2017-05-28 18:33:18 +08:00
|
|
|
import {Error, ErrorCodes} 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";
|
|
|
|
import {hardwareRenderer, softwareRenderer} from "./THRenderers";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../../common/config/private/Config";
|
2016-03-30 03:57:41 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
|
|
|
|
Config.Client.concurrentThumbnailGenerations = Math.max(1, os.cpus().length - 1);
|
|
|
|
|
2017-01-22 05:54:43 +08:00
|
|
|
const Pool = require('threads').Pool;
|
2017-01-23 03:30:23 +08:00
|
|
|
const pool = new Pool(Config.Client.concurrentThumbnailGenerations);
|
2017-01-22 05:54:43 +08:00
|
|
|
|
2017-01-23 03:30:23 +08:00
|
|
|
|
2016-03-30 03:46:44 +08:00
|
|
|
export class ThumbnailGeneratorMWs {
|
2017-06-04 21:25:08 +08:00
|
|
|
private static poolsInited = false;
|
2016-03-30 03:46:44 +08:00
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static initPools() {
|
|
|
|
if (this.poolsInited == true) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (Config.Server.thumbnail.hardwareAcceleration == true) {
|
|
|
|
pool.run(hardwareRenderer);
|
|
|
|
} else {
|
|
|
|
pool.run(softwareRenderer);
|
|
|
|
}
|
|
|
|
this.poolsInited = true;
|
|
|
|
}
|
2016-06-22 22:34:44 +08:00
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
private static addThInfoTODir(directory: DirectoryDTO) {
|
|
|
|
if (typeof directory.photos == "undefined") {
|
|
|
|
directory.photos = [];
|
|
|
|
}
|
|
|
|
if (typeof directory.directories == "undefined") {
|
|
|
|
directory.directories = [];
|
|
|
|
}
|
2016-06-22 22:34:44 +08:00
|
|
|
ThumbnailGeneratorMWs.addThInfoToPhotos(directory.photos);
|
|
|
|
|
|
|
|
for (let i = 0; i < directory.directories.length; i++) {
|
|
|
|
ThumbnailGeneratorMWs.addThInfoTODir(directory.directories[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
private static addThInfoToPhotos(photos: Array<PhotoDTO>) {
|
2016-06-22 22:34:44 +08:00
|
|
|
let thumbnailFolder = ProjectPath.ThumbnailFolder;
|
2017-03-21 04:37:23 +08:00
|
|
|
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);
|
|
|
|
for (let j = 0; j < Config.Client.thumbnailSizes.length; j++) {
|
|
|
|
let size = Config.Client.thumbnailSizes[j];
|
2016-06-22 22:34:44 +08:00
|
|
|
let thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullImagePath, size));
|
|
|
|
if (fs.existsSync(thPath) === true) {
|
2016-12-28 03:55:51 +08:00
|
|
|
if (typeof photos[i].readyThumbnails == "undefined") {
|
|
|
|
photos[i].readyThumbnails = [];
|
|
|
|
}
|
2016-06-22 22:34:44 +08:00
|
|
|
photos[i].readyThumbnails.push(size);
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
let iconPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullImagePath, Config.Client.iconSize));
|
|
|
|
if (fs.existsSync(iconPath) === true) {
|
|
|
|
photos[i].readyIcon = true;
|
|
|
|
}
|
|
|
|
|
2016-06-22 22:34:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
public static addThumbnailInformation(req: Request, res: Response, next: NextFunction) {
|
2016-06-22 22:34:44 +08:00
|
|
|
if (!req.resultPipe)
|
|
|
|
return next();
|
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
let cw: ContentWrapper = req.resultPipe;
|
2016-06-22 22:34:44 +08:00
|
|
|
if (cw.directory) {
|
|
|
|
ThumbnailGeneratorMWs.addThInfoTODir(cw.directory);
|
|
|
|
}
|
|
|
|
if (cw.searchResult) {
|
|
|
|
ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.photos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return next();
|
2016-12-28 03:55:51 +08:00
|
|
|
|
2016-04-10 05:09:28 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
public static generateThumbnail(req: Request, res: Response, next: NextFunction) {
|
2016-05-09 23:04:56 +08:00
|
|
|
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;
|
2016-12-28 03:55:51 +08:00
|
|
|
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
|
|
|
|
2017-01-23 05:31:29 +08:00
|
|
|
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-03-21 04:37:23 +08:00
|
|
|
let size: number = Config.Client.iconSize;
|
2017-01-23 05:31:29 +08:00
|
|
|
ThumbnailGeneratorMWs.generateImage(imagePath, size, true, req, res, next);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
|
2017-03-21 04:37:23 +08:00
|
|
|
private static generateImage(imagePath: string, size: number, makeSquare: boolean, req: Request, res: Response, next: NextFunction) {
|
2017-01-23 05:31:29 +08:00
|
|
|
|
2016-05-12 17:00:46 +08:00
|
|
|
//generate thumbnail path
|
2016-06-22 22:34:44 +08:00
|
|
|
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
|
2016-06-22 22:34:44 +08:00
|
|
|
if (!fs.existsSync(ProjectPath.ThumbnailFolder)) {
|
|
|
|
fs.mkdirSync(ProjectPath.ThumbnailFolder);
|
2016-03-30 04:09:02 +08:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
this.initPools();
|
2017-01-22 05:54:43 +08:00
|
|
|
//run on other thread
|
2017-05-28 18:33:18 +08:00
|
|
|
pool.send({imagePath: imagePath, size: size, thPath: thPath, makeSquare: makeSquare, __dirname: __dirname})
|
2017-01-22 05:54:43 +08:00
|
|
|
.on('done', (out) => {
|
|
|
|
return next(out);
|
2017-05-28 18:33:18 +08:00
|
|
|
}).on('error', (error) => {
|
|
|
|
console.log(error);
|
|
|
|
return next(new Error(ErrorCodes.THUMBNAIL_GENERATION_ERROR, error));
|
2016-03-30 03:46:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-28 03:55:51 +08:00
|
|
|
private static generateThumbnailName(imagePath: string, size: number): string {
|
2016-05-09 23:04:56 +08:00
|
|
|
return crypto.createHash('md5').update(imagePath).digest('hex') + "_" + size + ".jpg";
|
2016-03-30 03:46:44 +08:00
|
|
|
}
|
|
|
|
}
|