62 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-10-29 10:29:14 +01:00
import immich from './immich'
import { Response } from 'express-serve-static-core'
2024-10-29 15:07:54 +01:00
import { Asset, AssetType, ImageSize, SharedLink } from './types'
2024-10-29 10:29:14 +01:00
import dayjs from 'dayjs'
class Render {
async assetBuffer (res: Response, asset: Asset, size?: ImageSize) {
const data = await immich.getAssetBuffer(asset, size)
if (data) {
for (const header of ['content-type', 'content-length']) {
res.set(header, data.headers[header])
}
console.log(`${dayjs().format()} Serving asset ${asset.id}`)
res.send(Buffer.from(await data.arrayBuffer()))
} else {
res.status(404).send()
}
}
2024-10-30 12:45:23 +01:00
/**
* Render a gallery page for a given SharedLink, using EJS and lightGallery.
*
* @param res - ExpressJS Response
* @param share - Immich `shared-link` containing the assets to show in the gallery
* @param [openItem] - Immediately open a lightbox to the Nth item when the gallery loads
*/
2024-10-29 15:07:54 +01:00
async gallery (res: Response, share: SharedLink, openItem?: number) {
2024-10-29 10:29:14 +01:00
const items = []
2024-10-29 15:07:54 +01:00
for (const asset of share.assets) {
2024-10-29 10:29:14 +01:00
let video
if (asset.type === AssetType.video) {
// Populate the data-video property
video = JSON.stringify({
source: [
{
2024-10-29 15:07:54 +01:00
src: immich.videoUrl(share.key, asset.id),
2024-10-29 10:29:14 +01:00
type: await immich.getContentType(asset)
}
],
attributes: {
preload: false,
controls: true
}
})
}
items.push({
2024-10-29 15:07:54 +01:00
originalUrl: immich.photoUrl(share.key, asset.id),
thumbnailUrl: immich.photoUrl(share.key, asset.id, ImageSize.thumbnail),
2024-10-29 10:29:14 +01:00
video
})
}
res.render('gallery', {
items,
openItem
})
}
}
const render = new Render()
export default render