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

279 lines
9.9 KiB
TypeScript
Raw Normal View History

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 {Config} from '../../../common/config/private/Config';
import {ThumbnailTH} from '../../model/threading/ThreadPool';
2018-12-09 01:17:33 +08:00
import {RendererInput, ThumbnailSourceType, ThumbnailWorker} from '../../model/threading/ThumbnailWorker';
2018-11-05 02:28:32 +08:00
import {MediaDTO} from '../../../common/entities/MediaDTO';
2018-12-09 01:17:33 +08:00
import {ITaskExecuter, TaskExecuter} from '../../model/threading/TaskExecuter';
import {FaceRegion, PhotoDTO} from '../../../common/entities/PhotoDTO';
import {PersonWithPhoto} from '../PersonMWs';
import {ServerConfig} from '../../../common/config/private/IPrivateConfig';
2016-03-30 03:57:41 +08:00
2016-03-30 03:46:44 +08:00
export class ThumbnailGeneratorMWs {
private static initDone = false;
2018-12-09 01:17:33 +08:00
private static taskQue: ITaskExecuter<RendererInput, void> = null;
2016-03-30 03:46:44 +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
}
if (Config.Server.Threading.enable === true) {
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);
}
} else {
Config.Client.Thumbnail.concurrentThumbnailGenerations = 1;
}
if (Config.Server.Threading.enable === true &&
Config.Server.Thumbnail.processingLibrary === ServerConfig.ThumbnailProcessingLib.Jimp) {
this.taskQue = new ThumbnailTH(Config.Client.Thumbnail.concurrentThumbnailGenerations);
2017-07-24 04:37:29 +08:00
} else {
2018-12-09 01:17:33 +08:00
this.taskQue = new TaskExecuter(Config.Client.Thumbnail.concurrentThumbnailGenerations,
(input => ThumbnailWorker.render(input, Config.Server.Thumbnail.processingLibrary)));
}
this.initDone = true;
}
public static addThumbnailInformation(req: Request, res: Response, next: NextFunction) {
2018-03-31 03:30:30 +08:00
if (!req.resultPipe) {
return next();
2018-03-31 03:30:30 +08:00
}
2016-12-28 03:55:51 +08:00
try {
const cw: ContentWrapper = req.resultPipe;
if (cw.notModified === true) {
return next();
}
if (cw.directory) {
ThumbnailGeneratorMWs.addThInfoTODir(cw.directory);
}
if (cw.searchResult && cw.searchResult.media) {
ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.media);
}
2016-05-09 23:04:56 +08:00
} catch (error) {
2019-01-20 00:31:36 +08:00
return next(new ErrorDTO(ErrorCodes.SERVER_ERROR, 'error during postprocessing result (adding thumbnail info)', error.toString()));
}
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 addThumbnailInfoForPersons(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe) {
return next();
}
try {
const size: number = Config.Client.Thumbnail.personThumbnailSize;
const persons: PersonWithPhoto[] = req.resultPipe;
for (let i = 0; i < persons.length; i++) {
// load parameters
const mediaPath = path.join(ProjectPath.ImageFolder,
persons[i].samplePhoto.directory.path,
persons[i].samplePhoto.directory.name, persons[i].samplePhoto.name);
// generate thumbnail path
const thPath = path.join(ProjectPath.ThumbnailFolder,
ThumbnailGeneratorMWs.generatePersonThumbnailName(mediaPath, persons[i].samplePhoto.metadata.faces[0], size));
persons[i].readyThumbnail = fs.existsSync(thPath);
}
} catch (error) {
return next(new ErrorDTO(ErrorCodes.SERVER_ERROR, 'error during postprocessing result (adding thumbnail info)', error.toString()));
}
return next();
}
2019-02-15 07:25:55 +08:00
public static async generatePersonThumbnail(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe) {
return next();
}
// load parameters
const photo: PhotoDTO = req.resultPipe;
if (!photo.metadata.faces || photo.metadata.faces.length !== 1) {
return next(new ErrorDTO(ErrorCodes.THUMBNAIL_GENERATION_ERROR, 'Photo does not contain a face'));
}
// load parameters
2019-02-23 06:39:01 +08:00
const mediaPath = path.join(ProjectPath.ImageFolder, photo.directory.path, photo.directory.name, photo.name);
2019-02-15 07:25:55 +08:00
const size: number = Config.Client.Thumbnail.personThumbnailSize;
// generate thumbnail path
const thPath = path.join(ProjectPath.ThumbnailFolder,
ThumbnailGeneratorMWs.generatePersonThumbnailName(mediaPath, photo.metadata.faces[0], size));
2019-02-15 07:25:55 +08:00
req.resultPipe = thPath;
// check if thumbnail already exist
if (fs.existsSync(thPath) === true) {
return next();
}
2019-02-15 07:25:55 +08:00
const margin = {
x: Math.round(photo.metadata.faces[0].box.width * (Config.Server.Thumbnail.personFaceMargin)),
y: Math.round(photo.metadata.faces[0].box.height * (Config.Server.Thumbnail.personFaceMargin))
2019-02-15 07:25:55 +08:00
};
2019-03-03 19:11:16 +08:00
2019-02-15 07:25:55 +08:00
// run on other thread
const input = <RendererInput>{
type: ThumbnailSourceType.Image,
mediaPath: mediaPath,
size: size,
thPath: thPath,
makeSquare: false,
cut: {
2019-03-11 03:57:27 +08:00
left: Math.round(Math.max(0, photo.metadata.faces[0].box.left - margin.x / 2)),
top: Math.round(Math.max(0, photo.metadata.faces[0].box.top - margin.y / 2)),
2019-02-15 07:25:55 +08:00
width: photo.metadata.faces[0].box.width + margin.x,
height: photo.metadata.faces[0].box.height + margin.y
},
qualityPriority: Config.Server.Thumbnail.qualityPriority
2019-02-15 07:25:55 +08:00
};
2019-03-03 19:11:16 +08:00
input.cut.width = Math.min(input.cut.width, photo.metadata.size.width - input.cut.left);
input.cut.height = Math.min(input.cut.height, photo.metadata.size.height - input.cut.top);
2019-02-15 07:25:55 +08:00
try {
await ThumbnailGeneratorMWs.taskQue.execute(input);
return next();
} catch (error) {
return next(new ErrorDTO(ErrorCodes.THUMBNAIL_GENERATION_ERROR,
'Error during generating face thumbnail: ' + input.mediaPath, error.toString()));
}
}
2018-11-05 02:28:32 +08:00
public static generateThumbnailFactory(sourceType: ThumbnailSourceType) {
return (req: Request, res: Response, next: NextFunction) => {
if (!req.resultPipe) {
return next();
}
2018-11-05 02:28:32 +08:00
// load parameters
const mediaPath = req.resultPipe;
let size: number = parseInt(req.params.size, 10) || Config.Client.Thumbnail.thumbnailSizes[0];
2018-11-05 02:28:32 +08:00
// validate size
if (Config.Client.Thumbnail.thumbnailSizes.indexOf(size) === -1) {
size = Config.Client.Thumbnail.thumbnailSizes[0];
}
2018-11-05 02:28:32 +08:00
ThumbnailGeneratorMWs.generateImage(mediaPath, size, sourceType, false, req, res, next);
};
}
2018-11-05 02:28:32 +08:00
public static generateIconFactory(sourceType: ThumbnailSourceType) {
return (req: Request, res: Response, next: NextFunction) => {
if (!req.resultPipe) {
return next();
}
2017-06-04 21:25:08 +08:00
2018-11-05 02:28:32 +08:00
// load parameters
const mediaPath = req.resultPipe;
const size: number = Config.Client.Thumbnail.iconSize;
ThumbnailGeneratorMWs.generateImage(mediaPath, size, sourceType, true, req, res, next);
2018-11-05 02:28:32 +08:00
};
}
2016-05-09 23:04:56 +08:00
2019-02-15 07:25:55 +08:00
public static generateThumbnailName(mediaPath: string, size: number): string {
return crypto.createHash('md5').update(mediaPath).digest('hex') + '_' + size + '.jpg';
}
public static generatePersonThumbnailName(mediaPath: string, faceRegion: FaceRegion, size: number): string {
2019-03-11 03:57:27 +08:00
return crypto.createHash('md5').update(mediaPath + '_' + faceRegion.name + '_' + faceRegion.box.left + '_' + faceRegion.box.top)
.digest('hex') + '_' + size + '.jpg';
2019-02-15 07:25:55 +08:00
}
2018-03-31 03:30:30 +08:00
private static addThInfoTODir(directory: DirectoryDTO) {
if (typeof directory.media !== 'undefined') {
ThumbnailGeneratorMWs.addThInfoToPhotos(directory.media);
2018-03-31 03:30:30 +08:00
}
if (typeof directory.directories !== 'undefined') {
for (let i = 0; i < directory.directories.length; i++) {
ThumbnailGeneratorMWs.addThInfoTODir(directory.directories[i]);
}
2018-03-31 03:30:30 +08:00
}
}
2018-11-05 02:28:32 +08:00
private static addThInfoToPhotos(photos: MediaDTO[]) {
2018-03-31 03:30:30 +08:00
const thumbnailFolder = ProjectPath.ThumbnailFolder;
for (let i = 0; i < photos.length; i++) {
2018-11-05 02:28:32 +08:00
const fullMediaPath = path.join(ProjectPath.ImageFolder, photos[i].directory.path, photos[i].directory.name, photos[i].name);
2018-03-31 03:30:30 +08:00
for (let j = 0; j < Config.Client.Thumbnail.thumbnailSizes.length; j++) {
const size = Config.Client.Thumbnail.thumbnailSizes[j];
2018-11-05 02:28:32 +08:00
const thPath = path.join(thumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(fullMediaPath, size));
2018-03-31 03:30:30 +08:00
if (fs.existsSync(thPath) === true) {
2018-12-09 01:17:33 +08:00
if (typeof photos[i].readyThumbnails === 'undefined') {
2018-03-31 03:30:30 +08:00
photos[i].readyThumbnails = [];
}
photos[i].readyThumbnails.push(size);
}
}
const iconPath = path.join(thumbnailFolder,
2018-11-05 02:28:32 +08:00
ThumbnailGeneratorMWs.generateThumbnailName(fullMediaPath, 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-11-05 02:28:32 +08:00
private static async generateImage(mediaPath: string,
size: number,
2018-11-05 02:28:32 +08:00
sourceType: ThumbnailSourceType,
makeSquare: boolean,
req: Request, res: Response, next: NextFunction) {
2018-03-31 03:30:30 +08:00
// generate thumbnail path
2018-11-05 02:28:32 +08:00
const thPath = path.join(ProjectPath.ThumbnailFolder, ThumbnailGeneratorMWs.generateThumbnailName(mediaPath, size));
2016-05-09 23:04:56 +08:00
req.resultPipe = thPath;
2018-03-31 03:30:30 +08:00
// check if thumbnail already exist
if (fs.existsSync(thPath) === true) {
return next();
2016-03-30 03:46:44 +08:00
}
2018-03-31 03:30:30 +08:00
// run on other thread
const input = <RendererInput>{
2018-11-05 02:28:32 +08:00
type: sourceType,
mediaPath: mediaPath,
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) {
2018-11-21 23:07:37 +08:00
return next(new ErrorDTO(ErrorCodes.THUMBNAIL_GENERATION_ERROR,
'Error during generating thumbnail: ' + input.mediaPath, error.toString()));
}
}
2017-06-11 04:56:23 +08:00
}