Load assets in parallel

When loading the assets in the album, we do so sequentially which is not ideal, particularly on larger albums. This PR loads all assets in parallel instead.
This commit is contained in:
Zaida-3dO
2025-11-03 19:01:40 +00:00
committed by GitHub
parent 35ac017ee4
commit 675a6730e1
+40 -40
View File
@@ -105,53 +105,53 @@ class Render {
* @param [openItem] - Immediately open a lightbox to the Nth item when the gallery loads * @param [openItem] - Immediately open a lightbox to the Nth item when the gallery loads
*/ */
async gallery (res: Response, share: SharedLink, openItem?: number) { async gallery (res: Response, share: SharedLink, openItem?: number) {
const items = []
// publicBaseUrl is used for the og:image, which requires a fully qualified URL. // 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 // 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) const publicBaseUrl = process.env.PUBLIC_BASE_URL || res.req.headers.publicBaseUrl || (res.req.protocol + '://' + res.req.headers.host)
for (const asset of share.assets) { // Use .map to generate an array of promises, then await them all to load in parallel.
let video, downloadUrl const items = await Promise.all(share.assets.map(async (asset) => {
if (asset.type === AssetType.video) { let video, downloadUrl
// Populate the data-video property if (asset.type === AssetType.video) {
video = JSON.stringify({ // Populate the data-video property
source: [ video = JSON.stringify({
{ source: [
src: immich.videoUrl(share.key, asset.id), {
type: await immich.getVideoContentType(asset) src: immich.videoUrl(share.key, asset.id),
} type: await immich.getVideoContentType(asset)
], }
attributes: { ],
playsinline: 'playsinline', attributes: {
controls: 'controls' playsinline: 'playsinline',
} controls: 'controls'
}) }
} })
if (getConfigOption('ipp.downloadOriginalPhoto', true)) { }
// Add a download link for the original-size image, if configured in config.json if (getConfigOption('ipp.downloadOriginalPhoto', true)) {
downloadUrl = immich.photoUrl(share.key, asset.id, ImageSize.original) // Add a download link for the original-size image, if configured in config.json
} downloadUrl = immich.photoUrl(share.key, asset.id, ImageSize.original)
}
const thumbnailUrl = immich.photoUrl(share.key, asset.id, ImageSize.thumbnail) const thumbnailUrl = immich.photoUrl(share.key, asset.id, ImageSize.thumbnail)
const previewUrl = immich.photoUrl(share.key, asset.id, immich.getPreviewImageSize(asset)) const previewUrl = immich.photoUrl(share.key, asset.id, immich.getPreviewImageSize(asset))
const description = getConfigOption('ipp.showMetadata.description', false) && typeof asset?.exifInfo?.description === 'string' ? asset.exifInfo.description.replace(/'/g, ''') : '' const description = getConfigOption('ipp.showMetadata.description', false) && typeof asset?.exifInfo?.description === 'string' ? asset.exifInfo.description.replace(/'/g, ''') : ''
// Create the full HTML element source to pass to the gallery view // Create the full HTML element source to pass to the gallery view
const itemHtml = [ const itemHtml = [
video ? `<a data-video='${video}'` : `<a href="${previewUrl}"`, video ? `<a data-video='${video}'` : `<a href="${previewUrl}"`,
downloadUrl ? ` data-download-url="${downloadUrl}"` : '', downloadUrl ? ` data-download-url="${downloadUrl}"` : '',
description ? ` data-sub-html='<p>${description}</p>'` : '', description ? ` data-sub-html='<p>${description}</p>'` : '',
` data-download="${this.getFilename(asset)}"><img alt="" src="${thumbnailUrl}"/>`, ` data-download="${this.getFilename(asset)}"><img alt="" src="${thumbnailUrl}"/>`,
video ? '<div class="play-icon"></div>' : '', video ? '<div class="play-icon"></div>' : '',
'</a>' '</a>'
].join('') ].join('')
items.push({ return {
html: itemHtml, html: itemHtml,
thumbnailUrl thumbnailUrl
}) };
} }));
res.render('gallery', { res.render('gallery', {
items, items,
openItem, openItem,