-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
99 lines (81 loc) · 2.61 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require("dotenv").config();
const multer = require("multer");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const File = require("./models/File");
const path = require("path");
const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
const upload = multer({ dest: "uploads" });
// Serve static files from the 'public' folder
app.use(express.static(path.join(__dirname, "public")));
mongoose.connect(process.env.DATABASE_URL);
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("index");
});
app.post("/upload", upload.single("file"), async (req, res) => {
try {
const fileData = {
path: req.file.path,
originalName: req.file.originalname,
};
if (req.body.password != null && req.body.password !== "") {
fileData.password = await bcrypt.hash(req.body.password, 10);
}
// Handle expiration logic
const { expiryDate, expiryTime } = req.body;
// console.log(expiryDate, expiryTime);
// Check if expiresInDate and expiresInTime are provided
if (!expiryDate || !expiryTime) {
throw new Error("Expiration date and time are required.");
}
const file = await File.create({
path: req.file.path,
originalName: req.file.originalname,
password: fileData.password,
expiryDate,
expiryTime,
});
res.render("index", {
fileLink: `${req.headers.origin}/file/${file.id}`,
expiryTime: expiryTime,
expiryDate: expiryDate,
});
} catch (error) {
// console.error(error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.route("/file/:id").get(handleDownload).post(handleDownload);
async function handleDownload(req, res) {
const file = await File.findById(req.params.id);
if (file.password != null) {
if (req.body.password == null) {
res.render("password");
return;
}
if (!(await bcrypt.compare(req.body.password, file.password))) {
res.render("password", { error: true });
return;
}
// Check if the file has expired based on both date and time
if (file.expiryDate && file.expiryTime) {
const currentDate = new Date();
const expiryDate = new Date(file.expiryDate);
const [hours, minutes] = file.expiryTime.split(":");
expiryDate.setHours(Number(hours), Number(minutes));
// console.log(expiryDate);
if (expiryDate <= currentDate) {
res.render("expired", {
expired: true,
});
return;
}
}
}
await file.save();
res.download(file.path, file.originalName);
}
app.listen(process.env.PORT);