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 = {} 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