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
+1 -1
View File
@@ -10,7 +10,7 @@
"showGalleryTitle": false, "showGalleryTitle": false,
"allowDownloadAll": false, "allowDownloadAll": false,
"showHomePage": true, "showHomePage": true,
"stealthMode": false "customInvalidResponse": false
}, },
"lightGallery": { "lightGallery": {
"controls": true, "controls": true,
+5 -10
View File
@@ -3,6 +3,7 @@ import dayjs from 'dayjs'
import { addResponseHeaders, getConfigOption, log } from './functions' import { addResponseHeaders, getConfigOption, log } from './functions'
import render from './render' import render from './render'
import { Response } from 'express-serve-static-core' import { Response } from 'express-serve-static-core'
import { respondToInvalidRequest } from './invalidRequestHandler'
class Immich { class Immich {
/** /**
@@ -49,10 +50,7 @@ class Immich {
// 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)
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
return return
} }
@@ -60,10 +58,7 @@ class Immich {
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
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
return return
} }
@@ -96,7 +91,7 @@ class Immich {
if (!sharedLinkRes.link) { if (!sharedLinkRes.link) {
log('Unknown error with key ' + request.key) log('Unknown error with key ' + request.key)
res.status(404).send() respondToInvalidRequest(res, 404)
return return
} }
@@ -104,7 +99,7 @@ class Immich {
const link = sharedLinkRes.link const link = sharedLinkRes.link
if (!link.assets.length) { if (!link.assets.length) {
log('No assets for key ' + request.key) log('No assets for key ' + request.key)
res.status(404).send() respondToInvalidRequest(res, 404)
return return
} }
+5 -16
View File
@@ -10,6 +10,7 @@ import { Request, Response, NextFunction } from 'express-serve-static-core'
import { AssetType, ImageSize } from './types' import { AssetType, ImageSize } from './types'
import { log, toString, addResponseHeaders, getConfigOption } from './functions' import { log, toString, addResponseHeaders, getConfigOption } from './functions'
import { decrypt, encrypt } from './encrypt' import { decrypt, encrypt } from './encrypt'
import { respondToInvalidRequest } from './invalidRequestHandler'
// Extend the Request type with a `password` property // Extend the Request type with a `password` property
declare module 'express-serve-static-core' { 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 // Check for valid key and ID
if (!immich.isKey(req.params.key) || !immich.isId(req.params.id)) { if (!immich.isKey(req.params.key) || !immich.isId(req.params.id)) {
log('Invalid key or ID for ' + req.path) log('Invalid key or ID for ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
return return
} }
// Validate the size parameter // Validate the size parameter
if (req.params.size && !Object.values(ImageSize).includes(req.params.size as ImageSize)) { if (req.params.size && !Object.values(ImageSize).includes(req.params.size as ImageSize)) {
log('Invalid size parameter ' + req.path) log('Invalid size parameter ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
return return
} }
@@ -141,10 +136,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, async (req, r
} }
} else { } else {
log('No asset found for ' + req.path) log('No asset found for ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
} }
}) })
@@ -169,10 +161,7 @@ if (getConfigOption('ipp.showHomePage', true)) {
*/ */
app.get('*', (req, res) => { app.get('*', (req, res) => {
log('Invalid route ' + req.path) log('Invalid route ' + req.path)
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
}) })
// Start the ExpressJS server // 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 { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink } from './types'
import { getConfigOption } from './functions' import { getConfigOption } from './functions'
import archiver from 'archiver' import archiver from 'archiver'
import { respondToInvalidRequest } from './invalidRequestHandler'
class Render { class Render {
lgConfig lgConfig
@@ -74,10 +75,7 @@ class Render {
) )
res.end() res.end()
} else { } else {
if (getConfigOption('ipp.stealthMode', true)) { respondToInvalidRequest(res, 404)
res.destroy();
}
res.status(404).send()
} }
} }