mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
improving thumbnail generation and directory parsing
This commit is contained in:
parent
79ed8c0c6d
commit
e8d96ea700
@ -1,5 +1,7 @@
|
||||
import {Metadata, SharpInstance} from "@types/sharp";
|
||||
import {Dimensions, State} from "@types/gm";
|
||||
import {Logger} from "../../Logger";
|
||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||
|
||||
export module ThumbnailRenderers {
|
||||
|
||||
@ -60,7 +62,7 @@ export module ThumbnailRenderers {
|
||||
.metadata()
|
||||
.then((metadata: Metadata) => {
|
||||
|
||||
const Logger = require(input.__dirname + "/../../Logger").Logger;
|
||||
// const Logger = require(input.__dirname + "/../../Logger").Logger;
|
||||
Logger.silly("[SharpThRenderer] rendering thumbnail:", input.imagePath);
|
||||
/**
|
||||
* newWidth * newHeight = size*size
|
||||
@ -95,13 +97,13 @@ export module ThumbnailRenderers {
|
||||
.toFile(input.thPath).then(() => {
|
||||
return done();
|
||||
}).catch(function (err) {
|
||||
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
||||
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
||||
// const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
||||
// const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
||||
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
||||
});
|
||||
} catch (err) {
|
||||
const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
||||
const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
||||
// const Error = require(input.__dirname + "/../../../common/entities/Error").Error;
|
||||
// const ErrorCodes = require(input.__dirname + "/../../../common/entities/Error").ErrorCodes;
|
||||
return done(new Error(ErrorCodes.GENERAL_ERROR, err));
|
||||
}
|
||||
});
|
||||
|
@ -15,20 +15,19 @@ import {ThumbnailProcessingLib} from "../../../common/config/private/IPrivateCon
|
||||
import RendererInput = ThumbnailRenderers.RendererInput;
|
||||
|
||||
|
||||
Config.Client.concurrentThumbnailGenerations = Math.max(1, os.cpus().length - 1);
|
||||
|
||||
const Pool = require('threads').Pool;
|
||||
const pool = new Pool(Config.Client.concurrentThumbnailGenerations);
|
||||
|
||||
|
||||
export class ThumbnailGeneratorMWs {
|
||||
private static poolsInited = false;
|
||||
private static ThumbnailFunction = null
|
||||
private static initDone = false;
|
||||
private static ThumbnailFunction = null;
|
||||
private static thPool = null;
|
||||
|
||||
private static initPools() {
|
||||
if (this.poolsInited == true) {
|
||||
public static init() {
|
||||
if (this.initDone == true) {
|
||||
return
|
||||
}
|
||||
|
||||
Config.Client.concurrentThumbnailGenerations = Math.max(1, os.cpus().length - 1);
|
||||
|
||||
|
||||
switch (Config.Server.thumbnail.processingLibrary) {
|
||||
case ThumbnailProcessingLib.Jimp:
|
||||
this.ThumbnailFunction = ThumbnailRenderers.jimp;
|
||||
@ -43,9 +42,15 @@ export class ThumbnailGeneratorMWs {
|
||||
default:
|
||||
throw "Unknown thumbnail processing lib";
|
||||
}
|
||||
pool.run(this.ThumbnailFunction);
|
||||
|
||||
this.poolsInited = true;
|
||||
if (Config.Server.enableThreading == true &&
|
||||
Config.Server.thumbnail.processingLibrary == ThumbnailProcessingLib.Jimp) {
|
||||
const Pool = require('threads').Pool;
|
||||
this.thPool = new Pool(Config.Client.concurrentThumbnailGenerations);
|
||||
this.thPool.run(this.ThumbnailFunction);
|
||||
}
|
||||
|
||||
this.initDone = true;
|
||||
}
|
||||
|
||||
private static addThInfoTODir(directory: DirectoryDTO) {
|
||||
@ -151,7 +156,6 @@ export class ThumbnailGeneratorMWs {
|
||||
fs.mkdirSync(ProjectPath.ThumbnailFolder);
|
||||
}
|
||||
|
||||
this.initPools();
|
||||
//run on other thread
|
||||
|
||||
let input = <RendererInput>{
|
||||
@ -162,8 +166,8 @@ export class ThumbnailGeneratorMWs {
|
||||
qualityPriority: Config.Server.thumbnail.qualityPriority,
|
||||
__dirname: __dirname,
|
||||
};
|
||||
if (Config.Server.enableThreading == true) {
|
||||
pool.send(input)
|
||||
if (this.thPool !== null) {
|
||||
this.thPool.send(input)
|
||||
.on('done', (out) => {
|
||||
return next(out);
|
||||
}).on('error', (error) => {
|
||||
@ -184,3 +188,5 @@ export class ThumbnailGeneratorMWs {
|
||||
return crypto.createHash('md5').update(imagePath).digest('hex') + "_" + size + ".jpg";
|
||||
}
|
||||
}
|
||||
|
||||
ThumbnailGeneratorMWs.init();
|
||||
|
@ -7,7 +7,7 @@ import {diskManagerTask, DiskManagerTask} from "./DiskMangerTask";
|
||||
import {Config} from "../../common/config/private/Config";
|
||||
|
||||
const Pool = require('threads').Pool;
|
||||
const pool = new Pool();
|
||||
const pool = new Pool(1);
|
||||
|
||||
const LOG_TAG = "[DiskManager]";
|
||||
|
||||
|
@ -1,13 +1,7 @@
|
||||
///<reference path="exif.d.ts"/>
|
||||
import {DirectoryDTO} from "../../common/entities/DirectoryDTO";
|
||||
import {
|
||||
CameraMetadata,
|
||||
GPSMetadata,
|
||||
ImageSize,
|
||||
PhotoDTO,
|
||||
PhotoMetadata,
|
||||
PositionMetaData
|
||||
} from "../../common/entities/PhotoDTO";
|
||||
import {CameraMetadata, GPSMetadata, ImageSize, PhotoDTO, PhotoMetadata} from "../../common/entities/PhotoDTO";
|
||||
import {Logger} from "../Logger";
|
||||
|
||||
const LOG_TAG = "[DiskManagerTask]";
|
||||
|
||||
@ -43,13 +37,24 @@ export const diskManagerTask = (input: DiskManagerTask.PoolInput, done) => {
|
||||
if (err) {
|
||||
return reject({file: fullPath, error: err});
|
||||
}
|
||||
const metadata: PhotoMetadata = <PhotoMetadata>{
|
||||
keywords: {},
|
||||
cameraData: {},
|
||||
positionData: {
|
||||
GPSData: {},
|
||||
country: null,
|
||||
state: null,
|
||||
city: null
|
||||
},
|
||||
size: {},
|
||||
creationDate: {}
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
try {
|
||||
const exif = exif_parser.create(data).parse();
|
||||
const iptcData = iptc(data);
|
||||
|
||||
const imageSize: ImageSize = {width: exif.imageSize.width, height: exif.imageSize.height};
|
||||
const cameraData: CameraMetadata = {
|
||||
metadata.cameraData = <CameraMetadata> {
|
||||
ISO: exif.tags.ISO,
|
||||
model: exif.tags.Modeol,
|
||||
maker: exif.tags.Make,
|
||||
@ -58,20 +63,22 @@ export const diskManagerTask = (input: DiskManagerTask.PoolInput, done) => {
|
||||
focalLength: exif.tags.FocalLength,
|
||||
lens: exif.tags.LensModel,
|
||||
};
|
||||
const GPS: GPSMetadata = {
|
||||
metadata.positionData.GPSData = <GPSMetadata> {
|
||||
latitude: exif.tags.GPSLatitude,
|
||||
longitude: exif.tags.GPSLongitude,
|
||||
altitude: exif.tags.GPSAltitude
|
||||
|
||||
};
|
||||
|
||||
const positionData: PositionMetaData = {
|
||||
GPSData: GPS,
|
||||
country: iptcData.country_or_primary_location_name,
|
||||
state: iptcData.province_or_state,
|
||||
city: iptcData.city
|
||||
};
|
||||
metadata.size = <ImageSize> {width: exif.imageSize.width, height: exif.imageSize.height};
|
||||
} catch (err) {
|
||||
Logger.info(LOG_TAG, "Error parsing exif", fullPath);
|
||||
metadata.size = <ImageSize> {width: 1, height: 1};
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const iptcData = iptc(data);
|
||||
//Decode characters to UTF8
|
||||
const decode = (s: any) => {
|
||||
for (let a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
|
||||
@ -82,25 +89,27 @@ export const diskManagerTask = (input: DiskManagerTask.PoolInput, done) => {
|
||||
return s.join("");
|
||||
};
|
||||
|
||||
|
||||
const keywords: string[] = (iptcData.keywords || []).map((s: string) => decode(s));
|
||||
const creationDate: number = iptcData.date_time ? iptcData.date_time.getTime() : 0;
|
||||
metadata.positionData.country = iptcData.country_or_primary_location_name;
|
||||
metadata.positionData.state = iptcData.province_or_state;
|
||||
metadata.positionData.city = iptcData.city;
|
||||
|
||||
|
||||
metadata.keywords = <string[]> (iptcData.keywords || []).map((s: string) => decode(s));
|
||||
metadata.creationDate = <number> iptcData.date_time ? iptcData.date_time.getTime() : 0;
|
||||
} catch (err) {
|
||||
Logger.info(LOG_TAG, "Error parsing iptc data", fullPath);
|
||||
}
|
||||
|
||||
|
||||
const metadata: PhotoMetadata = <PhotoMetadata>{
|
||||
keywords: keywords,
|
||||
cameraData: cameraData,
|
||||
positionData: positionData,
|
||||
size: imageSize,
|
||||
creationDate: creationDate
|
||||
};
|
||||
return resolve(metadata);
|
||||
} catch (err) {
|
||||
return reject({file: fullPath, error: err});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
;
|
||||
|
||||
let parseDir = (directoryInfo: {
|
||||
relativeDirectoryName: string,
|
||||
@ -177,7 +186,8 @@ export const diskManagerTask = (input: DiskManagerTask.PoolInput, done) => {
|
||||
done(err, null);
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
;
|
||||
|
||||
|
||||
export module DiskManagerTask {
|
||||
|
Loading…
x
Reference in New Issue
Block a user