Add locale for expiry date

This commit is contained in:
Alan Grainger
2026-07-17 14:58:33 +12:00
parent c1e2d3c0e7
commit ca5d23c6d7
5 changed files with 60 additions and 10 deletions
+1
View File
@@ -44,6 +44,7 @@
"showDescription": false, "showDescription": false,
"showExpiryDate": false, "showExpiryDate": false,
"expiryDateFormat": "YYYY-MM-DD", "expiryDateFormat": "YYYY-MM-DD",
"expiryDateLocale": "",
"groupByDate": false, "groupByDate": false,
"cacheTime": 300 "cacheTime": 300
}, },
+34 -6
View File
@@ -33,20 +33,48 @@ export function canDownload (share: SharedLink): boolean {
} }
} }
const DEFAULT_EXPIRY_FORMAT = 'YYYY-MM-DD'
/** /**
* Formatted expiry date for the gallery subtitle, or undefined when the * Formatted expiry date for the gallery subtitle, or undefined when the
* feature is off, the share never expires, or the date can't be parsed. * feature is off, the share never expires, or the date can't be parsed.
* *
* Gated by `ipp.gallery.showExpiryDate` (default `false`). The date is * Gated by `ipp.gallery.showExpiryDate` (default `false`). Formatted with the
* formatted with `ipp.gallery.expiryDateFormat`, a dayjs format string that * dayjs format string `ipp.gallery.expiryDateFormat` (default ISO 8601 date
* defaults to the ISO 8601 date `YYYY-MM-DD` (e.g. `2026-07-10`). * `YYYY-MM-DD`, e.g. `2026-07-10`). Name-based tokens (e.g. `MMMM` -> "July")
* render in the operator's `ipp.gallery.expiryDateLocale` when set, otherwise
* dayjs's default English.
*/ */
export function expiryDate (share: SharedLink): string | undefined { export function expiryDate (share: SharedLink): string | undefined {
if (!getConfigOption('ipp.gallery.showExpiryDate', false)) return undefined if (!getConfigOption('ipp.gallery.showExpiryDate', false)) return undefined
if (!share.expiresAt) return undefined if (!share.expiresAt) return undefined
const parsed = dayjs(share.expiresAt) const parsed = dayjs(share.expiresAt)
if (!parsed.isValid()) return undefined if (!parsed.isValid()) return undefined
const configured = getConfigOption('ipp.gallery.expiryDateFormat', 'YYYY-MM-DD') const configured = getConfigOption('ipp.gallery.expiryDateFormat', DEFAULT_EXPIRY_FORMAT)
const format = typeof configured === 'string' && configured ? configured : 'YYYY-MM-DD' const format = typeof configured === 'string' && configured ? configured : DEFAULT_EXPIRY_FORMAT
return parsed.format(format) const locale = expiryDateLocale()
return (locale ? parsed.locale(locale) : parsed).format(format)
}
/**
* Resolve and lazily load the dayjs locale named by `ipp.gallery.expiryDateLocale`
* so name-based expiry tokens localise. Returns the locale name to apply, or
* undefined to keep dayjs's default (English) - including when the value is not
* a valid, bundled dayjs locale. Node caches the require, so repeat lookups for
* the same locale are cheap.
*/
function expiryDateLocale (): string | undefined {
const configured = getConfigOption('ipp.gallery.expiryDateLocale', '')
if (typeof configured !== 'string' || !configured) return undefined
// dayjs locale files are lowercase (e.g. `en-gb.js`); normalise `en-GB` etc.
const name = configured.toLowerCase()
// Constrain to dayjs-shaped locale names (e.g. `de`, `en-gb`, `pt-br`); this
// also blocks the config value from reaching require() as a traversal path.
if (!/^[a-z]{2,3}(-[a-z]{2,4})?$/.test(name)) return undefined
try {
require('dayjs/locale/' + name)
return name
} catch {
return undefined
}
} }
+6 -3
View File
@@ -41,6 +41,9 @@ export function Gallery (props: GalleryProps) {
? (ogItem.type === AssetType.video ? ogItem.thumbnailUrl : ogItem.previewUrl) ? (ogItem.type === AssetType.video ? ogItem.thumbnailUrl : ogItem.previewUrl)
: '' : ''
const ogImageUrl = ogItem ? props.publicBaseUrl + ogImageAsset : '' const ogImageUrl = ogItem ? props.publicBaseUrl + ogImageAsset : ''
// The title block also carries the subtitle (item count + optional expiry),
// so it renders when a title is wanted OR there's an expiry date to show.
const showHeaderText = props.showTitle || !!props.expiryDate
return ( return (
<html lang="en"> <html lang="en">
@@ -66,11 +69,11 @@ export function Gallery (props: GalleryProps) {
<link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/photoswipe-overrides.css`}/> <link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/photoswipe-overrides.css`}/>
</head> </head>
<body> <body>
{(props.showTitle || props.showDownload) && ( {(showHeaderText || props.showDownload) && (
<header id="header"> <header id="header">
{props.showTitle && ( {showHeaderText && (
<div class="header-text"> <div class="header-text">
<h1>{props.title || 'Gallery'}</h1> {props.showTitle && <h1>{props.title || 'Gallery'}</h1>}
<p class="subtitle"> <p class="subtitle">
{props.items.length}{' '} {props.items.length}{' '}
{props.items.length === 1 ? 'item' : 'items'} {props.items.length === 1 ? 'item' : 'items'}
+17
View File
@@ -1,5 +1,6 @@
import { describe, it, expect, beforeEach } from 'vitest' import { describe, it, expect, beforeEach } from 'vitest'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import 'dayjs/locale/de' // load German so the localised-format assertions are deterministic
import { expiryDate } from '../src/share' import { expiryDate } from '../src/share'
import { loadConfig } from '../src/config/loader' import { loadConfig } from '../src/config/loader'
import { KeyType, SharedLink } from '../src/types' import { KeyType, SharedLink } from '../src/types'
@@ -58,4 +59,20 @@ describe('expiryDate', () => {
setConfig({ ipp: { gallery: { showExpiryDate: true } } }) setConfig({ ipp: { gallery: { showExpiryDate: true } } })
expect(expiryDate(share('not-a-date'))).toBeUndefined() expect(expiryDate(share('not-a-date'))).toBeUndefined()
}) })
it('localises name tokens with a configured expiryDateLocale', () => {
setConfig({ ipp: { gallery: { showExpiryDate: true, expiryDateFormat: 'D MMMM YYYY', expiryDateLocale: 'de' } } })
// "10 Juli 2026" in German, not "10 July 2026"
expect(expiryDate(share(EXPIRES))).toBe(dayjs(EXPIRES).locale('de').format('D MMMM YYYY'))
})
it('accepts a locale case-insensitively', () => {
setConfig({ ipp: { gallery: { showExpiryDate: true, expiryDateFormat: 'D MMMM YYYY', expiryDateLocale: 'DE' } } })
expect(expiryDate(share(EXPIRES))).toBe(dayjs(EXPIRES).locale('de').format('D MMMM YYYY'))
})
it('falls back to English for an unknown or malformed locale', () => {
setConfig({ ipp: { gallery: { showExpiryDate: true, expiryDateFormat: 'D MMMM YYYY', expiryDateLocale: '../en' } } })
expect(expiryDate(share(EXPIRES))).toBe(dayjs(EXPIRES).locale('en').format('D MMMM YYYY'))
})
}) })
+2 -1
View File
@@ -85,7 +85,8 @@ Options that control how the gallery page is rendered. Configured under `ipp.gal
| `showTitle` | `bool` | Show a title on the gallery page. This is taken from the album title if it is an album being shared, otherwise the "Description" from the shared link will be used. | | `showTitle` | `bool` | Show a title on the gallery page. This is taken from the album title if it is an album being shared, otherwise the "Description" from the shared link will be used. |
| `showDescription` | `bool` | Show the album description below the title. This only applies if it is an album which is being shared. | | `showDescription` | `bool` | Show the album description below the title. This only applies if it is an album which is being shared. |
| `showExpiryDate` | `bool` | Show the share's expiry date next to the item count in the subtitle, as `45 items · available until 2026-07-10`. Only appears when the share actually has an expiry set in Immich (shares set to "never" show nothing extra). Default `false`. | | `showExpiryDate` | `bool` | Show the share's expiry date next to the item count in the subtitle, as `45 items · available until 2026-07-10`. Only appears when the share actually has an expiry set in Immich (shares set to "never" show nothing extra). Default `false`. |
| `expiryDateFormat` | `string` | Format for the `showExpiryDate` date, as a [dayjs format string](https://day.js.org/docs/en/display/format). Default `"YYYY-MM-DD"` (ISO 8601 date, e.g. `2026-07-10`). Example: `"D MMMM YYYY"` renders `10 July 2026`. | | `expiryDateFormat` | `string` | Format for the `showExpiryDate` date, as a [dayjs format string](https://day.js.org/docs/en/display/format). Default `"YYYY-MM-DD"` (ISO 8601 date, e.g. `2026-07-10`). Example: `"D MMMM YYYY"` renders `10 July 2026`. Name-based tokens (`MMMM`, `dddd`, ...) render in English unless you also set `expiryDateLocale`. |
| `expiryDateLocale` | `string` | Locale for `showExpiryDate` when the format uses name tokens (e.g. `MMMM` for month names), as a [dayjs locale code](https://github.com/iamkun/dayjs/tree/dev/src/locale) such as `"de"`, `"fr"` or `"en-gb"`. Default `""` (dayjs's default English). The numeric default format needs no locale. Applies only to this server-rendered date; thumbnail date-group headers (`groupByDate`) already follow each viewer's own browser locale. |
| `groupByDate` | `bool` or `string` | Group the gallery's thumbnails by date, with a header above each group. Set to `false` (default, no grouping), `true` / `"month"` for month headers like "December 2024", or `"day"` for day headers like "25 December 2024". Grouping uses each photo's local "taken" date (matching Immich's own timeline), and sorts photos newest-first. Items missing a creation date end up under an "Undated" bucket at the end (or render without any header when the whole gallery is undated). **Requires "Show metadata" to be enabled on the share in Immich** - when that's off, Immich strips creation dates from the assets it returns to IPP, so grouping won't work. | | `groupByDate` | `bool` or `string` | Group the gallery's thumbnails by date, with a header above each group. Set to `false` (default, no grouping), `true` / `"month"` for month headers like "December 2024", or `"day"` for day headers like "25 December 2024". Grouping uses each photo's local "taken" date (matching Immich's own timeline), and sorts photos newest-first. Items missing a creation date end up under an "Undated" bucket at the end (or render without any header when the whole gallery is undated). **Requires "Show metadata" to be enabled on the share in Immich** - when that's off, Immich strips creation dates from the assets it returns to IPP, so grouping won't work. |
| `cacheTime` | `int` | How long (in seconds) browsers and any CDN may cache the gallery **page** before revalidating. Set this longer to reduce load on your server, but be aware that if you add new photos to a gallery they won't show up until the cache times out or you manually clear any downstream caches. | | `cacheTime` | `int` | How long (in seconds) browsers and any CDN may cache the gallery **page** before revalidating. Set this longer to reduce load on your server, but be aware that if you add new photos to a gallery they won't show up until the cache times out or you manually clear any downstream caches. |