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)
}