Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(exif): enhance EXIF data extraction with additional *description* fields and normalization functions

Timothy Yin77610a3b060e
diff --git a/app/components/photo/InfoPanel.vue b/app/components/photo/InfoPanel.vue
index e1d058be..ef1ed563 100644
--- a/app/components/photo/InfoPanel.vue
+++ b/app/components/photo/InfoPanel.vue
@@ -473,7 +473,7 @@ const onTagClick = (tag: string) => {
<!-- 照片描述 -->
<div
v-if="currentPhoto.description"
- class="text-sm text-white"
+ class="text-sm text-white text-justify"
>
{{ currentPhoto.description }}
</div>
diff --git a/server/services/image/exif.ts b/server/services/image/exif.ts
index 57292f64..286cc970 100644
--- a/server/services/image/exif.ts
+++ b/server/services/image/exif.ts
@@ -10,8 +10,16 @@ import { withRetry, RetryPresets, RetryConditions } from '../../utils/retry'
const neededKeys: Array<keyof Tags | (string & {})> = [
'Title',
+ 'XPTitle',
'Subject',
'Keywords',
+ 'XPKeywords',
+
+ 'Description',
+ 'ImageDescription',
+ 'Caption-Abstract',
+ 'XPComment',
+ 'UserComment',
'tz',
'tzSource',
@@ -350,30 +358,94 @@ export const extractExifData = async (
}
}
+const normalizeText = (value: unknown): string | undefined => {
+ if (value === null || value === undefined) {
+ return undefined
+ }
+
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ const normalized = normalizeText(item)
+ if (normalized) {
+ return normalized
+ }
+ }
+ return undefined
+ }
+
+ const text = String(value).trim()
+ return text.length > 0 ? text : undefined
+}
+
+const collectTextValues = (value: unknown, options?: { splitDelimited?: boolean }): string[] => {
+ const splitDelimited = options?.splitDelimited ?? false
+ const results: string[] = []
+
+ const append = (input: string) => {
+ if (splitDelimited) {
+ for (const part of input.split(/[;,]/)) {
+ const chunk = part.trim()
+ if (chunk.length > 0) {
+ results.push(chunk)
+ }
+ }
+ } else if (input.length > 0) {
+ results.push(input)
+ }
+ }
+
+ if (value === null || value === undefined) {
+ return results
+ }
+
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ const normalized = normalizeText(item)
+ if (normalized) {
+ append(normalized)
+ }
+ }
+ return results
+ }
+
+ const normalized = normalizeText(value)
+ if (normalized) {
+ append(normalized)
+ }
+
+ return results
+}
+
+const pickFirstText = (...values: Array<unknown>): string | undefined => {
+ for (const value of values) {
+ const normalized = normalizeText(value)
+ if (normalized) {
+ return normalized
+ }
+ }
+
+ return undefined
+}
+
export const extractPhotoInfo = (
s3key: string,
exifData?: NeededExif | null,
): PhotoInfo => {
const fileName = path.basename(s3key, path.extname(s3key))
- let title = fileName
let dateTaken = new Date().toISOString()
- let views = 0
- let tags: string[] = []
-
- if (exifData?.Subject || exifData?.Keywords) {
- tags = [
- ...new Set([
- ...(typeof exifData.Subject === 'string'
- ? [exifData.Subject]
- : exifData.Subject || []),
- ...(typeof exifData.Keywords === 'string'
- ? [exifData.Keywords]
- : exifData.Keywords || []),
- ]),
- ]
- .map((tag) => tag.trim())
- .filter((tag) => tag.length > 0)
+ const tagsSet = new Set<string>()
+
+ for (const tag of collectTextValues(exifData?.Subject)) {
+ tagsSet.add(tag)
+ }
+
+ for (const tag of collectTextValues(exifData?.Keywords)) {
+ tagsSet.add(tag)
+ }
+
+ for (const tag of collectTextValues(exifData?.XPKeywords, { splitDelimited: true })) {
+ tagsSet.add(tag)
}
if (exifData?.DateTimeOriginal) {
@@ -396,28 +468,38 @@ export const extractPhotoInfo = (
}
}
- const viewsMatch = fileName.match(/(\d+)views?/i)
- if (viewsMatch) {
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- views = Number.parseInt(viewsMatch[1])
- }
-
- title =
- exifData?.Title ||
- fileName
- .replaceAll(/\d{4}-\d{2}-\d{2}[_-]?/g, '')
- .replaceAll(/[_-]?\d+views?/gi, '')
- .replaceAll(/[_-]+/g, ' ')
- .trim()
+ const titleFromExif = pickFirstText(
+ exifData?.Title,
+ exifData?.XPTitle,
+ exifData?.Description,
+ exifData?.ImageDescription,
+ exifData?.CaptionAbstract,
+ )
+
+ let title = titleFromExif
+ const cleanedFileName = fileName
+ .replaceAll(/\d{4}-\d{2}-\d{2}[_-]?/g, '')
+ .replaceAll(/[_-]?\d+views?/gi, '')
+ .replaceAll(/[_-]+/g, ' ')
+ .trim()
if (!title) {
- title = path.basename(s3key, path.extname(s3key))
+ title = cleanedFileName || path.basename(s3key, path.extname(s3key))
}
+ const description =
+ pickFirstText(
+ exifData?.Description,
+ exifData?.ImageDescription,
+ exifData?.CaptionAbstract,
+ exifData?.XPComment,
+ exifData?.UserComment,
+ ) || ''
+
return {
title,
dateTaken,
- tags,
- description: '',
+ tags: [...tagsSet],
+ description,
}
}
diff --git a/shared/types/photo.ts b/shared/types/photo.ts
index 200c21d9..fad8827b 100644
--- a/shared/types/photo.ts
+++ b/shared/types/photo.ts
@@ -2,8 +2,16 @@ import type { Tags } from 'exiftool-vendored'
export interface NeededExif {
Title?: string
+ XPTitle?: string
Subject?: string[]
Keywords?: string[]
+ XPKeywords?: string
+
+ Description?: Tags['Description']
+ ImageDescription?: Tags['ImageDescription']
+ CaptionAbstract?: Tags['Caption-Abstract']
+ XPComment?: Tags['XPComment']
+ UserComment?: Tags['UserComment']
zone?: string
tz?: string