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,37 +44,58 @@ 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 // Get information about the shared link via Immich API
const sharedLinkRes = await immich.getShareByKey(request.key, request.password) const sharedLinkRes = await immich.getShareByKey(request.key, request.password)
if (!sharedLinkRes.valid) { if (!sharedLinkRes.valid) {
// This isn't a valid request - check the console for more information // This isn't a valid request - check the console for more information
res.status(404).send() res.status(404).send()
} else if (sharedLinkRes.passwordRequired && request.password) { return
}
// A password is required, but the visitor-provided one doesn't match // A password is required, but the visitor-provided one doesn't match
if (sharedLinkRes.passwordRequired && request.password) {
log('Invalid password for key ' + request.key) log('Invalid password for key ' + request.key)
res.status(401).send() res.status(401).send()
} else if (sharedLinkRes.passwordRequired) { return
}
// Password required - show the visitor the password page // Password required - show the visitor the password page
// `req.params.key` is already sanitised at this point, but it never hurts to be explicit if (sharedLinkRes.passwordRequired) {
// `request.key` is already sanitised at this point, but it never hurts to be explicit
const key = request.key.replace(/[^\w-]/g, '') const key = request.key.replace(/[^\w-]/g, '')
res.render('password', { res.render('password', {
key, key,
lgConfig: render.lgConfig lgConfig: render.lgConfig
}) })
} else if (sharedLinkRes.link) { return
// Valid shared link }
if (!sharedLinkRes.link) {
log('Unknown error with key ' + request.key)
res.status(404).send()
return
}
// Make sure there are some photo/video assets for this link
const link = sharedLinkRes.link const link = sharedLinkRes.link
if (!link.assets.length) { if (!link.assets.length) {
// Immich didn't return any assets for this link (empty array)
log('No assets for key ' + request.key) log('No assets for key ' + request.key)
res.status(404).send() res.status(404).send()
} else if (link.assets.length === 1) { return
}
// Everything is ok - output the link page
if (link.assets.length === 1) {
// This is an individual item (not a gallery) // This is an individual item (not a gallery)
log('Serving link ' + request.key) log('Serving link ' + request.key)
const asset = link.assets[0] const asset = link.assets[0]
@ -91,11 +112,6 @@ class Immich {
log('Serving link ' + request.key) log('Serving link ' + request.key)
await render.gallery(res, link) await render.gallery(res, link)
} }
} else {
log('Unknown error with key ' + request.key)
res.status(404).send()
}
}
} }
/** /**

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`)