import immich from './immich' import { Response } from 'express-serve-static-core' import { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink } from './types' import { canDownload, escapeHtml, getConfigOption, toString } from './functions' import archiver from 'archiver' import { respondToInvalidRequest } from './invalidRequestHandler' import { sanitize } from './includes/sanitize' import { h } from 'preact' import { renderPage } from './views/_render' import { Gallery, GalleryItem, GalleryProps } from './views/gallery' class Render { /** * Stream data from Immich back to the client */ async assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string) { // Get meta info regarding the asset const metaRes = await fetch(immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id), { key: asset.key })) const meta = await metaRes.json() // Make sure we should display this asset if (meta.isTrashed || meta.visibility === 'locked') { respondToInvalidRequest(res, 404, `Asset ${asset.id} is trashed or locked`) return } // Prepare the request const headerList = ['content-type', 'content-length', 'last-modified', 'etag'] size = immich.validateImageSize(size) let subpath, sizeQueryParam if (asset.type === AssetType.video) { subpath = '/video/playback' } else if (asset.type === AssetType.image) { // For images, there are three combinations of path + query string, depending on image size if (size === ImageSize.original && getConfigOption('ipp.downloadOriginalPhoto', true)) { subpath = '/original' } else if (size === ImageSize.preview || size === ImageSize.original) { // IPP is configured in config.json to send the preview size instead of the original size subpath = '/thumbnail' sizeQueryParam = 'preview' } else { subpath = '/' + size } } const headers = { range: '' } // For videos, request them in 2.5MB chunks rather than the entire video if (asset.type === AssetType.video) { const range = (req.range || '').replace(/bytes=/, '').split('-') const start = parseInt(range[0], 10) || 0 const end = parseInt(range[1], 10) || start + 2499999 headers.range = `bytes=${start}-${end}` headerList.push('cache-control', 'content-range') res.setHeader('accept-ranges', 'bytes') res.status(206) // Partial Content } // Request data from Immich const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + subpath, { [asset.keyType || 'key']: asset.key, size: sizeQueryParam, password: asset.password }) const data = await fetch(url, { headers }) // Add the filename for downloaded assets if (size === ImageSize.original && asset.originalFileName && getConfigOption('ipp.downloadOriginalPhoto', true)) { res.setHeader('Content-Disposition', `attachment; filename="${this.getFilename(asset)}"`) } // Return the response to the client if (data.status >= 200 && data.status < 300) { // Populate the whitelisted response headers headerList.forEach(header => { const value = data.headers.get(header) if (value) res.setHeader(header, value) }) // Return the Immich asset binary data await data.body?.pipeTo( new WritableStream({ write (chunk) { res.write(chunk) } }) ) res.end() } else { 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) } } /** * Render a gallery page for a given SharedLink. * * @param res - ExpressJS Response * @param share - Immich `shared-link` containing the assets to show in the gallery * @param [openItem] - Immediately open the lightbox to the Nth item when the gallery loads */ async gallery (res: Response, share: SharedLink, openItem?: number) { // publicBaseUrl is used for the og:image, which requires a fully qualified URL. // You can specify this in your docker-compose file, or send it dynamically as a `publicBaseUrl` header const publicBaseUrl = process.env.PUBLIC_BASE_URL || res.req.headers.publicBaseUrl || (res.req.protocol + '://' + res.req.headers.host) // Date grouping needs chronological order; sort newest-first when enabled // (overrides any album.order the upstream applied). const groupByDate = !!getConfigOption('ipp.gallery.groupByDate', false) if (groupByDate) { share.assets.sort((a, b) => (b.fileCreatedAt || '').localeCompare(a.fileCreatedAt || '')) } // Build structured items in parallel const items: GalleryItem[] = await Promise.all(share.assets.map(async (asset): Promise => { let videoData: string | undefined let downloadUrl: string | undefined if (asset.type === AssetType.video) { videoData = JSON.stringify({ source: [ { src: immich.videoUrl(share.key, asset.id), type: await immich.getVideoContentType(asset) } ], attributes: { playsinline: 'playsinline', controls: 'controls' } }) downloadUrl = immich.videoUrl(share.key, asset.id) } if (getConfigOption('ipp.downloadOriginalPhoto', true)) { downloadUrl = immich.photoUrl(share.key, asset.id, ImageSize.original) } const thumbnailUrl = immich.photoUrl(share.key, asset.id, ImageSize.thumbnail) const previewUrl = immich.photoUrl(share.key, asset.id, immich.getPreviewImageSize(asset)) const description = getConfigOption('ipp.showMetadata.description', false) && typeof asset?.exifInfo?.description === 'string' ? escapeHtml(asset.exifInfo.description) : '' let width = asset.exifInfo?.exifImageWidth let height = asset.exifInfo?.exifImageHeight const orientation = asset.exifInfo?.orientation if (orientation && ['5', '6', '7', '8'].includes(orientation) && width && height) { [width, height] = [height, width] } return { id: asset.id, type: asset.type, previewUrl, thumbnailUrl, downloadUrl, videoData, description: description || undefined, downloadFilename: this.getFilename(asset), width, height, thumbhash: asset.thumbhash, fileCreatedAt: asset.fileCreatedAt } })) const downloadAllowed = canDownload(share) const props: GalleryProps = { items, title: this.title(share), description: getConfigOption('ipp.gallery.showDescription', false) ? this.description(share) : '', publicBaseUrl: toString(publicBaseUrl), path: '/share/' + share.key, showDownload: downloadAllowed, showTitle: !!getConfigOption('ipp.gallery.showTitle', true), openItem, lightboxConfig: { // Show download button only if downloading is allowed AND configured. showDownload: downloadAllowed && !!getConfigOption('ipp.lightbox.showDownload', true), showArrows: !!getConfigOption('ipp.lightbox.showArrows', true), mobileArrows: !!getConfigOption('ipp.lightbox.mobileArrows', false) }, groupByDate } res.send(renderPage(h(Gallery, props))) } /** * Attempt to get a title from the link description or the album title */ title (share: SharedLink) { return share.description || share?.album?.albumName || 'Gallery' } /** * Get the Immich shared link description */ description (share: SharedLink) { return share?.album?.description || '' } /** * Download all assets in a share as a zip file */ async downloadAll (res: Response, share: SharedLink) { await this.downloadAssets(res, share, share.assets) } /** * Stream the given assets back as a zip file. Caller is responsible for * checking that every asset belongs to the share (security boundary). */ async downloadAssets (res: Response, share: SharedLink, assets: Asset[]) { const downloadOriginalAsset = getConfigOption('ipp.downloadOriginalPhoto', true) res.setHeader('Content-Type', 'application/zip') let filename = (sanitize(this.title(share)) || 'photos') + '.zip' filename = encodeURI(filename) res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${filename}`) const archive = archiver('zip', { zlib: { level: 6 } }) archive.pipe(res) await Promise.all(assets.map(async (asset) => { const endpoint = downloadOriginalAsset ? 'original' : 'thumbnail' const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + '/' + endpoint, { key: asset.key, password: asset.password, size: downloadOriginalAsset ? '' : 'preview' }) const data = await fetch(url) if (!data.ok) { console.warn(`Failed to fetch asset: ${asset.id}`) return } archive.append(Buffer.from(await data.arrayBuffer()), { name: this.getFilename(asset) }) })) await archive.finalize() archive.on('end', () => res.end()) } /** * Generate a filename for the downloaded asset based on the configuration option chosen */ getFilename (asset: Asset) { const extension = asset.originalFileName?.match(/(\.\w+)$/)?.[1] || '' switch (getConfigOption('ipp.downloadedFilename')) { case 1: // Immich's ID number for this asset return asset.id + extension case 2: // A sanitised version of the ID number return 'img_' + asset.id.slice(0, 8) + extension default: // By default, it will choose the asset's original filename return asset.originalFileName || (asset.id + extension) } } } const render = new Render() export default render