-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
336 lines (285 loc) · 8.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//#region Helper
const fs = require("fs");
const data = readData();
const config = readConfig();
const generator = require("generate-password");
const expiryAddition = config["hours-before-expiry"] * 60 * 60 * 1000;
init();
// Initializing all data needed.
function init() {
let fileDir = "./files/";
if (!fs.existsSync(fileDir)) {
//Setup File Directory if missing
fs.mkdirSync(fileDir);
fs.writeFileSync("files.json", JSON.stringify({}), { encoding: "utf-8" });
}
if(!fs.existsSync('./style/' + config["style"] + '.css')){
console.log("[fastcloud]: There is no stylesheet under ./style/" + config["style"] + ".css!\n"
+"[fastcloud]: Create it or switch to a existing stylesheet via editing the config file.");
process.exit(1);
}
console.log("Starting fastcloud with expiry of " + expiryAddition + " ms.");
}
//Read list of files known
function readData() {
let raw;
if (fs.existsSync("files.json")) {
raw = fs.readFileSync("files.json");
} else {
raw = "{}";
}
return JSON.parse(raw);
}
//Read config file
function readConfig() {
let raw;
if (fs.existsSync("config.json")) {
raw = fs.readFileSync("config.json");
} else {
throw "No config file!";
}
return JSON.parse(raw);
}
//Generator for random file names used internally.
function getNewFileName() {
let name = generator.generate({
length: 20,
numbers: true,
});
//Generate until the file is unique
while (data[name]) {
name = generator.generate({
length: 20,
numbers: true,
});
}
return name;
}
/*
Structure:
{
file: name,
type: type (ending)
maxd: max-downloads
expi: Expiry Date/Time
}
Planned: Expiry date (Autodelete)
*/
//Set file inside the file-data list
function setFile(_name,_oldname, _type, _maxd, _expiry) {
data[_name] = {
file: _name,
name: _oldname,
type: _type,
maxd: _maxd,
expi: _expiry,
};
fs.writeFileSync("files.json", JSON.stringify(data), { encoding: "utf-8" });
}
//Delete a file and delete it out of the file-data list
function deleteFile(_name, _type, _skipSave) {
if (fs.existsSync(__dirname + "/files/" + _name + "." + _type))
fs.unlinkSync(__dirname + "/files/" + _name + "." + _type);
delete data[_name];
if (!_skipSave)
fs.writeFileSync("files.json", JSON.stringify(data), { encoding: "utf-8" });
}
//Put the string inside the overall html-layout.
//GoUp should be true if the base url is a level below the current one
//=> Keep relative link working (Like 127.0.0.1:42069/thing/d)
function htmlify(title, text, goup) {
let rel = goup? "../" : "./"
return(
"<!DOCTYPE html><html><head><title>" +
title +
'</title><link rel="icon" type="image/png" href="' +
(rel + 'Icon.png">') +
'<link rel="stylesheet" href="' +
(rel + 'stylesheet">') +
'</head><body><div id="all">' +
'<div class="container"><a href="https://github.com/bluewingtitan/fastcloud"><img>' +
'</a></div><br><div class="container"><h2>' +
text +
"</h2></div></div></body></html>");
}
//#endregion
//#region 'Netcode'
const express = require("express");
const fileUpload = require("express-fileupload");
const e = require("express");
const { setInterval } = require("timers");
const { application } = require("express");
const processMultipart = require("express-fileupload/lib/processMultipart");
const app = express();
app.use(fileUpload());
const port = 33658;
//Make folder statics (Images and css) work more easily
app.use(express.static("statics"));
//Serve index file
app.get("/", (req, res) => {
res.sendFile("./index.html", { root: __dirname });
});
app.get("/f/:name", (req, res) => {
let name = req.params.name;
if(data[name]){
let d = data[name];
res.send(htmlify(
"Your download is ready!",
'File "' + d.name +'" is ready!</h2>' +
'<h2><a href="../d/' + name + '">Download</a>',
true
))
} else {
//If file does not exist
res.send(
htmlify(
"*Sad 404 Noises*",
"The File sadly does not seem to exist. Maybe it expired or has reached max downloads?",
true
)
);
}
})
//Serve hosted file
app.get("/d/:name", (req, res) => {
let name = req.params.name;
//Check if file even exists
if (data[name]) {
//res.sendFile("./files/" + name + data[name].type, { root: __dirname });
const file = `${__dirname}` + "/files/" + name + "." + data[name].type;
//Infinitely Downloadable files have maxd==1
const infiniteDownloads = data[name].maxd == -1;
if (!infiniteDownloads)
//Only decrease if not infinite
setFile(name, data[name].name, data[name].type, data[name].maxd - 1, data[name].expi);
//Only send file if not expired
if (data[name].maxd >= 0 || infiniteDownloads) {
res.download(file, data[name].name);
} else {
//Send error
return res.send(
htmlify(
"Expired!",
"The File has sadly reached it's maximum amount of downloads and is due to deletion.",
true
)
);
}
if (data[name].maxd <= 0) {
//Delete the file if expired
deleteFile(name, data[name].type, false);
}
} else {
//If file does not exist
res.send(
htmlify(
"*Sad 404 Noises*",
"The File sadly does not seem to exist. Maybe it expired or has reached max downloads?",
true
)
);
}
});
app.get("/stylesheet", (req, res) => {
res.sendFile('./style/' + config["style"] + ".css", { root: __dirname });
});
//Upload functionality
app.post("/upload", function (req, res) {
let sampleFile, uploadPath, filetype, filename;
if (!req.files || Object.keys(req.files).length === 0) {
//Check body for files
return res
.status(400)
.send(
htmlify(
"Crickets Chirping...",
"No Files to upload appended. Nothing happened.",
false
)
);
}
//Check if password is right
if (
req.body.password != config["password"] &&
config["password"].length > 0 //Set pw to empty to skip pw check
) {
return res.send(
htmlify("Not Allowed!", "Please enter the right password!", false)
);
}
// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
sampleFile = req.files.sampleFile;
//Check if file is too big
//If using a reverse proxy check if the max upload limit even is allowing for your file size
if (sampleFile.size > config["max-size-byte"]) {
res.type(".html");
return res.send(
htmlify(
"2Heavy4Me!",
"Your file is too big. Your file can be " +
config["max-size-byte"] / 1024 / 1024 +
"mb at max.",
false
)
);
}
//Beautiful code to get the file ending
filetype = sampleFile.name.split(".");
filetype = filetype[filetype.length - 1];
filename = getNewFileName(); //Get new name to save the file as
//File to upload to
uploadPath = __dirname + "/files/" + filename + "." + filetype;
//Tell fastcloud that this file exists
setFile(
filename,
sampleFile.name,
filetype,
config["max-downloads"],
Date.now() + expiryAddition //Current Unix-Timecode + ExpirySeconds
);
// using mv() to place the file on the server
sampleFile.mv(uploadPath, function (err) {
if (err) return res.status(500).send(err);
res.type(".html");
const url = req.hostname;
const relURL = "/f/" + filename;
const directRelURL = "/d/" + filename;
res.send(
htmlify(
"Uploaded!",
"File uploaded! Send this link to share the file:<br>" +
"<a href='" +
relURL +
"'>" +
url + relURL +
"</a><br/><h3>(or <a href='" + directRelURL + "'>" + url + directRelURL + "</a> for a direct download)</h3>",
false
)
);
});
});
//Start the server
app.listen(port, () => {
console.log("fastcloud v1.5 listening at http://localhost:" + port);
});
//#endregion
//#region File Deletion
setInterval(timePurge, 30 * 60 * 1000); //Once every 30 minutes -> Accurate enough for FastCloud-UseCases
//Purge files that are too old.
function timePurge() {
let currentTime = Date.now();
try {
for (const [key, e] of Object.entries(data)) {
if (e.expi < currentTime) {
deleteFile(e.file, e.type, true);
}
}
} catch {
//This mostly exists for the case when there are just no files.
//I could check for that...
//I also could do something more valuable in that time.
}
fs.writeFileSync("files.json", JSON.stringify(data), { encoding: "utf-8" });
}
timePurge(); //One-Shot at start to make sure very old files get deleted instantly after long off-times.
//#endregion