From 6173350a6982c451877a478a57861c8807c9051d Mon Sep 17 00:00:00 2001 From: Martin Kampas Date: Fri, 1 Mar 2024 22:24:46 +0100 Subject: [PATCH] Fix errors reading properties of undefined --- .../model/fileaccess/MetadataLoader.ts | 77 +++++++++++-------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/src/backend/model/fileaccess/MetadataLoader.ts b/src/backend/model/fileaccess/MetadataLoader.ts index d13a3796..80ac4d2f 100644 --- a/src/backend/model/fileaccess/MetadataLoader.ts +++ b/src/backend/model/fileaccess/MetadataLoader.ts @@ -82,9 +82,14 @@ export class MetadataLoader { if (Utils.isInt32(parseInt(stream.avg_frame_rate, 10))) { metadata.fps = parseInt(stream.avg_frame_rate, 10) || null; } - metadata.creationDate = - Date.parse(stream.tags.creation_time) || - metadata.creationDate; + if ( + stream.tags !== undefined && + typeof stream.tags.creation_time === 'string' + ) { + metadata.creationDate = + Date.parse(stream.tags.creation_time) || + metadata.creationDate; + } break; } } @@ -139,22 +144,26 @@ export class MetadataLoader { if (fs.existsSync(sidecarPath)) { const sidecarData = await exifr.sidecar(sidecarPath); if (sidecarData !== undefined) { - if ((sidecarData as SideCar).dc.subject !== undefined) { - if (metadata.keywords === undefined) { - metadata.keywords = []; - } - let keywords = (sidecarData as SideCar).dc.subject || []; - if (typeof keywords === 'string') { - keywords = [keywords]; - } - for (const kw of keywords) { - if (metadata.keywords.indexOf(kw) === -1) { - metadata.keywords.push(kw); + if ((sidecarData as SideCar).dc !== undefined) { + if ((sidecarData as SideCar).dc.subject !== undefined) { + if (metadata.keywords === undefined) { + metadata.keywords = []; + } + let keywords = (sidecarData as SideCar).dc.subject || []; + if (typeof keywords === 'string') { + keywords = [keywords]; + } + for (const kw of keywords) { + if (metadata.keywords.indexOf(kw) === -1) { + metadata.keywords.push(kw); + } } } } - if ((sidecarData as SideCar).xmp.Rating !== undefined) { - metadata.rating = (sidecarData as SideCar).xmp.Rating; + if ((sidecarData as SideCar).xmp !== undefined) { + if ((sidecarData as SideCar).xmp.Rating !== undefined) { + metadata.rating = (sidecarData as SideCar).xmp.Rating; + } } } } @@ -623,25 +632,29 @@ export class MetadataLoader { const sidecarData = await exifr.sidecar(sidecarPath); if (sidecarData !== undefined) { - if ((sidecarData as SideCar).dc.subject !== undefined) { - if (metadata.keywords === undefined) { - metadata.keywords = []; - } - let keywords = (sidecarData as SideCar).dc.subject || []; - if (typeof keywords === 'string') { - keywords = [keywords]; - } - for (const kw of keywords) { - if (metadata.keywords.indexOf(kw) === -1) { - metadata.keywords.push(kw); + if ((sidecarData as SideCar).dc !== undefined) { + if ((sidecarData as SideCar).dc.subject !== undefined) { + if (metadata.keywords === undefined) { + metadata.keywords = []; + } + let keywords = (sidecarData as SideCar).dc.subject || []; + if (typeof keywords === 'string') { + keywords = [keywords]; + } + for (const kw of keywords) { + if (metadata.keywords.indexOf(kw) === -1) { + metadata.keywords.push(kw); + } } } } - if ((sidecarData as SideCar).xmp.Rating !== undefined) { - metadata.rating = (sidecarData as SideCar).xmp.Rating; - } - if ((sidecarData as SideCar).xmp.CreateDate) { - metadata.creationDate = timestampToMS((sidecarData as SideCar).xmp.CreateDate, null); + if ((sidecarData as SideCar).xmp !== undefined) { + if ((sidecarData as SideCar).xmp.Rating !== undefined) { + metadata.rating = (sidecarData as SideCar).xmp.Rating; + } + if ((sidecarData as SideCar).xmp.CreateDate) { + metadata.creationDate = timestampToMS((sidecarData as SideCar).xmp.CreateDate, null); + } } } }