Add nostore to upload healthcheck and fix double-sort

This commit is contained in:
Alan Grainger
2026-10-20 09:39:37 +13:00
parent fc7670157b
commit 6ae80569ff
10 changed files with 115 additions and 68 deletions
+26 -12
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, afterEach } from 'vitest'
import { Asset, AssetType, KeyType, loadConfig } from '@ipp/core'
import { Asset, AssetType, KeyType, loadConfig, SharedLink } from '@ipp/core'
import { Writable } from 'stream'
import type { Request, Response } from 'express-serve-static-core'
import { assetBuffer } from '../src/stream/asset'
@@ -28,6 +28,11 @@ const asset: Asset = {
originalMimeType: 'image/jpeg'
}
/** A share holding just `item`, which every streamed asset belongs to. */
function shareOf (item: Asset): SharedLink {
return { key: 'testkey', keyType: KeyType.key, type: 'INDIVIDUAL', assets: [item] }
}
function makeRequest (method = 'GET'): IncomingShareRequest {
return { req: { method } as Request, key: 'testkey', range: '' }
}
@@ -117,10 +122,11 @@ function newSource (): Source {
describe('assetBuffer streaming', () => {
it('delivers the whole body and forwards upstream headers', async () => {
setConfig({ ipp: { downloadedFilename: 0 } })
const source = newSource()
vi.stubGlobal('fetch', pullFetch(4 * CHUNK, source))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(res.received).toBe(4 * CHUNK)
expect(res.writableFinished).toBe(true)
expect(res.headers['content-type']).toBe('image/jpeg')
@@ -129,6 +135,13 @@ describe('assetBuffer streaming', () => {
expect(res.headers['x-accel-buffering']).toBe('no')
})
it('names a download by its position in the share by default', async () => {
vi.stubGlobal('fetch', pullFetch(CHUNK, newSource()))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(res.headers['content-disposition']).toMatch(/''[0-9a-f]{8}_001\.jpg$/)
})
it('applies backpressure so upstream is read no faster than the visitor drains it', async () => {
// Issue #288: a fast NAS read against a slow visitor used to buffer the
// whole file in memory. Track how far ahead of the sink the source gets.
@@ -142,7 +155,7 @@ describe('assetBuffer streaming', () => {
maxOutstanding = Math.max(maxOutstanding, source.pulled - res.received)
origWrite(chunk, enc, cb)
}
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(res.received).toBe(total)
expect(maxOutstanding).toBeLessThan(1024 * 1024)
}, 20_000)
@@ -160,7 +173,7 @@ describe('assetBuffer streaming', () => {
setImmediate(() => res.destroy(new Error('client aborted')))
}
}
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(source.cancelled || source.signals[0].aborted).toBe(true)
// Upstream must stop shortly after the disconnect, not run to infinity
expect(source.pulled).toBeLessThan(1024 * 1024)
@@ -171,7 +184,7 @@ describe('assetBuffer streaming', () => {
vi.stubGlobal('fetch', pullFetch(Infinity, source, 200))
const res = new FakeRes()
setTimeout(() => res.destroy(new Error('client aborted')), 20)
await expect(assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)).resolves.toBeUndefined()
await expect(assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))).resolves.toBeUndefined()
expect(source.signals[0].aborted).toBe(true)
expect(source.pulled).toBe(0)
expect(res.received).toBe(0)
@@ -182,7 +195,7 @@ describe('assetBuffer streaming', () => {
vi.stubGlobal('fetch', pullFetch(Infinity, source, 50))
const res = new FakeRes()
res.destroy()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(source.signals[0].aborted).toBe(true)
expect(source.pulled).toBe(0)
})
@@ -191,7 +204,7 @@ describe('assetBuffer streaming', () => {
const source = newSource()
vi.stubGlobal('fetch', pullFetch(4 * CHUNK, source))
const res = new FakeRes()
await assetBuffer(makeRequest('HEAD'), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest('HEAD'), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(res.writableEnded).toBe(true)
expect(res.received).toBe(0)
expect(source.cancelled).toBe(true)
@@ -199,7 +212,7 @@ describe('assetBuffer streaming', () => {
})
it('names a WebP preview download .webp', async () => {
setConfig({ ipp: { maxDownloadQuality: 'preview' } })
setConfig({ ipp: { maxDownloadQuality: 'preview', downloadedFilename: 0 } })
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response(
new ReadableStream({
start (controller) { controller.enqueue(new Uint8Array(1024)); controller.close() }
@@ -207,7 +220,7 @@ describe('assetBuffer streaming', () => {
{ status: 200, headers: { 'content-type': 'image/webp', 'content-length': '1024' } }
)))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.original, shareOf(asset))
expect(res.headers['content-disposition']).toContain('a1.webp')
expect(res.headers['content-type']).toBe('image/webp')
})
@@ -227,15 +240,16 @@ describe('assetBuffer streaming', () => {
setConfig({ ipp: { downloadedFilename: 0 } })
vi.stubGlobal('fetch', svgFetch({ 'content-disposition': 'attachment; filename="drawing.svg"' }))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original, shareOf(albumAsset))
expect(res.headers['content-disposition']).toBe("attachment; filename*=UTF-8''drawing.svg")
expect(res.headers['x-content-type-options']).toBe('nosniff')
})
it('falls back to the asset id when Immich sends no filename', async () => {
setConfig({ ipp: { downloadedFilename: 0 } })
vi.stubGlobal('fetch', svgFetch({}))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original)
await assetBuffer(makeRequest(), asResponse(res), albumAsset, ImageSize.original, shareOf(albumAsset))
expect(res.headers['content-disposition']).toBe("attachment; filename*=UTF-8''a2.svg")
})
})
@@ -243,7 +257,7 @@ describe('assetBuffer streaming', () => {
it('serves display sizes inline, still with nosniff', async () => {
vi.stubGlobal('fetch', pullFetch(CHUNK, newSource()))
const res = new FakeRes()
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.preview)
await assetBuffer(makeRequest(), asResponse(res), asset, ImageSize.preview, shareOf(asset))
expect(res.headers['content-disposition']).toBeUndefined()
expect(res.headers['x-content-type-options']).toBe('nosniff')
})
+9 -3
View File
@@ -3,9 +3,9 @@ import { Asset, AssetType, KeyType, loadConfig } from '@ipp/core'
import { dateSortComparator, displayOrder } from '../src/gallery/order'
/*
The date-grouping sort must follow the album's own order while
keeping undated assets last in BOTH directions. displayOrder applies it,
after the album's own sort, when `gallery.groupByDate` is on.
The date sort must follow the album's own order while keeping undated
assets last in BOTH directions. displayOrder sorts once: by local time when
`gallery.groupByDate` is on, else by capture time in the album's order.
*/
afterEach(() => {
@@ -84,6 +84,12 @@ describe('displayOrder', () => {
expect(ids(displayOrder(album(), 'desc'))).toEqual(['b', 'a', 'c'])
})
it('puts undated assets last in an album sort', () => {
const withUndated = () => [dated('a', '2024-02-01T10:00:00.000Z'), { ...asset(), id: 'u' }, dated('c', '2024-01-01T10:00:00.000Z')]
expect(ids(displayOrder(withUndated(), 'asc'))).toEqual(['c', 'a', 'u'])
expect(ids(displayOrder(withUndated(), 'desc'))).toEqual(['a', 'c', 'u'])
})
it('sorts by local time when date grouping is on, so buckets stay contiguous', () => {
process.env.CONFIG = JSON.stringify({ ipp: { gallery: { groupByDate: 'day' } } })
loadConfig()