-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (140 loc) · 4.89 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** @format */
const express = require("express");
const bodyParser = require("body-parser");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "box/");
},
filename: function (req, file, cb) {
cb(null, Buffer.from(file.originalname, "latin1").toString("utf8"));
},
});
const upload = multer({
storage: storage,
fileFilter: (req, file, cb) => {
let filePath = path.resolve(__dirname, "box", file.originalname);
if (fs.existsSync(filePath)) {
let count = 1;
let newFileName = file.originalname;
while (fs.existsSync(filePath)) {
const extension = path.extname(file.originalname);
const fileNameWithoutExtension = path.basename(
file.originalname,
extension
);
newFileName = `${fileNameWithoutExtension}_${count}${extension}`;
filePath = path.resolve(__dirname, "box", newFileName);
count++;
}
file.originalname = newFileName;
}
cb(null, true);
},
}).array("file");
app.get("/box", (req, res) => {
var file = req.query.file;
if (file === undefined || file === "") {
res.redirect("/");
return;
}
const normalizedFilePath = path.resolve(__dirname, "box", file);
const boxDirectory = path.resolve(__dirname, "box");
if (!normalizedFilePath.startsWith(boxDirectory)) {
res.status(403).send("What are you trying to do?");
return;
}
//check if file exist
if (!fs.existsSync(normalizedFilePath)) {
res.status(404).send("File not found.");
return;
}
var password = require("./password.json")[file.split("/").pop()];
if (password === undefined || req.query.password === password) {
res.sendFile(normalizedFilePath);
} else {
res.sendFile(path.resolve(__dirname, "public", "download.html"));
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public/index.html"));
});
app.get("/list", (req, res) => {
fs.readdir("box", (err, files) => {
if (err) {
return res.status(500).send("Unable to read directory contents.");
}
let reply = files.map(file => ({
extension: getFileExtension(file),
file: file,
size: getFileSize(file),
uploadDate: new Date(
fs.statSync(path.join("box", file)).mtime
).toUTCString(),
}));
res.send(reply);
});
});
// Handle file upload
app.post("/upload", (req, res) => {
upload(req, res, function (err) {
if (err) return res.status(500).send("Error uploading file.");
let password = req.body.password;
if (password !== "") {
var passwordFile = require("./password.json");
req.files.forEach(file => {
let realFileName = file.originalname; // Store the real original file name
let newFileName = realFileName; // Initialize new file name with real original file name
if (
fs.existsSync(path.resolve(__dirname, "box", realFileName))
) {
let count = 1;
while (
fs.existsSync(
path.resolve(__dirname, "box", newFileName)
)
) {
const extension = path.extname(realFileName);
const fileNameWithoutExtension = path.basename(
realFileName,
extension
);
newFileName = `${fileNameWithoutExtension}_${count}${extension}`;
count++;
}
}
passwordFile[realFileName] = password; // Use the real original file name as key
fs.writeFileSync(
"password.json",
JSON.stringify(passwordFile, null, 4),
"utf8"
);
});
}
res.redirect("/");
});
});
// Function to get file extension
function getFileExtension(filename) {
return filename.split(".").pop();
}
function getFileSize(filename) {
const stats = fs.statSync(path.join("box", filename));
// Convert file size to KB
var size = stats.size / 1024;
if (size < 1024) {
return size.toFixed(2) + " KB";
}
// Convert file size to MB
size = size / 1024;
return size.toFixed(2) + " MB";
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});