From bf038444566ab948a814c7a11b3ef4eb0703c915 Mon Sep 17 00:00:00 2001 From: jszuminski Date: Tue, 24 Sep 2024 13:58:19 +0200 Subject: [PATCH] Made upload size configurable. --- lib/envs.js | 1 + lib/schemas/config.js | 7 +++++++ lib/server/server.js | 36 ++++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lib/envs.js b/lib/envs.js index 885d7f07..3f63d14c 100644 --- a/lib/envs.js +++ b/lib/envs.js @@ -207,6 +207,7 @@ export const Config = z.object({ OTHER_NO_LOGO: v.boolean(), OTHER_HARD_RESET_PAGE: v.boolean(), OTHER_BROWSER_SHELL_MODE: v.boolean(), + OTHER_UPLOAD_SIZE_LIMIT: v.positiveNum(), // debugger DEBUG_ENABLE: v.boolean(), diff --git a/lib/schemas/config.js b/lib/schemas/config.js index 50600dfd..aa2d03b7 100644 --- a/lib/schemas/config.js +++ b/lib/schemas/config.js @@ -643,6 +643,13 @@ export const defaultConfig = { type: 'boolean', envLink: 'OTHER_BROWSER_SHELL_MODE', description: 'Decides if the browser runs in the shell mode.' + }, + uploadSizeLimit: { + value: 50 * 1024 * 1024, + type: 'number', + envLink: 'OTHER_UPLOAD_SIZE_LIMIT', + description: + 'The maximum size of the uploaded file in bytes. The default is 50 MB (50 * 1024 * 1024 bytes).' } }, debug: { diff --git a/lib/server/server.js b/lib/server/server.js index 45c03b86..3dbdc540 100644 --- a/lib/server/server.js +++ b/lib/server/server.js @@ -25,6 +25,7 @@ import errorHandler from './error.js'; import rateLimit from './rate_limit.js'; import { log, logWithStack } from '../logger.js'; import { __dirname } from '../utils.js'; +import { getOptions } from '../config.js'; import vSwitchRoute from './routes/change_hc_version.js'; import exportRoutes from './routes/export.js'; @@ -45,22 +46,6 @@ app.disable('x-powered-by'); // Enable CORS support app.use(cors()); -// Enable parsing of form data (files) with Multer package -const storage = multer.memoryStorage(); -const upload = multer({ - storage, - limits: { - fieldSize: 50 * 1024 * 1024 - } -}); - -// Enable body parser -app.use(express.json({ limit: 50 * 1024 * 1024 })); -app.use(express.urlencoded({ extended: true, limit: 50 * 1024 * 1024 })); - -// Use only non-file multipart form fields -app.use(upload.none()); - /** * Attach error handlers to the server. * @@ -93,6 +78,25 @@ const attachServerErrorHandlers = (server) => { * and started. */ export const startServer = async (serverConfig) => { + const uploadSizeLimit = + getOptions()?.other?.uploadSizeLimit || 50 * 1024 * 1024; + + // Enable parsing of form data (files) with Multer package + const storage = multer.memoryStorage(); + const upload = multer({ + storage, + limits: { + fieldSize: uploadSizeLimit + } + }); + + // Enable body parser + app.use(express.json({ limit: uploadSizeLimit })); + app.use(express.urlencoded({ extended: true, limit: uploadSizeLimit })); + + // Use only non-file multipart form fields + app.use(upload.none()); + try { // Stop if not enabled if (!serverConfig.enable) {