1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-01-08 04:03:48 +02:00

Make Metadaloader use async await #792

This commit is contained in:
Patrik J. Braun 2023-11-30 20:37:26 +01:00
parent ed811a051d
commit 7f7954d5d2

View File

@ -12,18 +12,19 @@ import {IptcParser} from 'ts-node-iptc';
import {FFmpegFactory} from '../FFmpegFactory';
import {FfprobeData} from 'fluent-ffmpeg';
import {Utils} from '../../../common/Utils';
import { ExtensionDecorator } from '../extension/ExtensionDecorator';
import {ExtensionDecorator} from '../extension/ExtensionDecorator';
import * as exifr from 'exifr';
import * as path from 'path';
import * as util from 'node:util';
import {FileHandle} from 'fs/promises';
const LOG_TAG = '[MetadataLoader]';
const ffmpeg = FFmpegFactory.get();
export class MetadataLoader {
@ExtensionDecorator(e=>e.gallery.MetadataLoader.loadVideoMetadata)
public static loadVideoMetadata(fullPath: string): Promise<VideoMetadata> {
return new Promise<VideoMetadata>((resolve) => {
@ExtensionDecorator(e => e.gallery.MetadataLoader.loadVideoMetadata)
public static async loadVideoMetadata(fullPath: string): Promise<VideoMetadata> {
const metadata: VideoMetadata = {
size: {
width: 1,
@ -48,15 +49,14 @@ export class MetadataLoader {
for (const sidecarPath of sidecarPaths) {
if (fs.existsSync(sidecarPath)) {
const sidecarData = exifr.sidecar(sidecarPath);
sidecarData.then((response) => {
metadata.keywords = [(response as any).dc.subject].flat();
metadata.rating = (response as any).xmp.Rating;
});
const sidecarData = await exifr.sidecar(sidecarPath);
metadata.keywords = [(sidecarData as any).dc.subject].flat();
metadata.rating = (sidecarData as any).xmp.Rating;
}
}
} catch (err) {
// ignoring errors
Logger.silly(LOG_TAG, 'Error loading sidecar metadata for : ' + fullPath);
Logger.silly(err);
}
try {
@ -64,13 +64,16 @@ export class MetadataLoader {
metadata.fileSize = stat.size;
metadata.creationDate = stat.mtime.getTime();
} catch (err) {
console.log(err);
// ignoring errors
}
try {
ffmpeg(fullPath).ffprobe((err: unknown, data: FfprobeData) => {
if (!!err || data === null || !data.streams[0]) {
return resolve(metadata);
}
const data: FfprobeData = await util.promisify<FfprobeData>(
// wrap to arrow function otherwise 'this' is lost for ffprobe
(cb) => ffmpeg(fullPath).ffprobe(cb)
)();
try {
for (const stream of data.streams) {
@ -140,15 +143,15 @@ export class MetadataLoader {
// eslint-disable-next-line no-empty
} catch (err) {
Logger.silly(LOG_TAG, 'Error loading metadata for : ' + fullPath);
Logger.silly(err);
}
metadata.creationDate = metadata.creationDate || 0;
return resolve(metadata);
});
} catch (e) {
return resolve(metadata);
} catch (err) {
Logger.silly(LOG_TAG, 'Error loading metadata for : ' + fullPath);
Logger.silly(err);
}
});
return metadata;
}
private static readonly EMPTY_METADATA: PhotoMetadata = {
@ -157,25 +160,27 @@ export class MetadataLoader {
fileSize: 0,
};
@ExtensionDecorator(e=>e.gallery.MetadataLoader.loadPhotoMetadata)
public static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
return new Promise<PhotoMetadata>((resolve, reject) => {
try {
const fd = fs.openSync(fullPath, 'r');
const data = Buffer.allocUnsafe(Config.Media.photoMetadataSize);
fs.read(fd, data, 0, Config.Media.photoMetadataSize, 0, (err) => {
fs.closeSync(fd);
@ExtensionDecorator(e => e.gallery.MetadataLoader.loadPhotoMetadata)
public static async loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
let fileHandle: FileHandle;
const metadata: PhotoMetadata = {
size: {width: 1, height: 1},
creationDate: 0,
fileSize: 0,
};
if (err) {
try {
const data = Buffer.allocUnsafe(Config.Media.photoMetadataSize);
fileHandle = await fs.promises.open(fullPath, 'r');
try {
await fileHandle.read(data, 0, Config.Media.photoMetadataSize, 0);
} catch (err) {
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
console.error(err);
return resolve(MetadataLoader.EMPTY_METADATA);
return MetadataLoader.EMPTY_METADATA;
} finally {
await fileHandle.close();
}
try {
try {
const stat = fs.statSync(fullPath);
@ -197,11 +202,9 @@ export class MetadataLoader {
for (const sidecarPath of sidecarPaths) {
if (fs.existsSync(sidecarPath)) {
const sidecarData = exifr.sidecar(sidecarPath);
sidecarData.then((response) => {
metadata.keywords = [(response as any).dc.subject].flat();
metadata.rating = (response as any).xmp.Rating;
});
const sidecarData = await exifr.sidecar(sidecarPath);
metadata.keywords = [(sidecarData as any).dc.subject].flat();
metadata.rating = (sidecarData as any).xmp.Rating;
}
}
} catch (err) {
@ -505,17 +508,18 @@ export class MetadataLoader {
// ignoring errors
}
return resolve(metadata);
} catch (err) {
return reject({file: fullPath, error: err});
}
});
} catch (err) {
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
console.error(err);
return resolve(MetadataLoader.EMPTY_METADATA);
return MetadataLoader.EMPTY_METADATA;
}
});
} catch (err) {
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
console.error(err);
return MetadataLoader.EMPTY_METADATA;
}
return metadata;
}
}