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
+21 -6
View File
@@ -5,7 +5,7 @@ import { resolveImageEndpoint } from './sizing'
/**
* Where an asset sits in its share, for the position-based download names of
* `downloadedFilename: 2`. Built with `positionInShare` / `findPositionInShare`.
* `downloadedFilename: 2`. Built with `positionInShare`, `sharePositions` or `findPositionInShare`.
*/
export interface AssetPosition {
/** The share's key. Only its hash reaches a filename, never the key itself. */
@@ -25,13 +25,28 @@ export function positionInShare (share: SharedLink, index: number): AssetPositio
}
/**
* Look up an asset's position in its share by id. A motion photo's clip takes
* the position of the still it belongs to. Undefined when the id is not in
* the share, which `getFilename` treats as "no position".
* Every asset's position in its share, by id, for a caller that looks up many.
* A motion photo's clip takes the position of the still it belongs to. The
* first asset to claim an id keeps it.
*/
export function sharePositions (share: SharedLink): Map<string, AssetPosition> {
const positions = new Map<string, AssetPosition>()
share.assets.forEach((asset, index) => {
const position = positionInShare(share, index)
for (const id of [asset.id, asset.livePhotoVideoId]) {
if (id && !positions.has(id)) positions.set(id, position)
}
})
return positions
}
/**
* Look up one asset's position in its share by id, as `sharePositions` does.
* Undefined when the id is not in the share, which `getFilename` treats as
* "no position".
*/
export function findPositionInShare (share: SharedLink, assetId: string): AssetPosition | undefined {
const index = share.assets.findIndex(a => a.id === assetId || a.livePhotoVideoId === assetId)
return index === -1 ? undefined : positionInShare(share, index)
return sharePositions(share).get(assetId)
}
/** `ipp.downloadedFilename` when the operator's config omits it: share hash plus position */
+17 -15
View File
@@ -6,36 +6,38 @@ import type { GroupByDateMode } from '../shared/types'
* when the share is loaded, so the gallery, the zip download and the single
* download all see the same order and an asset's position is its index.
*
* An album with a sort order is sorted by capture time in that direction.
* Date grouping then sorts by the local timestamp the client buckets on, so
* buckets stay contiguous (`dateSortComparator`).
* With date grouping on, sorts by the local timestamp the client buckets on,
* so buckets stay contiguous. Otherwise an album with a sort order is sorted
* by capture time in that direction, and anything else keeps Immich's order.
*
* Sorts in place and returns the same array.
*/
export function displayOrder (assets: Asset[], albumOrder?: string): Asset[] {
if (albumOrder === 'asc') {
assets.sort((a, b) => a?.fileCreatedAt?.localeCompare(b.fileCreatedAt || '') || 0)
} else if (albumOrder === 'desc') {
assets.sort((a, b) => b?.fileCreatedAt?.localeCompare(a.fileCreatedAt || '') || 0)
}
if (groupByDateMode()) {
assets.sort(dateSortComparator(albumOrder))
} else if (albumOrder === 'asc' || albumOrder === 'desc') {
assets.sort(dateSortComparator(albumOrder, captureTime))
}
return assets
}
/** The local timestamp the client's date groups bucket on */
const localTime = (a: Asset) => a.localDateTime || a.fileCreatedAt || ''
/** The capture timestamp in UTC, which Immich sorts an album by */
const captureTime = (a: Asset) => a.fileCreatedAt || ''
/**
* Comparator for the date-grouping sort: ascending when the album's order is
* `'asc'`, otherwise newest-first (individual shares and orderless albums).
* Comparator on the timestamp `key` returns: ascending when the album's order
* is `'asc'`, otherwise newest-first (individual shares and orderless albums).
* Undated assets always sort last regardless of direction, so the client's
* "Undated" group renders at the bottom.
* "Undated" group renders at the bottom and the order is consistent.
*/
export function dateSortComparator (order?: string): (a: Asset, b: Asset) => number {
export function dateSortComparator (order?: string, key: (a: Asset) => string = localTime): (a: Asset, b: Asset) => number {
const ascending = order === 'asc'
const sortKey = (a: Asset) => a.localDateTime || a.fileCreatedAt || ''
return (a, b) => {
const ka = sortKey(a)
const kb = sortKey(b)
const ka = key(a)
const kb = key(b)
if (!ka || !kb) return ka ? -1 : kb ? 1 : 0 // undated always last
return ascending ? ka.localeCompare(kb) : kb.localeCompare(ka)
}
+4 -4
View File
@@ -19,7 +19,7 @@ import { pipeline } from 'stream/promises'
* or locked assets are handled implicitly: Immich's own endpoints refuse
* to serve them, and the upstream failure surfaces as a client 404.
*/
export async function assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string, share?: SharedLink, forceVideoPlayback = false) {
export async function assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size: ImageSize | string | undefined, share: SharedLink, forceVideoPlayback = false) {
// A cancelled download must not leave Immich streaming #288
const upstream = abortOnClose(res)
@@ -31,7 +31,7 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass
let servedSize: ImageSize | undefined
const requested = validateImageSize(size)
const useVideoPlayback = forceVideoPlayback || (isVideoAsset(asset) && requested === ImageSize.original && share?.allowDownload === false)
const useVideoPlayback = forceVideoPlayback || (isVideoAsset(asset) && requested === ImageSize.original && share.allowDownload === false)
if (useVideoPlayback) {
subpath = '/video/playback'
@@ -60,7 +60,7 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass
if (detail?.originalMimeType) asset = { ...asset, originalMimeType: detail.originalMimeType }
}
const endpoint = requested === ImageSize.original
? resolveDownloadEndpoint(asset, share?.allowDownload !== false)
? resolveDownloadEndpoint(asset, share.allowDownload !== false)
: resolveImageEndpoint(requested, asset)
subpath = endpoint.subpath
sizeQueryParam = endpoint.sizeQueryParam
@@ -95,7 +95,7 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass
// Always an attachment, so an SVG can't render inline. Album assets have
// no originalFileName; Immich's own Content-Disposition carries it.
const named = asset.originalFileName ? asset : enrichFromHeaders(asset, data)
const position = share && findPositionInShare(share, asset.id)
const position = findPositionInShare(share, asset.id)
res.setHeader('Content-Disposition', attachmentDisposition(getFilename(named, servedSize, servedMimeFrom(subpath, data), position)))
}
headerList.forEach(header => {
+5 -5
View File
@@ -3,7 +3,7 @@ import { assetFetchUrl, authHeadersForAsset } from '../immich'
import { Response } from 'express-serve-static-core'
import archiver, { Archiver } from 'archiver'
import { resolveDownloadEndpoint, ImageEndpoint } from '../gallery/sizing'
import { AssetPosition, attachmentDisposition, enrichFromHeaders, findPositionInShare, getFilename, servedMimeFrom } from '../gallery/filename'
import { AssetPosition, attachmentDisposition, enrichFromHeaders, getFilename, servedMimeFrom, sharePositions } from '../gallery/filename'
/** Attempts to get response headers from Immich for one asset before giving up. */
const MAX_ATTEMPTS = 3
@@ -77,10 +77,12 @@ export async function downloadAssets (res: Response, share: SharedLink, assets:
// Headers and piping are deferred until the first asset has arrived, so a
// failure before then can still be answered with a normal error response.
let piped = false
// A multi-select download keeps each asset's position in the whole share
const positions = sharePositions(share)
for (const asset of assets) {
if (controller.signal.aborted) break
const fetched = await fetchOne(share, asset, controller.signal)
const fetched = await fetchOne(share, asset, positions.get(asset.id), controller.signal)
if (fetched === null) break // aborted while waiting on Immich
if ('failure' in fetched) {
if (!piped) {
@@ -196,7 +198,7 @@ function teardownArchive (archive: Archiver, res: Response) {
* Returns the fetched asset on success, a wrapped Failure on error, or null
* if the download was aborted before we got an answer.
*/
async function fetchOne (share: SharedLink, asset: Asset, signal: AbortSignal): Promise<FetchOutcome> {
async function fetchOne (share: SharedLink, asset: Asset, position: AssetPosition | undefined, signal: AbortSignal): Promise<FetchOutcome> {
if (signal.aborted) return null
const endpoint = resolveDownloadEndpoint(asset, share.allowDownload !== false)
@@ -213,8 +215,6 @@ async function fetchOne (share: SharedLink, asset: Asset, signal: AbortSignal):
// so recover them from the headers we already fetched - no extra calls.
const namedAsset = asset.originalFileName ? asset : enrichFromHeaders(asset, fetched.response)
// A multi-select download keeps each asset's position in the whole share
const position = findPositionInShare(share, asset.id)
return { response: fetched.response, asset: namedAsset, endpoint, servedMime: servedMimeFrom(endpoint.subpath, fetched.response), url, position }
}