139 lines
5.9 KiB
TypeScript
139 lines
5.9 KiB
TypeScript
import { abortOnClose, Asset, log, readableFromWeb, respondToInvalidRequest, SharedLink } from '@ipp/core'
|
|
import {
|
|
assetFetchUrl,
|
|
authHeadersForAsset,
|
|
fetchAssetDetail,
|
|
validateImageSize
|
|
} from '../immich'
|
|
import { Response } from 'express-serve-static-core'
|
|
import { ImageSize, IncomingShareRequest } from '../types'
|
|
import { attachmentDisposition, enrichFromHeaders, findPositionInShare, getFilename, servedMimeFrom } from '../gallery/filename'
|
|
import { isVideoAsset, resolveDownloadEndpoint, resolveImageEndpoint } from '../gallery/sizing'
|
|
import { pipeline } from 'stream/promises'
|
|
|
|
/**
|
|
* Stream an asset from Immich back to the client.
|
|
*
|
|
* Errors from Immich are always reported to the client as 404 - see
|
|
* invalidRequestHandler. Upstream status codes are never surfaced. Trashed
|
|
* 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 | undefined, share: SharedLink, forceVideoPlayback = false) {
|
|
// A cancelled download must not leave Immich streaming #288
|
|
const upstream = abortOnClose(res)
|
|
|
|
const headerList = ['content-type', 'content-length', 'last-modified', 'etag']
|
|
const fetchHeaders: Record<string, string> = {}
|
|
let subpath: string
|
|
let sizeQueryParam: string | undefined
|
|
let attachment = false
|
|
let servedSize: ImageSize | undefined
|
|
|
|
const requested = validateImageSize(size)
|
|
const useVideoPlayback = forceVideoPlayback || (isVideoAsset(asset) && requested === ImageSize.original && share.allowDownload === false)
|
|
|
|
if (useVideoPlayback) {
|
|
subpath = '/video/playback'
|
|
attachment = requested === ImageSize.original
|
|
servedSize = ImageSize.original
|
|
res.setHeader('accept-ranges', 'bytes')
|
|
// Only chunk when the client sent a Range header. A browser <video>
|
|
// element does, so playback still streams in 2.5 MB chunks. Clients
|
|
// that don't (wget, right-click "Save As", link unfurlers) get the
|
|
// full file with 200 OK; otherwise they'd save a truncated 2.5 MB
|
|
// partial response as the whole video.
|
|
if (req.range) {
|
|
const range = req.range.replace(/bytes=/, '').split('-')
|
|
const start = parseInt(range[0], 10) || 0
|
|
const end = parseInt(range[1], 10) || start + 2499999
|
|
fetchHeaders.range = `bytes=${start}-${end}`
|
|
headerList.push('cache-control', 'content-range')
|
|
res.status(206) // Partial Content
|
|
}
|
|
} else {
|
|
// Album "grid" items arrive without originalMimeType. The fullsize tier
|
|
// needs it to pick /original (web formats) vs ?size=fullsize (RAW/HEIF), so
|
|
// fetch the asset detail on demand (cached) before resolving.
|
|
if (requested === ImageSize.fullsize && !asset.originalMimeType) {
|
|
const detail = await fetchAssetDetail(asset)
|
|
if (detail?.originalMimeType) asset = { ...asset, originalMimeType: detail.originalMimeType }
|
|
}
|
|
const endpoint = requested === ImageSize.original
|
|
? resolveDownloadEndpoint(asset, share.allowDownload !== false)
|
|
: resolveImageEndpoint(requested, asset)
|
|
subpath = endpoint.subpath
|
|
sizeQueryParam = endpoint.sizeQueryParam
|
|
attachment = endpoint.attachment
|
|
servedSize = endpoint.servedSize
|
|
}
|
|
|
|
const url = assetFetchUrl(asset, subpath, sizeQueryParam)
|
|
const reqHeaders = await authHeadersForAsset(asset)
|
|
let data: globalThis.Response
|
|
try {
|
|
data = await fetch(url, { headers: { ...fetchHeaders, ...reqHeaders }, signal: upstream })
|
|
} catch (e) {
|
|
if (upstream.aborted) return // visitor left before Immich answered
|
|
throw e
|
|
}
|
|
|
|
if (data.status < 200 || data.status >= 300) {
|
|
let immichMessage = ''
|
|
try {
|
|
const json = await data.json()
|
|
if (json.message) immichMessage = '\nResponse from Immich: ' + json.message
|
|
} catch (e) { }
|
|
respondToInvalidRequest(res, 404, 'Failed response from Immich for asset ' + asset.id + ' on this URL:\n' + url + immichMessage)
|
|
return
|
|
}
|
|
|
|
// An original can be any format Immich accepts, so never let a browser sniff it
|
|
res.setHeader('X-Content-Type-Options', 'nosniff')
|
|
if (attachment) {
|
|
res.setHeader('X-Accel-Buffering', 'no')
|
|
// 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 = findPositionInShare(share, asset.id)
|
|
res.setHeader('Content-Disposition', attachmentDisposition(getFilename(named, servedSize, servedMimeFrom(subpath, data), position)))
|
|
}
|
|
headerList.forEach(header => {
|
|
const value = data.headers.get(header)
|
|
if (value) res.setHeader(header, value)
|
|
})
|
|
|
|
// Express routes HEAD through the GET handler and Node silently drops the
|
|
// body writes, so without this we'd read the whole file from Immich for
|
|
// nothing. The headers above are all a HEAD needs.
|
|
if (req.req.method === 'HEAD' || !data.body) {
|
|
await data.body?.cancel()
|
|
res.end()
|
|
return
|
|
}
|
|
|
|
/*
|
|
pipeline (rather than a WritableStream sink around res.write) honours
|
|
backpressure from `res`: a LAN-speed read from Immich can't pile up in
|
|
memory ahead of a slow visitor #288.
|
|
*/
|
|
try {
|
|
await pipeline(readableFromWeb(data.body), res)
|
|
} catch (e) {
|
|
if (!isClientAbort(e)) {
|
|
log.warn(`Stream from Immich failed for asset ${asset.id}: ${e instanceof Error ? e.message : String(e)}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* True when a pipeline error means the visitor went away: pipeline saw `res`
|
|
* close early (or found it already closed), or our abort signal, fired from
|
|
* that same close, reached the fetch body first.
|
|
*/
|
|
function isClientAbort (e: unknown): boolean {
|
|
if (!(e instanceof Error)) return false
|
|
const code = (e as NodeJS.ErrnoException).code
|
|
return code === 'ERR_STREAM_PREMATURE_CLOSE' || code === 'ERR_STREAM_UNABLE_TO_PIPE' || e.name === 'AbortError'
|
|
}
|