Refactor to a response function (no WET code)

This commit is contained in:
Alan Grainger
2025-03-07 13:00:41 +08:00
parent 01c3236e0f
commit 9e176e816a
5 changed files with 43 additions and 31 deletions
+5 -10
View File
@@ -3,6 +3,7 @@ import dayjs from 'dayjs'
import { addResponseHeaders, getConfigOption, log } from './functions'
import render from './render'
import { Response } from 'express-serve-static-core'
import { respondToInvalidRequest } from './invalidRequestHandler'
class Immich {
/**
@@ -49,10 +50,7 @@ class Immich {
// Check that the key is a valid format
if (!immich.isKey(request.key)) {
log('Invalid share key ' + request.key)
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
@@ -60,10 +58,7 @@ class Immich {
const sharedLinkRes = await immich.getShareByKey(request.key, request.password)
if (!sharedLinkRes.valid) {
// This isn't a valid request - check the console for more information
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
@@ -96,7 +91,7 @@ class Immich {
if (!sharedLinkRes.link) {
log('Unknown error with key ' + request.key)
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
@@ -104,7 +99,7 @@ class Immich {
const link = sharedLinkRes.link
if (!link.assets.length) {
log('No assets for key ' + request.key)
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
+5 -16
View File
@@ -10,6 +10,7 @@ import { Request, Response, NextFunction } from 'express-serve-static-core'
import { AssetType, ImageSize } from './types'
import { log, toString, addResponseHeaders, getConfigOption } from './functions'
import { decrypt, encrypt } from './encrypt'
import { respondToInvalidRequest } from './invalidRequestHandler'
// Extend the Request type with a `password` property
declare module 'express-serve-static-core' {
@@ -107,20 +108,14 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, async (req, r
// Check for valid key and ID
if (!immich.isKey(req.params.key) || !immich.isId(req.params.id)) {
log('Invalid key or ID for ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
// Validate the size parameter
if (req.params.size && !Object.values(ImageSize).includes(req.params.size as ImageSize)) {
log('Invalid size parameter ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
return
}
@@ -141,10 +136,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, async (req, r
}
} else {
log('No asset found for ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
}
})
@@ -169,10 +161,7 @@ if (getConfigOption('ipp.showHomePage', true)) {
*/
app.get('*', (req, res) => {
log('Invalid route ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
})
// Start the ExpressJS server
+30
View File
@@ -0,0 +1,30 @@
/*
This function is in its own file so that *if desired* someone can replace the entire
function with their own custom one, by replacing the invalidRequestHandler.js file
through a Docker volume mount.
*/
import { Response } from 'express-serve-static-core'
import { getConfigOption } from './functions'
export function respondToInvalidRequest (res: Response, defaultResponse: number | string | null) {
let method = getConfigOption('ipp.customInvalidResponse', false)
if (method === false) {
// No custom method specified, use the default
method = defaultResponse
}
if (typeof method === 'number') {
// Respond with an HTTP status code
res.status(method).send()
} else if (method === null) {
// Drop the connection without responding
res.destroy()
} else if (typeof method === 'string' && method.startsWith('http')) {
// Redirect to another URL
res.redirect(method)
} else {
// Fallback to 404
res.status(404).send()
}
}
+2 -4
View File
@@ -3,6 +3,7 @@ import { Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink } from './types'
import { getConfigOption } from './functions'
import archiver from 'archiver'
import { respondToInvalidRequest } from './invalidRequestHandler'
class Render {
lgConfig
@@ -74,10 +75,7 @@ class Render {
)
res.end()
} else {
if (getConfigOption('ipp.stealthMode', true)) {
res.destroy();
}
res.status(404).send()
respondToInvalidRequest(res, 404)
}
}