From ea04c6dc7a71b2e608b4695de266c1fb0c00d87d Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Mon, 25 Nov 2019 22:31:16 +0100 Subject: [PATCH] Filter out non-image OBJE entries when displaying personal photos --- src/gedcom_util.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/gedcom_util.ts b/src/gedcom_util.ts index 7b9f54c..893c79c 100644 --- a/src/gedcom_util.ts +++ b/src/gedcom_util.ts @@ -113,20 +113,32 @@ function sortChildren(gedcom: JsonGedcomData): JsonGedcomData { return Object.assign({}, gedcom, {fams: newFams}); } +const IMAGE_EXTENSIONS = ['.jpg', '.png', '.gif']; + +/** Returns true if the given file name has a known image extension. */ +function isImageFile(fileName: string): boolean { + const lowerName = fileName.toLowerCase(); + return IMAGE_EXTENSIONS.some((ext) => lowerName.endsWith(ext)); +} + /** - * Removes images that are not HTTP links. - * Does not modify the input object. + * Removes imageUrl fields that are not HTTP links or do not have known image + * extensions. Does not modify the input object. */ function filterImage(indi: JsonIndi, images: Map): JsonIndi { if (indi.imageUrl) { const fileName = indi.imageUrl.match(/[^/\\]*$/)![0]; + // If the image file has been loaded into memory, use it. if (images.has(fileName)) { const newIndi = Object.assign({}, indi); newIndi.imageUrl = images.get(fileName); return newIndi; } } - if (!indi.imageUrl || indi.imageUrl.startsWith('http')) { + if ( + !indi.imageUrl || + (indi.imageUrl.startsWith('http') && isImageFile(indi.imageUrl)) + ) { return indi; } const newIndi = Object.assign({}, indi);