#307 Add ipp.shareCacheTime and cachebusted link after an upload
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
"showHomePage": true,
|
"showHomePage": true,
|
||||||
"allowSlugLinks": true,
|
"allowSlugLinks": true,
|
||||||
"uploadUrl": "/upload",
|
"uploadUrl": "/upload",
|
||||||
|
"shareCacheTime": 120,
|
||||||
"showMetadata": {
|
"showMetadata": {
|
||||||
"description": {
|
"description": {
|
||||||
"caption": false,
|
"caption": false,
|
||||||
|
|||||||
+9
-1
@@ -12,6 +12,7 @@ import {
|
|||||||
cachedPromise,
|
cachedPromise,
|
||||||
fetchSharedLink,
|
fetchSharedLink,
|
||||||
getConfigOption,
|
getConfigOption,
|
||||||
|
getNumericConfigOption,
|
||||||
invalidPasswordResponse,
|
invalidPasswordResponse,
|
||||||
isKey,
|
isKey,
|
||||||
log,
|
log,
|
||||||
@@ -46,8 +47,15 @@ import { h } from 'preact'
|
|||||||
read-only. In-place mutation persists for the cache lifetime and leaks
|
read-only. In-place mutation persists for the cache lifetime and leaks
|
||||||
across concurrent requests. The assets are sorted into display order here,
|
across concurrent requests. The assets are sorted into display order here,
|
||||||
before caching, so no caller needs to sort them; clone before mutating.
|
before caching, so no caller needs to sort them; clone before mutating.
|
||||||
|
|
||||||
|
The TTL is `ipp.shareCacheTime`, so it bounds how long a visitor's upload
|
||||||
|
takes to reach the gallery. It is read on each `set` because this cache is
|
||||||
|
built at import time, before the config is loaded.
|
||||||
*/
|
*/
|
||||||
const shareCache = new TtlLruCache<Promise<SharedLinkResult>>({ ttlMs: 120_000, max: 100 })
|
const shareCache = new TtlLruCache<Promise<SharedLinkResult>>({
|
||||||
|
ttlMs: () => Math.max(1, getNumericConfigOption('ipp.shareCacheTime', 120)) * 1000,
|
||||||
|
max: 100
|
||||||
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Per-asset detail cache for the lazy album flow. When a `needsDetail` album
|
Per-asset detail cache for the lazy album flow. When a `needsDetail` album
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||||
|
import { KeyType } from '@ipp/core'
|
||||||
|
import { getShareByKey } from '../src/immich'
|
||||||
|
|
||||||
|
/*
|
||||||
|
`ipp.shareCacheTime` sets how long getShareByKey serves a share from memory
|
||||||
|
before it asks Immich again. Config is mocked per test rather than loaded
|
||||||
|
from a file.
|
||||||
|
*/
|
||||||
|
const cfg: Record<string, unknown> = {}
|
||||||
|
vi.mock('@ipp/core', async (importOriginal) => ({
|
||||||
|
...await importOriginal<typeof import('@ipp/core')>(),
|
||||||
|
getNumericConfigOption: (path: string, fallback: number) => (path in cfg ? Number(cfg[path]) : fallback)
|
||||||
|
}))
|
||||||
|
|
||||||
|
const individualShare = {
|
||||||
|
type: 'INDIVIDUAL',
|
||||||
|
key: 'real-key',
|
||||||
|
allowDownload: true,
|
||||||
|
expiresAt: null,
|
||||||
|
showMetadata: true,
|
||||||
|
assets: [{ id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', type: 'IMAGE', isTrashed: false, thumbhash: 'hashA' }]
|
||||||
|
}
|
||||||
|
|
||||||
|
function stubImmich () {
|
||||||
|
const fetchMock = vi.fn(async () => ({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
headers: { get: (h: string) => (h.toLowerCase() === 'content-type' ? 'application/json' : null) },
|
||||||
|
json: async () => individualShare
|
||||||
|
}))
|
||||||
|
vi.stubGlobal('fetch', fetchMock)
|
||||||
|
return fetchMock
|
||||||
|
}
|
||||||
|
|
||||||
|
let keyCounter = 0
|
||||||
|
/** A fresh key per test, because getShareByKey caches by key. */
|
||||||
|
function uniqueKey () {
|
||||||
|
return 'cache-key-' + (keyCounter++)
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('ipp.shareCacheTime', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
process.env.IMMICH_URL = 'http://immich.test'
|
||||||
|
vi.useFakeTimers()
|
||||||
|
for (const key of Object.keys(cfg)) delete cfg[key]
|
||||||
|
})
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers()
|
||||||
|
vi.unstubAllGlobals()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps a share for 120 seconds by default', async () => {
|
||||||
|
const fetchMock = stubImmich()
|
||||||
|
const key = uniqueKey()
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
vi.advanceTimersByTime(119_999)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||||
|
vi.advanceTimersByTime(1)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('uses the configured time', async () => {
|
||||||
|
cfg['ipp.shareCacheTime'] = 15
|
||||||
|
const fetchMock = stubImmich()
|
||||||
|
const key = uniqueKey()
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
vi.advanceTimersByTime(14_999)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||||
|
vi.advanceTimersByTime(1)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('treats a value under 1 as 1 second', async () => {
|
||||||
|
cfg['ipp.shareCacheTime'] = 0
|
||||||
|
const fetchMock = stubImmich()
|
||||||
|
const key = uniqueKey()
|
||||||
|
// Concurrent misses still share one Immich call
|
||||||
|
await Promise.all([getShareByKey(key, undefined, KeyType.key), getShareByKey(key, undefined, KeyType.key)])
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||||
|
vi.advanceTimersByTime(999)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||||
|
vi.advanceTimersByTime(1)
|
||||||
|
await getShareByKey(key, undefined, KeyType.key)
|
||||||
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -51,6 +51,7 @@ export const LABELS: Record<string, string> = {
|
|||||||
'ipp.gallery.singleImage': 'Gallery page for a single image',
|
'ipp.gallery.singleImage': 'Gallery page for a single image',
|
||||||
'ipp.gallery.singleVideo': 'Gallery page for a single video',
|
'ipp.gallery.singleVideo': 'Gallery page for a single video',
|
||||||
'ipp.gallery.singleItemAutoOpen': 'Open single items in the lightbox',
|
'ipp.gallery.singleItemAutoOpen': 'Open single items in the lightbox',
|
||||||
|
'ipp.shareCacheTime': 'Share cache time (seconds)',
|
||||||
'ipp.gallery.cacheTime': 'Cache time (seconds)',
|
'ipp.gallery.cacheTime': 'Cache time (seconds)',
|
||||||
'ipp.lightbox.autoPlayVideos': 'Autoplay videos',
|
'ipp.lightbox.autoPlayVideos': 'Autoplay videos',
|
||||||
'ipp.showMetadata.exif.dateTimeOriginal': 'Date taken',
|
'ipp.showMetadata.exif.dateTimeOriginal': 'Date taken',
|
||||||
|
|||||||
@@ -146,6 +146,16 @@ Where the optional upload service (the `immich-public-proxy-upload` container) i
|
|||||||
- **An absolute URL**, such as `https://upload.example.com`, means the service has a hostname of its own. It is trusted without a check. The URL may include a path, such as `https://photos.example.com/upload`, which points at the same place as the default but without the check.
|
- **An absolute URL**, such as `https://upload.example.com`, means the service has a hostname of its own. It is trusted without a check. The URL may include a path, such as `https://photos.example.com/upload`, which points at the same place as the default but without the check.
|
||||||
- **An empty string** never shows the button.
|
- **An empty string** never shows the button.
|
||||||
|
|
||||||
|
## `shareCacheTime`
|
||||||
|
|
||||||
|
**Type:** `int` · **Default:** `120`
|
||||||
|
|
||||||
|
How long, in seconds, IPP keeps a share's list of photos in memory before it asks Immich again. Photos added to a share, including [visitor uploads](/visitor-uploads#when-do-uploads-appear), don't show in the gallery until this runs out. The minimum is `1`.
|
||||||
|
|
||||||
|
A shorter time costs more calls to Immich. Each refresh of an album takes one call, plus one for each month that has photos in it, so an album that spans two years takes about 25 calls. While someone is viewing that album, IPP refreshes it once per `shareCacheTime`: about 12 calls a minute at the default, and about 100 a minute at `15`. A share that nobody is viewing costs nothing.
|
||||||
|
|
||||||
|
Other visitors also wait for [`gallery.cacheTime`](/config/gallery#cachetime), a separate cache in their browser. The visitor who sent the photo skips that one: the upload page's "Back to gallery" link fetches a fresh copy of the gallery page.
|
||||||
|
|
||||||
## `gallery`
|
## `gallery`
|
||||||
|
|
||||||
**Type:** `object`
|
**Type:** `object`
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ have no album.
|
|||||||
Two things to know:
|
Two things to know:
|
||||||
|
|
||||||
- The upload is pulled back, not held back. The workflow runs a few seconds after the file is stored, and IPP
|
- The upload is pulled back, not held back. The workflow runs a few seconds after the file is stored, and IPP
|
||||||
caches a share's photo list for up to 2 minutes, so a visitor who has the gallery open can see the photo for a
|
caches a share's photo list for [`shareCacheTime`](/config/ipp-options#sharecachetime), 2 minutes by default, so a visitor who has the gallery open can see the photo for a
|
||||||
short while. Anyone who copied the photo's address in that window can keep opening it until you delete it.
|
short while. Anyone who copied the photo's address in that window can keep opening it until you delete it.
|
||||||
- Keeping a photo relies on Immich leaving it in the album while it is locked, which it does for photos a workflow
|
- Keeping a photo relies on Immich leaving it in the album while it is locked, which it does for photos a workflow
|
||||||
moves. If a future Immich version removes it from the album instead, add the approved photos back to the album
|
moves. If a future Immich version removes it from the album instead, add the approved photos back to the album
|
||||||
|
|||||||
@@ -315,11 +315,14 @@ The owner sees them in Immich at once. Other visitors see them in the gallery on
|
|||||||
|
|
||||||
1. Immich has made the thumbnail. That takes seconds for a photo and longer for a video, and IPP hides a photo until
|
1. Immich has made the thumbnail. That takes seconds for a photo and longer for a video, and IPP hides a photo until
|
||||||
then.
|
then.
|
||||||
2. IPP's cache of the share has expired. It is kept for up to 2 minutes.
|
2. IPP's cache of the share has expired. It is kept for [`shareCacheTime`](/config/ipp-options#sharecachetime),
|
||||||
|
2 minutes by default. You can lower it, at the cost of more calls to Immich.
|
||||||
3. The visitor's browser has fetched the gallery page again. It keeps the page for
|
3. The visitor's browser has fetched the gallery page again. It keeps the page for
|
||||||
[`gallery.cacheTime`](/config/gallery#cachetime), 5 minutes by default.
|
[`gallery.cacheTime`](/config/gallery#cachetime), 5 minutes by default.
|
||||||
|
|
||||||
So a new photo can take up to about 7 minutes to show for a visitor who already had the gallery open. The upload page
|
So a new photo can take up to about 7 minutes to show for a visitor who already had the gallery open. The visitor
|
||||||
|
who sent it skips step 3: after an upload, the upload page's "Back to gallery" link fetches a fresh copy of the
|
||||||
|
gallery page. The upload page
|
||||||
includes a short notice to let visitors know about the delay; you can change the wording or hide it with the
|
includes a short notice to let visitors know about the delay; you can change the wording or hide it with the
|
||||||
[`notice`](/config/upload-service#notice) option.
|
[`notice`](/config/upload-service#notice) option.
|
||||||
|
|
||||||
|
|||||||
@@ -22,10 +22,14 @@ interface Entry<V> {
|
|||||||
|
|
||||||
export class TtlLruCache<V> {
|
export class TtlLruCache<V> {
|
||||||
private readonly entries = new Map<string, Entry<V>>()
|
private readonly entries = new Map<string, Entry<V>>()
|
||||||
private readonly ttlMs: number
|
private readonly ttlMs: number | (() => number)
|
||||||
private readonly max: number
|
private readonly max: number
|
||||||
|
|
||||||
constructor (opts: { ttlMs: number, max: number }) {
|
/**
|
||||||
|
* `ttlMs` may be a function, read on each `set`, for a TTL that comes from
|
||||||
|
* config: a cache built at import time exists before the config is loaded.
|
||||||
|
*/
|
||||||
|
constructor (opts: { ttlMs: number | (() => number), max: number }) {
|
||||||
this.ttlMs = opts.ttlMs
|
this.ttlMs = opts.ttlMs
|
||||||
this.max = opts.max
|
this.max = opts.max
|
||||||
}
|
}
|
||||||
@@ -55,7 +59,8 @@ export class TtlLruCache<V> {
|
|||||||
* just in case).
|
* just in case).
|
||||||
*/
|
*/
|
||||||
set (key: string, value: V): void {
|
set (key: string, value: V): void {
|
||||||
this.entries.set(key, { value, expiresAt: Date.now() + this.ttlMs })
|
const ttlMs = typeof this.ttlMs === 'function' ? this.ttlMs() : this.ttlMs
|
||||||
|
this.entries.set(key, { value, expiresAt: Date.now() + ttlMs })
|
||||||
if (this.entries.size > this.max) {
|
if (this.entries.size > this.max) {
|
||||||
const oldest = this.entries.keys().next().value
|
const oldest = this.entries.keys().next().value
|
||||||
if (oldest !== undefined && oldest !== key) this.entries.delete(oldest)
|
if (oldest !== undefined && oldest !== key) this.entries.delete(oldest)
|
||||||
|
|||||||
@@ -108,3 +108,16 @@ describe('TtlLruCache re-set', () => {
|
|||||||
expect(cache.get('a')).toBe('2')
|
expect(cache.get('a')).toBe('2')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('TtlLruCache TTL getter', () => {
|
||||||
|
it('reads the TTL on each set, so a later change applies to the next entry', () => {
|
||||||
|
let ttlMs = 1000
|
||||||
|
const cache = new TtlLruCache<string>({ ttlMs: () => ttlMs, max: 10 })
|
||||||
|
cache.set('a', '1')
|
||||||
|
ttlMs = 100
|
||||||
|
cache.set('b', '2')
|
||||||
|
vi.advanceTimersByTime(100)
|
||||||
|
expect(cache.get('a')).toBe('1')
|
||||||
|
expect(cache.get('b')).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -115,6 +115,20 @@ function showError (row: UploadRow, message: string) {
|
|||||||
row.li.appendChild(p)
|
row.li.appendChild(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After a file reaches the share, point "Back to gallery" at a fresh URL. The
|
||||||
|
* browser, and any CDN, cache the gallery page per URL, so the visitor then
|
||||||
|
* gets IPP's current copy of the share rather than the one from before. A new
|
||||||
|
* value each time, because the visitor may already have opened the last one.
|
||||||
|
*/
|
||||||
|
function freshenBackLink () {
|
||||||
|
const back = document.getElementById('upload-back') as HTMLAnchorElement | null
|
||||||
|
if (!back) return
|
||||||
|
const url = new URL(back.href)
|
||||||
|
url.searchParams.set('r', Math.random().toString(36).slice(2, 10))
|
||||||
|
back.href = url.toString()
|
||||||
|
}
|
||||||
|
|
||||||
/** Record a finished row: its state, its counter, and the button it keeps. */
|
/** Record a finished row: its state, its counter, and the button it keeps. */
|
||||||
function finish (row: UploadRow, state: 'done' | 'duplicate' | 'error', message?: string, retryable = false) {
|
function finish (row: UploadRow, state: 'done' | 'duplicate' | 'error', message?: string, retryable = false) {
|
||||||
setState(row, state)
|
setState(row, state)
|
||||||
@@ -122,10 +136,11 @@ function finish (row: UploadRow, state: 'done' | 'duplicate' | 'error', message?
|
|||||||
if (state === 'error') {
|
if (state === 'error') {
|
||||||
stats.errors++
|
stats.errors++
|
||||||
if (message) showError(row, message)
|
if (message) showError(row, message)
|
||||||
} else if (state === 'duplicate') {
|
|
||||||
stats.duplicates++
|
|
||||||
} else {
|
} else {
|
||||||
stats.uploaded++
|
if (state === 'duplicate') stats.duplicates++
|
||||||
|
else stats.uploaded++
|
||||||
|
// A duplicate is added to the share too
|
||||||
|
freshenBackLink()
|
||||||
}
|
}
|
||||||
setAction(row, retryable ? 'retry' : 'dismiss')
|
setAction(row, retryable ? 'retry' : 'dismiss')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export function UploadPage ({ config, basePath, galleryLink, notice }: UploadPag
|
|||||||
<body>
|
<body>
|
||||||
<header id="header">
|
<header id="header">
|
||||||
{galleryLink && (
|
{galleryLink && (
|
||||||
<a class="header-action" href={galleryLink} title="Back to gallery" aria-label="Back to gallery">
|
<a id="upload-back" class="header-action" href={galleryLink} title="Back to gallery" aria-label="Back to gallery">
|
||||||
<Icon path={ICON_ARROW_LEFT}/>
|
<Icon path={ICON_ARROW_LEFT}/>
|
||||||
</a>
|
</a>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user