Selaa lähdekoodia

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>
Thanh Pham 2 vuotta sitten
vanhempi
commit
e745cb5e4b

+ 32 - 2
server/apps/microservices/src/processors/metadata-extraction.processor.ts

@@ -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');