mirror-filecoffee-filehost/index.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-06-02 17:32:54 +02:00
require("dotenv").config();
const express = require("express");
2024-06-04 18:48:52 +02:00
const fileRoutes = require("./routes/file.routes");
2024-06-09 13:25:46 +02:00
const helmet = require("helmet");
2024-06-02 17:32:54 +02:00
const app = express();
2024-06-09 14:21:40 +02:00
const port = process.env.PORT;
2024-06-02 17:32:54 +02:00
const hosterEmail = process.env.HOSTER_EMAIL;
app.set("view engine", "ejs");
2024-06-04 18:45:19 +02:00
app.use(fileRoutes);
2024-06-09 13:25:46 +02:00
app.use(helmet());
2024-06-02 17:32:54 +02:00
2024-06-09 15:34:01 +02:00
const s3 = require("./engines/s3.engine");
const local = require("./engines/local.engine");
2024-06-09 15:37:54 +02:00
const storageMode = process.env.STORAGE_MODE || "local";
2024-06-09 15:34:01 +02:00
2024-06-09 15:41:06 +02:00
// Todo: refactor this way.
const fileNameLength = parseInt(process.env.FILE_NAME_LENGTH, 10) || 10;
const multerOptions = {
limits: parseInt(process.env.FILE_MAX_SIZE_MB, 10) * 1024 * 1024,
};
2024-06-09 14:55:39 +02:00
app.get("/", async (req, res) => {
2024-06-09 15:34:01 +02:00
let storageEngine;
if (storageMode === "local") {
storageEngine = local(
multerOptions,
fileNameLength,
process.env.LOCAL_UPLOAD_PATH,
);
} else if (storageMode === "s3") {
const s3Config = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
bucketName: process.env.S3_BUCKET_NAME,
endpoint: process.env.S3_ENDPOINT,
};
storageEngine = s3(multerOptions, fileNameLength, s3Config);
} else {
throw new Error("Invalid STORAGE_MODE");
}
2024-06-09 14:55:39 +02:00
2024-06-09 15:43:44 +02:00
const { totalUploads, totalSize } = await storageEngine.gatherStatistics();
2024-06-09 15:48:18 +02:00
const kbToMB = totalSize / 1024 / 1024;
2024-06-04 18:45:19 +02:00
res.render("index", {
2024-06-09 15:43:44 +02:00
totalUploads: totalUploads,
2024-06-09 15:49:47 +02:00
totalSize: kbToMB.toFixed(2),
2024-06-04 18:45:19 +02:00
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}`);
});