mirror of
https://github.com/filecoffee/filehost.git
synced 2024-11-13 19:49:56 +01:00
32 lines
884 B
JavaScript
32 lines
884 B
JavaScript
const express = require("express");
|
|
const { uploadFile, getFile } = require("../controllers/file.controller");
|
|
|
|
const router = express.Router();
|
|
|
|
const apiKeys = process.env.API_KEYS.split(",");
|
|
const allowPublicUploads = process.env.ALLOW_PUBLIC ?? false;
|
|
|
|
const authenticate = (req, res, next) => {
|
|
const apiKey = req.headers["x-api-key"] || req.query.api;
|
|
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("/files", async (req, res) => {
|
|
const userId = req.session.userId;
|
|
const files = await File.findAll({ where: { userId } });
|
|
res.render("files", { files });
|
|
});
|
|
|
|
module.exports = router;
|