#244 Reverse metadata field defaults

This commit is contained in:
Alan Grainger
2026-06-05 10:50:16 +12:00
parent 2390d0eb74
commit 7799fa71c9
11 changed files with 246 additions and 107 deletions
+113 -47
View File
@@ -1,16 +1,97 @@
import { Asset } from '../types'
import { Asset, ExifInfo } from '../types'
import { GalleryExif } from '../shared/types'
import { getConfigOption } from '../config/access'
type Group = 'exif' | 'location'
interface FieldRule {
// Config flag name, relative to the group (e.g. 'gps' -> ipp.showMetadata.location.gps)
flag: string
// Copies values from the Immich-side ExifInfo (+ asset for fileName) onto
// the gallery-side GalleryExif if they're present and non-empty.
emit: (out: GalleryExif, info: ExifInfo, asset: Asset) => void
}
/**
* Trivial copy: `out[key] = info[key]` when present. Used for fields where
* the config flag, source key, and output key all match.
*/
function copy (key: keyof ExifInfo & keyof GalleryExif): FieldRule {
return {
flag: key,
emit: (out, info) => {
const value = info[key]
if (value != null && value !== '') (out as Record<string, unknown>)[key] = value
}
}
}
/*
Fields whose config flag, ExifInfo key, and GalleryExif key are all the
same string. `copy` handles the whole if/copy dance for each. Fields
that need special handling (fileName, dimensions, fileSize, gps) appear
inline in the `*_RULES` arrays below.
*/
const EXIF_FIELDS = [
'dateTimeOriginal', 'make', 'model', 'lensModel',
'exposureTime', 'iso', 'fNumber', 'focalLength'
] as const
const LOCATION_FIELDS = ['city', 'state', 'country'] as const
const EXIF_RULES: FieldRule[] = [
...EXIF_FIELDS.map(copy),
{
flag: 'fileName',
emit: (out, _info, asset) => {
if (asset.originalFileName) out.fileName = asset.originalFileName
}
},
{
flag: 'dimensions',
emit: (out, info) => {
if (info.exifImageWidth && info.exifImageHeight) {
out.width = info.exifImageWidth
out.height = info.exifImageHeight
}
}
},
{
flag: 'fileSize',
emit: (out, info) => {
if (info.fileSizeInByte) out.fileSizeInByte = info.fileSizeInByte
}
}
]
const LOCATION_RULES: FieldRule[] = [
...LOCATION_FIELDS.map(copy),
{
flag: 'gps',
emit: (out, info) => {
if (info.latitude != null && info.longitude != null) {
out.latitude = info.latitude
out.longitude = info.longitude
}
}
}
]
const RULES: Record<Group, FieldRule[]> = {
exif: EXIF_RULES,
location: LOCATION_RULES
}
/**
* Build the per-asset metadata sub-object included in the gallery JSON.
* Reads `ipp.showMetadata.exif.*` and `ipp.showMetadata.location.*` config:
* each group has a master `enabled` toggle, and within each group the
* per-field flags default to `true`. A field appears in the output only
* when both the group is enabled AND the field's own flag is true.
* each group has a master `enableAll` toggle and a set of per-field flags
* that all default to `false`. A field appears in the output when either
* the group's `enableAll` is true, OR the field's own flag is true.
*
* Returns `undefined` when neither group is enabled, or when no fields
* survive gating (so the client knows there's no metadata to show).
* Returns `undefined` when neither group has any visible content, or when
* no fields survive gating (so the client knows there's no metadata to
* show).
*
* The server never sends Immich values that the operator hasn't opted in
* to via config; the client just renders what's present.
@@ -19,52 +100,37 @@ export function pickExif (asset: Asset): GalleryExif | undefined {
const exifInfo = asset?.exifInfo
if (!exifInfo) return undefined
const exifEnabled = !!getConfigOption('ipp.showMetadata.exif.enabled', false)
const locationEnabled = !!getConfigOption('ipp.showMetadata.location.enabled', false)
if (!exifEnabled && !locationEnabled) return undefined
const exifActive = metadataGroupActive('exif')
const locationActive = metadataGroupActive('location')
if (!exifActive && !locationActive) return undefined
const out: GalleryExif = {}
if (exifEnabled) {
if (allow('exif.dateTimeOriginal') && exifInfo.dateTimeOriginal) {
out.dateTimeOriginal = exifInfo.dateTimeOriginal
}
if (allow('exif.fileName') && asset.originalFileName) {
out.fileName = asset.originalFileName
}
if (allow('exif.dimensions') && exifInfo.exifImageWidth && exifInfo.exifImageHeight) {
out.width = exifInfo.exifImageWidth
out.height = exifInfo.exifImageHeight
}
if (allow('exif.fileSize') && exifInfo.fileSizeInByte) {
out.fileSizeInByte = exifInfo.fileSizeInByte
}
if (allow('exif.make') && exifInfo.make) out.make = exifInfo.make
if (allow('exif.model') && exifInfo.model) out.model = exifInfo.model
if (allow('exif.lensModel') && exifInfo.lensModel) out.lensModel = exifInfo.lensModel
if (allow('exif.exposureTime') && exifInfo.exposureTime) out.exposureTime = exifInfo.exposureTime
if (allow('exif.iso') && exifInfo.iso) out.iso = exifInfo.iso
if (allow('exif.fNumber') && exifInfo.fNumber) out.fNumber = exifInfo.fNumber
if (allow('exif.focalLength') && exifInfo.focalLength) out.focalLength = exifInfo.focalLength
}
if (locationEnabled) {
if (allow('location.city') && exifInfo.city) out.city = exifInfo.city
if (allow('location.state') && exifInfo.state) out.state = exifInfo.state
if (allow('location.country') && exifInfo.country) out.country = exifInfo.country
if (allow('location.gps') && exifInfo.latitude != null && exifInfo.longitude != null) {
out.latitude = exifInfo.latitude
out.longitude = exifInfo.longitude
}
}
if (exifActive) applyRules('exif', out, exifInfo, asset)
if (locationActive) applyRules('location', out, exifInfo, asset)
return Object.keys(out).length ? out : undefined
}
/**
* Per-field gate. Defaults to `true` so that turning on a group exposes
* every field unless the operator explicitly opts out of one.
* Returns true if a metadata group would render any field for the current
* config - either `enableAll` is set, or at least one per-field flag is
* explicitly `true`. Used by the sidebar visibility check.
*/
function allow (field: string): boolean {
return !!getConfigOption(`ipp.showMetadata.${field}`, true)
export function metadataGroupActive (group: Group): boolean {
if (groupEnableAll(group)) return true
return RULES[group].some(rule => fieldFlag(group, rule.flag))
}
function applyRules (group: Group, out: GalleryExif, info: ExifInfo, asset: Asset): void {
const enableAll = groupEnableAll(group)
for (const rule of RULES[group]) {
if (enableAll || fieldFlag(group, rule.flag)) rule.emit(out, info, asset)
}
}
function groupEnableAll (group: Group): boolean {
return !!getConfigOption(`ipp.showMetadata.${group}.enableAll`, false)
}
function fieldFlag (group: Group, flag: string): boolean {
return !!getConfigOption(`ipp.showMetadata.${group}.${flag}`, false)
}