fix(server): parse all img formats and enrich metadata (#547)

* fix(server): use file path instead buffer to reduce memory usage

fix undefined exif data

* fix(server): parse all img formats

* feat(server): enrich metadata

* Format oneliner condition

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
Thanh Pham 2022-08-29 03:43:31 +07:00 committed by GitHub
parent dfaa4969da
commit e745cb5e4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,7 @@ import exifr from 'exifr';
import ffmpeg from 'fluent-ffmpeg';
import { readFile } from 'fs/promises';
import path from 'path';
import sharp from 'sharp';
import { Repository } from 'typeorm/repository/Repository';
@Processor(metadataExtractionQueueName)
@ -50,10 +51,22 @@ export class MetadataExtractionProcessor {
async extractExifInfo(job: Job<IExifExtractionProcessor>) {
try {
const { asset, fileName, fileSize }: { asset: AssetEntity; fileName: string; fileSize: number } = job.data;
const exifData = await exifr.parse(asset.originalPath);
const exifData = await exifr.parse(asset.originalPath, {
tiff: true,
ifd0: true as any,
ifd1: true,
exif: true,
gps: true,
interop: true,
xmp: true,
icc: true,
iptc: true,
jfif: true,
ihdr: true,
});
if (!exifData) {
throw new Error(`can not fetch exif data from file ${asset.originalPath}`);
throw new Error(`can not parse exif data from file ${asset.originalPath}`);
}
const newExif = new ExifEntity();
@ -107,6 +120,23 @@ export class MetadataExtractionProcessor {
newExif.country = country || null;
}
// Enrich metadata
if (!newExif.exifImageHeight || !newExif.exifImageWidth || !newExif.orientation) {
const metadata = await sharp(asset.originalPath).metadata();
if (newExif.exifImageHeight === null) {
newExif.exifImageHeight = metadata.height || null;
}
if (newExif.exifImageWidth === null) {
newExif.exifImageWidth = metadata.width || null;
}
if (newExif.orientation === null) {
newExif.orientation = metadata.orientation !== undefined ? `${metadata.orientation}` : null;
}
}
await this.exifRepository.save(newExif);
} catch (e) {
Logger.error(`Error extracting EXIF ${String(e)}`, 'extractExif');