Fix download file extensions when Immich serves WebP previews
With maxDownloadQuality=preview and Immich generating WebP previews, the zip entries and per-asset downloads were named .jpg while carrying WebP bytes. Previews, converted fullsize images and transcodes follow the server's image settings, so derive the extension from the upstream content-type for every non-/original endpoint, via a shared servedMimeFrom() in gallery/sizing.ts.
This commit is contained in:
@@ -2,12 +2,20 @@ import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { Writable } from 'stream'
|
||||
import type { Request, Response } from 'express-serve-static-core'
|
||||
import { assetBuffer } from '../src/stream/asset'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { Asset, AssetType, ImageSize, IncomingShareRequest, KeyType } from '../src/types'
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
delete process.env.CONFIG
|
||||
loadConfig()
|
||||
})
|
||||
|
||||
function setConfig (config: unknown) {
|
||||
process.env.CONFIG = JSON.stringify(config)
|
||||
loadConfig()
|
||||
}
|
||||
|
||||
const CHUNK = 64 * 1024
|
||||
|
||||
const asset: Asset = {
|
||||
@@ -189,4 +197,20 @@ describe('assetBuffer streaming', () => {
|
||||
expect(source.cancelled).toBe(true)
|
||||
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 () => {
|
||||
// maxDownloadQuality=preview with Immich generating WebP previews: the
|
||||
// Content-Disposition extension must follow the response content-type.
|
||||
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
|
||||
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response(
|
||||
new ReadableStream({
|
||||
start (controller) { controller.enqueue(new Uint8Array(1024)); controller.close() }
|
||||
}),
|
||||
{ status: 200, headers: { 'content-type': 'image/webp', 'content-length': '1024' } }
|
||||
)))
|
||||
const res = new FakeRes()
|
||||
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
|
||||
expect(res.headers['content-disposition']).toContain('a1.webp')
|
||||
expect(res.headers['content-type']).toBe('image/webp')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Writable } from 'stream'
|
||||
import { once } from 'events'
|
||||
import type { Response } from 'express-serve-static-core'
|
||||
import { downloadAssets } from '../src/stream/download'
|
||||
import { loadConfig } from '../src/config/loader'
|
||||
import { Asset, AssetType, KeyType, SharedLink } from '../src/types'
|
||||
|
||||
/*
|
||||
@@ -16,8 +17,15 @@ dies mid-stream).
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.restoreAllMocks()
|
||||
delete process.env.CONFIG
|
||||
loadConfig()
|
||||
})
|
||||
|
||||
function setConfig (config: unknown) {
|
||||
process.env.CONFIG = JSON.stringify(config)
|
||||
loadConfig()
|
||||
}
|
||||
|
||||
function makeAsset (id: string): Asset {
|
||||
return {
|
||||
id,
|
||||
@@ -276,4 +284,19 @@ describe('downloadAssets', () => {
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(signals.every(s => s.aborted)).toBe(true)
|
||||
}, 10_000)
|
||||
|
||||
it('names a preview-clamped entry after the served bytes, not .jpg (WebP preview generation)', 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' } })
|
||||
const fetchMock = vi.fn(async () => new globalThis.Response(new Uint8Array(2048), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'image/webp' }
|
||||
}))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const res = new FakeRes()
|
||||
await downloadAssets(asResponse(res), share, [makeAsset('a1')])
|
||||
expect(String(fetchMock.mock.calls[0][0])).toContain('size=preview')
|
||||
expect(centralDirectory(res.output)).toEqual([{ name: 'a1.webp', size: 2048 }])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,6 +24,18 @@ function videoAsset (originalFileName: string, originalMimeType = 'video/x-msvid
|
||||
}
|
||||
}
|
||||
|
||||
function imageAsset (originalFileName: string, originalMimeType = 'image/heic'): Asset {
|
||||
return {
|
||||
id: 'asset-id',
|
||||
key: 'k',
|
||||
keyType: KeyType.key,
|
||||
type: AssetType.image,
|
||||
isTrashed: false,
|
||||
originalFileName,
|
||||
originalMimeType
|
||||
}
|
||||
}
|
||||
|
||||
describe('getFilename servedMime override (playback-fallback downloads)', () => {
|
||||
it('swaps the extension to match the transcoded bytes', () => {
|
||||
expect(getFilename(videoAsset('clip.avi'), ImageSize.original, 'video/mp4')).toBe('clip.mp4')
|
||||
@@ -43,3 +55,17 @@ describe('getFilename servedMime override (playback-fallback downloads)', () =>
|
||||
expect(getFilename(videoAsset('clip.avi'), ImageSize.original)).toBe('clip.avi')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getFilename converted-byte previews (e.g. WebP preview generation)', () => {
|
||||
it('swaps the extension to match the served preview bytes', () => {
|
||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/webp')).toBe('holiday.webp')
|
||||
})
|
||||
|
||||
it('keeps a JPEG extension when the preview is JPEG', () => {
|
||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview, 'image/jpeg')).toBe('holiday.jpg')
|
||||
})
|
||||
|
||||
it('falls back to the hardcoded tier mime without an override', () => {
|
||||
expect(getFilename(imageAsset('holiday.heic'), ImageSize.preview)).toBe('holiday.jpg')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user