Add video player

This commit is contained in:
Alan Grainger
2024-10-29 10:29:14 +01:00
parent 7be2508eac
commit 65eeba2df6
7 changed files with 157 additions and 54 deletions
+26 -33
View File
@@ -1,9 +1,9 @@
import express from 'express'
import api from './immich'
import immich from './immich'
import render from './render'
import dayjs from 'dayjs'
import { AssetType, ImageSize } from './types'
import { Request, Response } from 'express-serve-static-core'
import { Request } from 'express-serve-static-core'
require('dotenv').config()
@@ -11,22 +11,6 @@ const app = express()
app.set('view engine', 'ejs')
app.use(express.static('public'))
async function serveImage (res: Response, id: string, size?: ImageSize) {
const image = await api.getAssetBuffer({
id,
type: AssetType.image
}, size)
if (image) {
for (const header of ['content-type', 'content-length']) {
res.set(header, image.headers[header])
}
console.log(`${dayjs().format()} Serving image ${id}`)
res.send(Buffer.from(await image.arrayBuffer()))
} else {
res.status(404).send()
}
}
const getSize = (req: Request) => {
return req?.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
}
@@ -36,33 +20,42 @@ app.get('/share/:key', async (req, res) => {
// Invalid characters in the incoming URL
res.status(404).send()
} else {
const share = await api.getShareByKey(req.params.key)
const share = await immich.getShareByKey(req.params.key)
if (!share || !share.assets.length) {
res.status(404).send()
} else if (share.assets.length === 1) {
// This is an individual item (not a gallery)
await serveImage(res, share.assets[0].id, getSize(req))
const asset = share.assets[0]
if (asset.type === AssetType.image) {
// Output the image directly
await render.assetBuffer(res, share.assets[0], getSize(req))
} else if (asset.type === AssetType.video) {
// Show the video as a web player
await render.gallery(res, share.assets, 1)
}
} else {
// Multiple images - render as a gallery
res.render('gallery', {
photos: share.assets.map(photo => {
return {
originalUrl: immich.imageUrl(photo.id),
thumbnailUrl: immich.imageUrl(photo.id, ImageSize.thumbnail)
}
})
})
await render.gallery(res, share.assets, 1)
}
}
})
app.get('/photo/:id', (req, res) => {
if (req.params.id.match(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/)) {
// Check for photo
serveImage(res, req.params.id, getSize(req)).then()
} else {
// Output the buffer data for an photo or video
app.get('/:type(photo|video)/:id', (req, res) => {
if (!immich.isId(req.params.id)) {
// Invalid characters in the incoming URL
res.status(404).send()
return
}
const asset = {
id: req.params.id,
type: req.params.type === 'video' ? AssetType.video : AssetType.image
}
switch (req.params.type) {
case 'photo':
case 'video':
render.assetBuffer(res, asset, getSize(req)).then()
break
}
})