Simplify code for readability

This commit is contained in:
Alan Grainger 2024-11-12 21:01:39 +01:00
parent d5400c9a21
commit aeb27ef971
2 changed files with 62 additions and 46 deletions

View File

@ -44,57 +44,73 @@ class Immich {
* 404 - any other failed request. Check console.log for details. * 404 - any other failed request. Check console.log for details.
*/ */
async handleShareRequest (request: IncomingShareRequest, res: Response) { async handleShareRequest (request: IncomingShareRequest, res: Response) {
// Add the headers configured in config.json (most likely `cache-control`)
addResponseHeaders(res) addResponseHeaders(res)
// Check that the key is a valid format // Check that the key is a valid format
if (!immich.isKey(request.key)) { if (!immich.isKey(request.key)) {
log('Invalid share key ' + request.key) log('Invalid share key ' + request.key)
res.status(404).send() res.status(404).send()
} else { return
// Get information about the shared link via Immich API }
const sharedLinkRes = await immich.getShareByKey(request.key, request.password)
if (!sharedLinkRes.valid) { // Get information about the shared link via Immich API
// This isn't a valid request - check the console for more information const sharedLinkRes = await immich.getShareByKey(request.key, request.password)
res.status(404).send() if (!sharedLinkRes.valid) {
} else if (sharedLinkRes.passwordRequired && request.password) { // This isn't a valid request - check the console for more information
// A password is required, but the visitor-provided one doesn't match res.status(404).send()
log('Invalid password for key ' + request.key) return
res.status(401).send() }
} else if (sharedLinkRes.passwordRequired) {
// Password required - show the visitor the password page // A password is required, but the visitor-provided one doesn't match
// `req.params.key` is already sanitised at this point, but it never hurts to be explicit if (sharedLinkRes.passwordRequired && request.password) {
const key = request.key.replace(/[^\w-]/g, '') log('Invalid password for key ' + request.key)
res.render('password', { res.status(401).send()
key, return
lgConfig: render.lgConfig }
})
} else if (sharedLinkRes.link) { // Password required - show the visitor the password page
// Valid shared link if (sharedLinkRes.passwordRequired) {
const link = sharedLinkRes.link // `request.key` is already sanitised at this point, but it never hurts to be explicit
if (!link.assets.length) { const key = request.key.replace(/[^\w-]/g, '')
// Immich didn't return any assets for this link (empty array) res.render('password', {
log('No assets for key ' + request.key) key,
res.status(404).send() lgConfig: render.lgConfig
} else if (link.assets.length === 1) { })
// This is an individual item (not a gallery) return
log('Serving link ' + request.key) }
const asset = link.assets[0]
if (asset.type === AssetType.image && !getConfigOption('ipp.singleImageGallery')) { if (!sharedLinkRes.link) {
// For photos, output the image directly unless configured to show a gallery log('Unknown error with key ' + request.key)
await render.assetBuffer(request, res, link.assets[0], ImageSize.preview) res.status(404).send()
} else { return
// Show a gallery page }
const openItem = getConfigOption('ipp.singleItemAutoOpen', true) ? 1 : 0
await render.gallery(res, link, openItem) // Make sure there are some photo/video assets for this link
} const link = sharedLinkRes.link
} else { if (!link.assets.length) {
// Multiple images - render as a gallery log('No assets for key ' + request.key)
log('Serving link ' + request.key) res.status(404).send()
await render.gallery(res, link) return
} }
// Everything is ok - output the link page
if (link.assets.length === 1) {
// This is an individual item (not a gallery)
log('Serving link ' + request.key)
const asset = link.assets[0]
if (asset.type === AssetType.image && !getConfigOption('ipp.singleImageGallery')) {
// For photos, output the image directly unless configured to show a gallery
await render.assetBuffer(request, res, link.assets[0], ImageSize.preview)
} else { } else {
log('Unknown error with key ' + request.key) // Show a gallery page
res.status(404).send() const openItem = getConfigOption('ipp.singleItemAutoOpen', true) ? 1 : 0
await render.gallery(res, link, openItem)
} }
} else {
// Multiple images - render as a gallery
log('Serving link ' + request.key)
await render.gallery(res, link)
} }
} }

View File

@ -17,7 +17,7 @@ app.use(express.json())
app.use(express.static('public', { setHeaders: addResponseHeaders })) app.use(express.static('public', { setHeaders: addResponseHeaders }))
/* /*
* [ROUTE] An incoming request for a shared link * [ROUTE] This is the request for a shared link
*/ */
app.get('/share/:key', async (req, res) => { app.get('/share/:key', async (req, res) => {
await immich.handleShareRequest({ await immich.handleShareRequest({
@ -36,7 +36,7 @@ app.post('/unlock', async (req, res) => {
}) })
/* /*
* [ROUTE] Output the buffer data for a photo or video * [ROUTE] This is the direct link to a photo or video asset
*/ */
app.get('/:type(photo|video)/:key/:id/:size?', async (req, res) => { app.get('/:type(photo|video)/:key/:id/:size?', async (req, res) => {
// Add the headers configured in config.json (most likely `cache-control`) // Add the headers configured in config.json (most likely `cache-control`)