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