feat: add video streaming proxy and webhook cache invalidation

- Add /api/video/[id] route with Range header support for seeking
- Add /api/webhook route for Immich event-driven cache invalidation
- Add streamVideo() to ImmichClient with 206 Partial Content support
- Support album description overrides in gallery.yaml
- Add videoUrl() helper to lib/urls.ts
- Export cache.delete() and cache.clear() methods
This commit is contained in:
ralksta
2026-05-29 17:04:57 +02:00
parent ce7c1742be
commit e9063d3a71
7 changed files with 267 additions and 5 deletions

View File

@@ -44,6 +44,10 @@ class MemoryCache {
});
}
delete(key: string): void {
this.store.delete(key);
}
clear(): void {
this.store.clear();
}

View File

@@ -21,11 +21,15 @@ export interface SubpageObjectValue {
subtitle?: string;
password?: string;
grid?: Partial<GridConfig>;
albums?: Array<string | Record<string, string | { title: string; password?: string }>>;
albums?: Array<
string | Record<string, string | { title: string; description?: string; password?: string }>
>;
sections?: Array<{
title: string;
description?: string;
albums: Array<string | Record<string, string | { title: string; password?: string }>>;
albums: Array<
string | Record<string, string | { title: string; description?: string; password?: string }>
>;
}>;
}
@@ -91,6 +95,7 @@ export interface AppConfig {
map: boolean;
transitions: boolean;
albumOverrides: Record<string, string>;
albumDescriptions: Record<string, string>;
albumPasswords: Record<string, string>;
cacheTtl: number;
rateLimitRpm: number;
@@ -100,18 +105,26 @@ export interface AppConfig {
export interface GalleryYaml {
hero?: string | string[];
albums?: string[];
albums?: Array<
string | Record<string, string | { title: string; description?: string; password?: string }>
>;
subpages?:
| Record<string, string[] | Array<string | Record<string, string>>>
| Array<{
name: string;
title?: string;
subtitle?: string;
albums?: Array<string | Record<string, string | { title: string; password?: string }>>;
albums?: Array<
| string
| Record<string, string | { title: string; description?: string; password?: string }>
>;
sections?: Array<{
title: string;
description?: string;
albums: Array<string | Record<string, string | { title: string; password?: string }>>;
albums: Array<
| string
| Record<string, string | { title: string; description?: string; password?: string }>
>;
}>;
password?: string;
grid?: {

View File

@@ -108,6 +108,52 @@ class ImmichClient {
}
}
/**
* Stream a video from Immich with optional Range header forwarding.
* Range support is required for `<video>` seeking in browsers.
*/
async streamVideo(
assetId: string,
rangeHeader?: string | null,
): Promise<{
stream: ReadableStream;
contentType: string;
contentLength: string | null;
contentRange: string | null;
status: 200 | 206;
} | null> {
if (this.config.needsSetup || !this.config.immich.apiUrl) return null;
const endpoint = `/assets/${encodeURIComponent(assetId)}/video/playback`;
const url = `${this.config.immich.apiUrl}${endpoint}`;
try {
const headers: Record<string, string> = {
'x-api-key': this.config.immich.apiKey,
};
if (rangeHeader) {
headers['Range'] = rangeHeader;
}
const res = await fetch(url, { headers });
if ((!res.ok && res.status !== 206) || !res.body) {
console.error(`[Immich] Failed to stream video ${assetId}: ${res.status}`);
return null;
}
return {
stream: res.body,
contentType: res.headers.get('Content-Type') || 'video/mp4',
contentLength: res.headers.get('Content-Length'),
contentRange: res.headers.get('Content-Range'),
status: res.status === 206 ? 206 : 200,
};
} catch (error) {
console.error(`[Immich] Video stream error for ${assetId}:`, error);
return null;
}
}
/**
* Stream a binary response from Immich (for image proxying).
*/
@@ -169,9 +215,11 @@ class ImmichClient {
.filter((album) => allowedIds.has(album.id))
.map((album) => {
const name = this.config.albumOverrides[album.id] ?? album.albumName;
const description = this.config.albumDescriptions[album.id] ?? album.description ?? '';
return {
...album,
albumName: name,
description,
slug: slugify(name),
};
});
@@ -296,7 +344,9 @@ class ImmichClient {
album.assets = (album.assets || []).filter((a) => !a.isTrashed);
const name = this.config.albumOverrides[album.id] ?? album.albumName;
const description = this.config.albumDescriptions[album.id] ?? album.description ?? '';
album.albumName = name;
album.description = description;
album.slug = slugify(name);
cache.set(cacheKey, album, this.config.cacheTtl);
@@ -368,6 +418,25 @@ class ImmichClient {
return promise;
}
/**
* Invalidate the cache for a specific album (by Immich UUID) and the
* shared albums-list entry. Called by the webhook handler after an
* album.updated / album.assetAdded event from Immich.
*/
invalidateAlbum(albumId: string): void {
cache.delete(`album-${albumId}`);
cache.delete('albums-list');
console.log(`[Immich] 🔄 Cache invalidated for album ${albumId}`);
}
/**
* Invalidate all album caches at once (e.g. on a full sync event).
*/
invalidateAll(): void {
cache.clear();
console.log('[Immich] 🔄 Full cache cleared via webhook');
}
/**
* Check if the Immich server is reachable.
*/

View File

@@ -24,6 +24,13 @@ export function exifUrl(assetId: string): string {
return `/api/exif/${encodeAssetId(assetId)}`;
}
/**
* Generate a public video proxy URL for an asset.
*/
export function videoUrl(assetId: string): string {
return `/api/video/${encodeAssetId(assetId)}`;
}
/**
* Placeholder data derived from an asset's ThumbHash.
*/