Tidy up WebP download filename fix #297
This commit is contained in:
+17
-12
@@ -4,7 +4,7 @@ import { sanitize } from '../utils/sanitize'
|
|||||||
import { resolveImageEndpoint } from './sizing'
|
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
|
* Returns '' for unknown types; obscure RAW types fall through to '' so
|
||||||
* the original filename (which usually has the correct extension) is
|
* the original filename (which usually has the correct extension) is
|
||||||
* preserved by `withMimeExtension`.
|
* preserved by `withMimeExtension`.
|
||||||
@@ -28,7 +28,7 @@ function mimeToExt (mime: string | undefined): string {
|
|||||||
'video/x-msvideo': '.avi',
|
'video/x-msvideo': '.avi',
|
||||||
'video/x-matroska': '.mkv'
|
'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.
|
* Generate a filename for the downloaded asset based on the configuration option chosen.
|
||||||
*
|
*
|
||||||
* The extension reflects the served bytes, not the original: converted
|
* The extension follows the served bytes, not the original.
|
||||||
* bytes follow Immich's image settings, so pass the upstream content-type
|
|
||||||
* as `servedMimeOverride`.
|
|
||||||
*
|
*
|
||||||
* @param asset
|
* @param asset
|
||||||
* @param [servedSize] - what size Immich will actually serve. Defaults to
|
* @param [servedSize] - what size Immich will actually serve. Defaults to
|
||||||
* ImageSize.original (the bytes match the original asset).
|
* ImageSize.original (the bytes match the original asset).
|
||||||
* @param [servedMimeOverride] - actual MIME of the served bytes when they
|
* @param [servedMimeOverride] - MIME of Immich-generated bytes (see servedMimeFrom).
|
||||||
* differ from the original (a `/video/playback` transcode). Ignored when it
|
* Ignored if unmapped, so it can't strip the extension.
|
||||||
* 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, servedMimeOverride?: string): string {
|
export function getFilename (asset: Asset, servedSize: ImageSize = ImageSize.original, servedMimeOverride?: string): string {
|
||||||
const override = mimeToExt(servedMimeOverride) ? servedMimeOverride : undefined
|
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
|
* Filename for a download link's `download` attribute. Converted tiers are assumed
|
||||||
* (`/original` request) actually returns after the `maxDownloadQuality` clamp.
|
* JPEG; the download's Content-Disposition, which browsers prefer, has the real name.
|
||||||
* Used by both the eager (gallery builder) and lazy (`/meta/`) item paths.
|
|
||||||
*/
|
*/
|
||||||
export function downloadFilename (asset: Asset): string {
|
export function downloadFilename (asset: Asset): string {
|
||||||
return getFilename(asset, resolveImageEndpoint(ImageSize.original, asset).servedSize)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export function isVideoAsset (asset: Asset): boolean {
|
|||||||
MIME types a browser can render directly, mirroring Immich's
|
MIME types a browser can render directly, mirroring Immich's
|
||||||
`isWebSupportedImage`. For these the `fullsize` tier resolves to the original
|
`isWebSupportedImage`. For these the `fullsize` tier resolves to the original
|
||||||
bytes (`/original`); everything else (RAW/HEIF/TIFF/...) gets Immich's
|
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'])
|
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 }
|
return { subpath: '/original', attachment, servedSize: ImageSize.original }
|
||||||
case ImageSize.fullsize:
|
case ImageSize.fullsize:
|
||||||
// Web formats: fullsize is the original bytes, served from /original.
|
// 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).
|
// (falls back to preview unless full-size generation is enabled in Immich).
|
||||||
return isWebDisplayable(asset)
|
return isWebDisplayable(asset)
|
||||||
? { subpath: '/original', attachment, servedSize: ImageSize.original }
|
? { 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 }
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import {
|
|||||||
import { Response } from 'express-serve-static-core'
|
import { Response } from 'express-serve-static-core'
|
||||||
import { Asset, ImageSize, IncomingShareRequest, SharedLink } from '../types'
|
import { Asset, ImageSize, IncomingShareRequest, SharedLink } from '../types'
|
||||||
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
||||||
import { getFilename } from '../gallery/filename'
|
import { getFilename, servedMimeFrom } from '../gallery/filename'
|
||||||
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint, servedMimeFrom } from '../gallery/sizing'
|
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../gallery/sizing'
|
||||||
import { pipeline } from 'stream/promises'
|
import { pipeline } from 'stream/promises'
|
||||||
import { readableFromWeb } from '../utils/webStream'
|
import { readableFromWeb } from '../utils/webStream'
|
||||||
import { log } from '../utils/log'
|
import { log } from '../utils/log'
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import { Asset, SharedLink } from '../types'
|
|||||||
import { log } from '../utils/log'
|
import { log } from '../utils/log'
|
||||||
import archiver, { Archiver } from 'archiver'
|
import archiver, { Archiver } from 'archiver'
|
||||||
import { sanitize } from '../utils/sanitize'
|
import { sanitize } from '../utils/sanitize'
|
||||||
import { resolveDownloadEndpoint, servedMimeFrom, ImageEndpoint } from '../gallery/sizing'
|
import { resolveDownloadEndpoint, ImageEndpoint } from '../gallery/sizing'
|
||||||
import { title } from '../share'
|
import { title } from '../share'
|
||||||
import { getFilename } from '../gallery/filename'
|
import { getFilename, responseMime, servedMimeFrom } from '../gallery/filename'
|
||||||
import { readableFromWeb } from '../utils/webStream'
|
import { readableFromWeb } from '../utils/webStream'
|
||||||
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
import { respondToInvalidRequest } from '../invalidRequestHandler'
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ async function fetchOne (share: SharedLink, asset: Asset, signal: AbortSignal):
|
|||||||
*/
|
*/
|
||||||
function enrichFromHeaders (asset: Asset, response: globalThis.Response): Asset {
|
function enrichFromHeaders (asset: Asset, response: globalThis.Response): Asset {
|
||||||
const fileName = filenameFromContentDisposition(response.headers.get('content-disposition'))
|
const fileName = filenameFromContentDisposition(response.headers.get('content-disposition'))
|
||||||
const mime = (response.headers.get('content-type') || '').split(';')[0].trim() || undefined
|
const mime = responseMime(response)
|
||||||
if (!fileName && !mime) return asset
|
if (!fileName && !mime) return asset
|
||||||
return {
|
return {
|
||||||
...asset,
|
...asset,
|
||||||
|
|||||||
@@ -198,9 +198,7 @@ describe('assetBuffer streaming', () => {
|
|||||||
expect(res.headers['content-length']).toBe(String(4 * CHUNK))
|
expect(res.headers['content-length']).toBe(String(4 * CHUNK))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('names a preview-clamped download after the served bytes, not .jpg (WebP preview generation)', async () => {
|
it('names a WebP preview download .webp', async () => {
|
||||||
// maxDownloadQuality=preview with Immich generating WebP previews: the
|
|
||||||
// Content-Disposition extension must follow the response content-type.
|
|
||||||
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
|
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
|
||||||
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response(
|
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response(
|
||||||
new ReadableStream({
|
new ReadableStream({
|
||||||
|
|||||||
@@ -285,9 +285,7 @@ describe('downloadAssets', () => {
|
|||||||
expect(signals.every(s => s.aborted)).toBe(true)
|
expect(signals.every(s => s.aborted)).toBe(true)
|
||||||
}, 10_000)
|
}, 10_000)
|
||||||
|
|
||||||
it('names a preview-clamped entry after the served bytes, not .jpg (WebP preview generation)', async () => {
|
it('names a WebP preview zip entry .webp', async () => {
|
||||||
// Immich generates previews in an operator-chosen format; with WebP
|
|
||||||
// selected and maxDownloadQuality=preview, the zip entry must be .webp.
|
|
||||||
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
|
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
|
||||||
const fetchMock = vi.fn(async () => new globalThis.Response(new Uint8Array(2048), {
|
const fetchMock = vi.fn(async () => new globalThis.Response(new Uint8Array(2048), {
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|||||||
@@ -56,11 +56,15 @@ describe('getFilename servedMime override (playback-fallback downloads)', () =>
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getFilename converted-byte previews (e.g. WebP preview generation)', () => {
|
describe('getFilename WebP previews', () => {
|
||||||
it('swaps the extension to match the served preview bytes', () => {
|
it('swaps the extension to match the served preview bytes', () => {
|
||||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/webp')).toBe('holiday.webp')
|
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/webp')).toBe('holiday.webp')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('matches the served MIME type case-insensitively', () => {
|
||||||
|
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/WebP')).toBe('holiday.webp')
|
||||||
|
})
|
||||||
|
|
||||||
it('keeps a JPEG extension when the preview is JPEG', () => {
|
it('keeps a JPEG extension when the preview is JPEG', () => {
|
||||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/jpeg')).toBe('holiday.jpg')
|
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/jpeg')).toBe('holiday.jpg')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ to prevent CDN issues, and the gallery page uses its own [`gallery.cacheTime`](/
|
|||||||
Highest quality served for a download (the download button and "download all" zip).
|
Highest quality served for a download (the download button and "download all" zip).
|
||||||
|
|
||||||
- `"original"` - the full-resolution original file (default).
|
- `"original"` - the full-resolution original file (default).
|
||||||
- `"fullsize"` - full resolution but always browser-displayable: the original for JPEG/PNG/WebP, Immich's converted JPEG for RAW/HEIF.
|
- `"fullsize"` - full resolution but always browser-displayable: the original for JPEG/PNG/WebP, Immich's converted full-size image for RAW/HEIF, in JPEG or WebP per the server's image settings.
|
||||||
- `"preview"` - only the ~1440px preview. The format is whatever Immich generates for its preview (JPEG or WebP, per the server's image settings), and downloaded filenames follow it.
|
- `"preview"` - only the ~1440px preview, in JPEG or WebP per the server's image settings.
|
||||||
|
|
||||||
## `maxZoomQuality`
|
## `maxZoomQuality`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user