mirror-filecoffee-filehost/index.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-06-02 17:32:54 +02:00
require("dotenv").config();
const express = require("express");
const ejs = require("ejs");
2024-06-04 18:45:19 +02:00
const fileRoutes = require("./routes/fileRoutes");
2024-06-02 17:32:54 +02:00
const app = express();
const port = process.env.PORT || 3000;
const apiKeys = process.env.API_KEYS.split(",");
2024-06-04 18:45:19 +02:00
const allowPublicUploads = process.env.ALLOW_PUBLIC_UPLOADS === "true";
2024-06-02 17:32:54 +02:00
const hosterEmail = process.env.HOSTER_EMAIL;
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)) {
2024-06-04 18:45:19 +02:00
if (allowPublicUploads) {
req.isPublicUpload = true;
next();
} else {
return res.status(403).json({ error: "Forbidden" });
}
2024-06-02 17:32:54 +02:00
} else {
2024-06-04 18:45:19 +02:00
next();
2024-06-02 17:32:54 +02:00
}
2024-06-04 18:45:19 +02:00
};
2024-06-02 17:32:54 +02:00
2024-06-04 18:45:19 +02:00
app.use(authenticate);
app.use(fileRoutes);
2024-06-02 17:32:54 +02:00
2024-06-04 18:45:19 +02:00
app.get("/", (req, res) => {
res.render("index", {
totalUploads: totalUploads,
totalSize: totalSize.toFixed(2), // Format to 2 decimal places
hosterEmail: hosterEmail,
2024-06-02 17:32:54 +02:00
});
2024-06-04 18:45:19 +02:00
});
2024-06-02 17:32:54 +02:00
2024-06-04 18:45:19 +02:00
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});