fixing issues and moving auth

This commit is contained in:
Daniël 2024-06-04 18:48:52 +02:00
parent 3137cb01cc
commit 4cbc4c2d31
2 changed files with 16 additions and 18 deletions

View file

@ -1,7 +1,7 @@
require("dotenv").config();
const express = require("express");
const ejs = require("ejs");
const fileRoutes = require("./routes/fileRoutes");
const fileRoutes = require("./routes/file.routes");
const app = express();
const port = process.env.PORT || 3000;
@ -13,22 +13,6 @@ let totalUploads = 0;
let totalSize = 0;
app.set("view engine", "ejs");
const authenticate = (req, res, next) => {
const apiKey = req.headers["x-api-key"];
if (!apiKey || !apiKeys.includes(apiKey)) {
if (allowPublicUploads) {
req.isPublicUpload = true;
next();
} else {
return res.status(403).json({ error: "Forbidden" });
}
} else {
next();
}
};
app.use(authenticate);
app.use(fileRoutes);
app.get("/", (req, res) => {

View file

@ -3,7 +3,21 @@ const { uploadFile, getFile } = require("../controllers/file.controller");
const router = express.Router();
router.post("/upload", uploadFile);
const authenticate = (req, res, next) => {
const apiKey = req.headers["x-api-key"];
if (!apiKey || !apiKeys.includes(apiKey)) {
if (allowPublicUploads) {
req.isPublicUpload = true;
next();
} else {
return res.status(403).json({ error: "Forbidden" });
}
} else {
next();
}
};
router.post("/upload", authenticate, uploadFile);
router.get("/u/:filename", getFile);
module.exports = router;