mirror of
https://github.com/filecoffee/filehost.git
synced 2024-11-13 19:49:56 +01:00
fixing issues and moving auth
This commit is contained in:
parent
3137cb01cc
commit
4cbc4c2d31
2 changed files with 16 additions and 18 deletions
18
index.js
18
index.js
|
@ -1,7 +1,7 @@
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const ejs = require("ejs");
|
const ejs = require("ejs");
|
||||||
const fileRoutes = require("./routes/fileRoutes");
|
const fileRoutes = require("./routes/file.routes");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 3000;
|
const port = process.env.PORT || 3000;
|
||||||
|
@ -13,22 +13,6 @@ let totalUploads = 0;
|
||||||
let totalSize = 0;
|
let totalSize = 0;
|
||||||
|
|
||||||
app.set("view engine", "ejs");
|
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.use(fileRoutes);
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
|
|
|
@ -3,7 +3,21 @@ const { uploadFile, getFile } = require("../controllers/file.controller");
|
||||||
|
|
||||||
const router = express.Router();
|
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);
|
router.get("/u/:filename", getFile);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in a new issue