Add locale for expiry date
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
"showDescription": false,
|
||||
"showExpiryDate": false,
|
||||
"expiryDateFormat": "YYYY-MM-DD",
|
||||
"expiryDateLocale": "",
|
||||
"groupByDate": false,
|
||||
"cacheTime": 300
|
||||
},
|
||||
|
||||
+34
-6
@@ -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
|
||||
* feature is off, the share never expires, or the date can't be parsed.
|
||||
*
|
||||
* Gated by `ipp.gallery.showExpiryDate` (default `false`). The date is
|
||||
* formatted with `ipp.gallery.expiryDateFormat`, a dayjs format string that
|
||||
* defaults to the ISO 8601 date `YYYY-MM-DD` (e.g. `2026-07-10`).
|
||||
* Gated by `ipp.gallery.showExpiryDate` (default `false`). Formatted with the
|
||||
* dayjs format string `ipp.gallery.expiryDateFormat` (default ISO 8601 date
|
||||
* `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 {
|
||||
if (!getConfigOption('ipp.gallery.showExpiryDate', false)) return undefined
|
||||
if (!share.expiresAt) return undefined
|
||||
const parsed = dayjs(share.expiresAt)
|
||||
if (!parsed.isValid()) return undefined
|
||||
const configured = getConfigOption('ipp.gallery.expiryDateFormat', 'YYYY-MM-DD')
|
||||
const format = typeof configured === 'string' && configured ? configured : 'YYYY-MM-DD'
|
||||
return parsed.format(format)
|
||||
const configured = getConfigOption('ipp.gallery.expiryDateFormat', DEFAULT_EXPIRY_FORMAT)
|
||||
const format = typeof configured === 'string' && configured ? configured : DEFAULT_EXPIRY_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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ export function Gallery (props: GalleryProps) {
|
||||
? (ogItem.type === AssetType.video ? ogItem.thumbnailUrl : ogItem.previewUrl)
|
||||
: ''
|
||||
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 (
|
||||
<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`}/>
|
||||
</head>
|
||||
<body>
|
||||
{(props.showTitle || props.showDownload) && (
|
||||
{(showHeaderText || props.showDownload) && (
|
||||
<header id="header">
|
||||
{props.showTitle && (
|
||||
{showHeaderText && (
|
||||
<div class="header-text">
|
||||
<h1>{props.title || 'Gallery'}</h1>
|
||||
{props.showTitle && <h1>{props.title || 'Gallery'}</h1>}
|
||||
<p class="subtitle">
|
||||
{props.items.length}{' '}
|
||||
{props.items.length === 1 ? 'item' : 'items'}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import dayjs from 'dayjs'
|
||||
import 'dayjs/locale/de' // load German so the localised-format assertions are deterministic
|
||||
import { expiryDate } from '../src/share'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { KeyType, SharedLink } from '../src/types'
|
||||
@@ -58,4 +59,20 @@ describe('expiryDate', () => {
|
||||
setConfig({ ipp: { gallery: { showExpiryDate: true } } })
|
||||
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'))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user