92 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-10-28 09:56:11 +01:00
import express from 'express'
2024-10-28 20:47:14 +01:00
import immich from './immich'
2024-10-29 10:29:14 +01:00
import render from './render'
2024-10-28 09:56:11 +01:00
import dayjs from 'dayjs'
2024-10-28 20:47:14 +01:00
import { AssetType, ImageSize } from './types'
2024-10-29 10:29:14 +01:00
import { Request } from 'express-serve-static-core'
2024-10-28 20:47:14 +01:00
require('dotenv').config()
2024-10-28 09:08:16 +01:00
const app = express()
2024-10-30 15:43:07 +01:00
// Add the EJS view engine, to render the gallery page
2024-10-28 20:47:14 +01:00
app.set('view engine', 'ejs')
2024-10-30 15:43:07 +01:00
// Serve static assets from the /public folder
2024-10-28 20:47:14 +01:00
app.use(express.static('public'))
2024-10-28 09:08:16 +01:00
2024-10-30 15:40:42 +01:00
// An incoming request for a shared link
2024-10-28 09:08:16 +01:00
app.get('/share/:key', async (req, res) => {
2024-10-29 16:15:34 +01:00
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE)
2024-10-29 15:07:54 +01:00
if (!immich.isKey(req.params.key)) {
2024-10-30 14:28:29 +01:00
log('Invalid share key ' + req.params.key)
2024-10-28 09:08:16 +01:00
res.status(404).send()
} else {
2024-10-29 15:07:54 +01:00
const sharedLink = await immich.getShareByKey(req.params.key)
2024-10-30 14:28:29 +01:00
if (!sharedLink) {
log('Unknown share key ' + req.params.key)
res.status(404).send()
} else if (!sharedLink.assets.length) {
log('No assets for key ' + req.params.key)
2024-10-28 20:47:14 +01:00
res.status(404).send()
2024-10-29 15:07:54 +01:00
} else if (sharedLink.assets.length === 1) {
2024-10-28 09:08:16 +01:00
// This is an individual item (not a gallery)
2024-10-30 14:38:08 +01:00
log('Serving link ' + req.params.key)
2024-10-29 15:07:54 +01:00
const asset = sharedLink.assets[0]
2024-10-29 10:29:14 +01:00
if (asset.type === AssetType.image) {
2024-10-29 11:48:16 +01:00
// For photos, output the image directly
2024-10-29 15:07:54 +01:00
await render.assetBuffer(res, sharedLink.assets[0], getSize(req))
2024-10-29 10:29:14 +01:00
} else if (asset.type === AssetType.video) {
2024-10-29 11:48:16 +01:00
// For videos, show the video as a web player
2024-10-29 15:07:54 +01:00
await render.gallery(res, sharedLink, 1)
2024-10-29 10:29:14 +01:00
}
2024-10-28 09:08:16 +01:00
} else {
2024-10-28 20:47:14 +01:00
// Multiple images - render as a gallery
2024-10-30 14:38:08 +01:00
log('Serving link ' + req.params.key)
2024-10-29 15:07:54 +01:00
await render.gallery(res, sharedLink)
2024-10-28 09:08:16 +01:00
}
}
})
2024-10-30 15:40:42 +01:00
// Output the buffer data for a photo or video
2024-10-29 15:07:54 +01:00
app.get('/:type(photo|video)/:key/:id', async (req, res) => {
2024-10-29 16:15:34 +01:00
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE)
2024-10-29 15:07:54 +01:00
// Check for valid key and ID
if (immich.isKey(req.params.key) && immich.isId(req.params.id)) {
// Check if the key is a valid share link
const sharedLink = await immich.getShareByKey(req.params.key)
if (sharedLink?.assets.length) {
// Check that the requested asset exists in this share
const asset = sharedLink.assets.find(x => x.id === req.params.id)
if (asset) {
asset.type = req.params.type === 'video' ? AssetType.video : AssetType.image
render.assetBuffer(res, asset, getSize(req)).then()
return
}
}
2024-10-29 10:29:14 +01:00
}
2024-10-30 14:28:29 +01:00
log('No asset found for ' + req.path)
2024-10-29 15:07:54 +01:00
res.status(404).send()
2024-10-29 11:48:16 +01:00
})
2024-10-30 15:40:42 +01:00
// Send a 404 for all other routes
2024-10-30 14:28:29 +01:00
app.get('*', (req, res) => {
log('Invalid route ' + req.path)
2024-10-29 11:48:16 +01:00
res.status(404).send()
2024-10-28 20:47:14 +01:00
})
2024-10-30 15:40:42 +01:00
/**
* Sanitise the data for an incoming query string `size` parameter
* e.g. https://example.com/share/abc...xyz?size=thumbnail
*/
const getSize = (req: Request) => {
return req?.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
}
/**
* Output a console.log message with timestamp
*/
export const log = (message: string) => console.log(dayjs().format() + ' ' + message)
2024-10-28 09:08:16 +01:00
app.listen(3000, () => {
console.log(dayjs().format() + ' Server started')
})