Tidy up WebP download filename fix #297

This commit is contained in:
Alan Grainger
2026-10-05 09:12:10 +13:00
parent 2a99258278
commit 0ae17abdc0
8 changed files with 33 additions and 38 deletions
+17 -12
View File
@@ -4,7 +4,7 @@ import { sanitize } from '../utils/sanitize'
import { resolveImageEndpoint } from './sizing'
/**
* Map an Immich asset MIME type to a file extension (including the dot).
* Map a MIME type to a file extension (including the dot), case-insensitively.
* Returns '' for unknown types; obscure RAW types fall through to '' so
* the original filename (which usually has the correct extension) is
* preserved by `withMimeExtension`.
@@ -28,7 +28,7 @@ function mimeToExt (mime: string | undefined): string {
'video/x-msvideo': '.avi',
'video/x-matroska': '.mkv'
}
return map[mime] || ''
return map[mime.toLowerCase()] || ''
}
/**
@@ -53,17 +53,13 @@ function withMimeExtension (filename: string, mime: string | undefined): string
/**
* Generate a filename for the downloaded asset based on the configuration option chosen.
*
* The extension reflects the served bytes, not the original: converted
* bytes follow Immich's image settings, so pass the upstream content-type
* as `servedMimeOverride`.
* The extension follows the served bytes, not the original.
*
* @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.
* @param [servedMimeOverride] - MIME of Immich-generated bytes (see servedMimeFrom).
* Ignored if unmapped, so it can't strip the extension.
*/
export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.original, servedMimeOverride?: string): string {
const override = mimeToExt(servedMimeOverride) ? servedMimeOverride : undefined
@@ -100,10 +96,19 @@ export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.ori
}
/**
* Filename for a downloaded asset, whose extension matches the bytes a download
* (`/original` request) actually returns after the `maxDownloadQuality` clamp.
* Used by both the eager (gallery builder) and lazy (`/meta/`) item paths.
* Filename for a download link's `download` attribute. Converted tiers are assumed
* JPEG; the download's Content-Disposition, which browsers prefer, has the real name.
*/
export function downloadFilename (asset: Asset): string {
return getFilename(asset, resolveImageEndpoint(ImageSize.original, asset).servedSize)
}
/** Content-type without its parameters, or undefined if absent. */
export function responseMime (response: globalThis.Response): string | undefined {
return (response.headers.get('content-type') || '').split(';')[0].trim() || undefined
}
/** Content-type of Immich-generated bytes (anything but /original), to name the file by. */
export function servedMimeFrom (subpath: string, response: globalThis.Response): string | undefined {
return subpath === '/original' ? undefined : responseMime(response)
}
+2 -12
View File
@@ -50,7 +50,7 @@ export function isVideoAsset (asset: Asset): boolean {
MIME types a browser can render directly, mirroring Immich's
`isWebSupportedImage`. For these the `fullsize` tier resolves to the original
bytes (`/original`); everything else (RAW/HEIF/TIFF/...) gets Immich's
converted full-size JPEG via `?size=fullsize`.
converted full-size image (JPEG or WebP) via `?size=fullsize`.
*/
const WEB_DISPLAYABLE_MIME = new Set(['image/jpeg', 'image/png', 'image/webp'])
@@ -156,7 +156,7 @@ function endpointFor (size: ImageSize, asset: Asset, attachment: boolean): Image
return { subpath: '/original', attachment, servedSize: ImageSize.original }
case ImageSize.fullsize:
// Web formats: fullsize is the original bytes, served from /original.
// Non-web (RAW/HEIF): Immich's converted full-size JPEG via ?size=fullsize
// Non-web (RAW/HEIF): Immich's converted full-size image via ?size=fullsize
// (falls back to preview unless full-size generation is enabled in Immich).
return isWebDisplayable(asset)
? { subpath: '/original', attachment, servedSize: ImageSize.original }
@@ -165,13 +165,3 @@ function endpointFor (size: ImageSize, asset: Asset, attachment: boolean): Image
return { subpath: '/thumbnail', sizeQueryParam: 'preview', attachment, servedSize: ImageSize.preview }
}
}
/**
* Served content-type for filename derivation: everything but /original is
* Immich-generated (previews can be WebP), so the response headers are the
* only reliable extension signal.
*/
export function servedMimeFrom (subpath: string, response: globalThis.Response): string | undefined {
if (subpath === '/original') return undefined
return (response.headers.get('content-type') || '').split(';')[0].trim() || undefined
}