From ffcee4e802077888a572045b68aa86871df00947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl?= <15849319+abcdan@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:34:01 +0200 Subject: [PATCH] fix --- index.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index e3e8926..eb48791 100644 --- a/index.js +++ b/index.js @@ -11,14 +11,32 @@ app.set("view engine", "ejs"); app.use(fileRoutes); app.use(helmet()); -app.get("/", async (req, res) => { - const engine = - process.env.STORAGE_MODE === "s3" - ? require("./engines/s3.engine") - : require("./engines/local.engine"); +const s3 = require("./engines/s3.engine"); +const local = require("./engines/local.engine"); - const { gatherStatistics } = engine; - const { uploads, size } = await gatherStatistics(); +app.get("/", async (req, res) => { + let storageEngine; + + if (storageMode === "local") { + storageEngine = local( + multerOptions, + fileNameLength, + process.env.LOCAL_UPLOAD_PATH, + ); + } else if (storageMode === "s3") { + const s3Config = { + accessKeyId: process.env.AWS_ACCESS_KEY_ID, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + region: process.env.AWS_REGION, + bucketName: process.env.S3_BUCKET_NAME, + endpoint: process.env.S3_ENDPOINT, + }; + storageEngine = s3(multerOptions, fileNameLength, s3Config); + } else { + throw new Error("Invalid STORAGE_MODE"); + } + + const { uploads, size } = await storageEngine.gatherStatistics(); res.render("index", { totalUploads: uploads, totalSize: size.toFixed(2),