From 72ae93ad9dcdc90ec5099e08beede21a386a40f2 Mon Sep 17 00:00:00 2001 From: mFIND <-> Date: Fri, 14 Aug 2026 16:47:13 +0200 Subject: [PATCH 1/2] Issue #239 fix --- app/src/gallery/sizing.ts | 33 ++++++++++++++++++++++++------ app/src/immich.ts | 2 +- app/src/index.ts | 4 ++-- app/src/stream/asset.ts | 18 +++++++++++------ app/src/stream/download.ts | 14 ++++++------- app/tests/sizing.test.ts | 41 +++++++++++++++++++++++++++++++++++++- 6 files changed, 89 insertions(+), 23 deletions(-) diff --git a/app/src/gallery/sizing.ts b/app/src/gallery/sizing.ts index 615f3a4..9afead8 100644 --- a/app/src/gallery/sizing.ts +++ b/app/src/gallery/sizing.ts @@ -24,14 +24,12 @@ export interface ImageEndpoint { * listed because Immich doesn't expose a distinct MIME type for them - they * share `image/png` / `image/webp` with their static counterparts. * - * Used by both the display path (lightbox preview URL) and the download path - * (single-asset download + zip), so the lightbox shows the same bytes the - * user gets when they hit "download". + * Used by display sizing and by downloads that Immich can serve from the + * original endpoint. IPP-forced video downloads have their own playback + * fallback in `resolveDownloadEndpoint`. */ export function requiresOriginal (asset: Asset): boolean { - if (asset.type === AssetType.video) { - return true - } else if (asset.originalMimeType?.startsWith('video/')) { + if (isVideoAsset(asset)) { return true } else if (asset.originalMimeType === 'image/gif') { return true @@ -39,6 +37,15 @@ export function requiresOriginal (asset: Asset): boolean { return false } +/** + * Whether an asset is a video. Route handlers can coerce `type` from the URL, + * while detailed Immich asset responses identify video originals by MIME type; + * endpoint selection accepts either signal. + */ +export function isVideoAsset (asset: Asset): boolean { + return asset.type === AssetType.video || !!asset.originalMimeType?.startsWith('video/') +} + /* MIME types a browser can render directly, mirroring Immich's `isWebSupportedImage`. For these the `fullsize` tier resolves to the original @@ -125,6 +132,20 @@ export function resolveImageEndpoint (requested: ImageSize, asset: Asset): Image return endpointFor(served, asset, attachment) } +/** + * Resolve the endpoint for an IPP download. This is usually the same as an + * `original` image-size request. The exception is a video on a shared link + * whose Immich download permission is off: Immich refuses `/original` with + * `asset.download` access, while `/video/playback` can still serve the + * transcoded playback file that visitors are allowed to stream. + */ +export function resolveDownloadEndpoint (asset: Asset, immichAllowsOriginalDownload = true): ImageEndpoint { + if (!immichAllowsOriginalDownload && isVideoAsset(asset)) { + return { subpath: '/video/playback', attachment: true, servedSize: ImageSize.original } + } + return resolveImageEndpoint(ImageSize.original, asset) +} + /** * The one place that maps a (clamped) ImageSize onto a concrete Immich path, * and the only place that knows `fullsize` is per-asset. diff --git a/app/src/immich.ts b/app/src/immich.ts index c965bd0..89d5a22 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -209,7 +209,7 @@ export async function handleShareRequest (req: IncomingShareRequest, res: Respon if ((directImage || directVideo) && !req.password) { // Output the asset directly rather than a gallery page, unless it's a // password-protected link - await assetBuffer(req, res, link.assets[0], ImageSize.preview) + await assetBuffer(req, res, link.assets[0], ImageSize.preview, link, directVideo) } else { // Show a gallery page const openItem = getConfigOption('ipp.gallery.singleItemAutoOpen', true) ? 1 : 0 diff --git a/app/src/index.ts b/app/src/index.ts index 3364989..7805638 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -266,7 +266,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, asyncHandler( } const asset: Asset = { ...resolved.asset, - type: req.params.type === 'video' ? AssetType.video : AssetType.image + type: req.params.type === 'video' ? AssetType.video : resolved.asset.type } const request = { @@ -274,7 +274,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, asyncHandler( key: req.params.key, range: req.headers.range || '' } - await assetBuffer(request, res, asset, req.params.size) + await assetBuffer(request, res, asset, req.params.size, resolved.link, req.params.type === 'video') })) /* diff --git a/app/src/stream/asset.ts b/app/src/stream/asset.ts index e406ec9..6be31b5 100644 --- a/app/src/stream/asset.ts +++ b/app/src/stream/asset.ts @@ -5,10 +5,10 @@ import { validateImageSize } from '../immich' import { Response } from 'express-serve-static-core' -import { Asset, AssetType, ImageSize, IncomingShareRequest } from '../types' +import { Asset, ImageSize, IncomingShareRequest, SharedLink } from '../types' import { respondToInvalidRequest } from '../invalidRequestHandler' import { getFilename } from '../gallery/filename' -import { resolveImageEndpoint } from '../gallery/sizing' +import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../gallery/sizing' /** * Stream an asset from Immich back to the client. @@ -18,7 +18,7 @@ import { resolveImageEndpoint } from '../gallery/sizing' * or locked assets are handled implicitly: Immich's own endpoints refuse * to serve them, and the upstream failure surfaces as a client 404. */ -export async function assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string) { +export async function assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string, share?: SharedLink, forceVideoPlayback = false) { const headerList = ['content-type', 'content-length', 'last-modified', 'etag'] const fetchHeaders: Record = {} let subpath: string @@ -26,8 +26,13 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass let attachment = false let servedSize: ImageSize | undefined - if (asset.type === AssetType.video) { + const requested = validateImageSize(size) + const useVideoPlayback = forceVideoPlayback || (isVideoAsset(asset) && requested === ImageSize.original && share?.allowDownload === false) + + if (useVideoPlayback) { subpath = '/video/playback' + attachment = requested === ImageSize.original + servedSize = ImageSize.original res.setHeader('accept-ranges', 'bytes') // Only chunk when the client sent a Range header. A browser