diff --git a/app/src/gallery/filename.ts b/app/src/gallery/filename.ts index 1329523..705f00e 100644 --- a/app/src/gallery/filename.ts +++ b/app/src/gallery/filename.ts @@ -61,10 +61,17 @@ function withMimeExtension (filename: string, mime: string | undefined): string * @param asset * @param [servedSize] - what size Immich will actually serve. Defaults to * ImageSize.original (the bytes match the original asset). + * @param [servedMimeOverride] - actual MIME of the served bytes when they + * differ from the original (a `/video/playback` transcode). Ignored when it + * doesn't map to a known extension, so an unrecognised content-type can't + * strip the filename's extension. */ -export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.original): string { +export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.original, servedMimeOverride?: string): string { + const override = mimeToExt(servedMimeOverride) ? servedMimeOverride : undefined let servedMime: string | undefined - if (servedSize === ImageSize.original) { + if (override) { + servedMime = override + } else if (servedSize === ImageSize.original) { servedMime = asset.originalMimeType } else if (servedSize === ImageSize.thumbnail) { servedMime = 'image/webp' @@ -83,7 +90,9 @@ export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.ori // By default, use the asset's original filename const cleanName = asset.originalFileName ? sanitize(asset.originalFileName) : '' if (!cleanName) return withMimeExtension(asset.id, servedMime) - const stem = servedSize === ImageSize.original + // Keep the original extension only when the served bytes really are the + // original file; a size downgrade or playback transcode replaces it. + const stem = servedSize === ImageSize.original && !override ? cleanName : cleanName.replace(/\.[a-zA-Z0-9]{2,5}$/, '') return withMimeExtension(stem, servedMime) 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..8fabf0b 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