mirror-filecoffee-filehost/routes/file.routes.js

27 lines
706 B
JavaScript
Raw Normal View History

2024-06-04 18:45:19 +02:00
const express = require("express");
const { uploadFile, getFile } = require("../controllers/file.controller");
const router = express.Router();
2024-06-09 14:30:12 +02:00
const apiKeys = process.env.API_KEYS.split(",");
2024-06-09 14:30:27 +02:00
const allowPublicUploads = process.env.ALLOW_PUBLIC ?? false;
2024-06-09 14:30:12 +02:00
2024-06-04 18:48:52 +02:00
const authenticate = (req, res, next) => {
2024-06-09 14:30:12 +02:00
const apiKey = req.headers["x-api-key"] || req.query.api;
2024-06-04 18:48:52 +02:00
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);
2024-06-04 18:45:19 +02:00
router.get("/u/:filename", getFile);
module.exports = router;