-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
223 lines (208 loc) · 8.28 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
const express = require('express');
const fu = require('express-fileupload');
const cors = require('cors');
const bodyParser = require('body-parser');
const fs = require('fs');
const config = JSON.parse(fs.readFileSync("./config.json"));
const port = config.settings.port;
const host = config.settings.host;
const morgan = require('morgan');
const _ = require('lodash');
const app = express();
require('dotenv').config();
const admin_key = process.env.ADMIN;
const now = Date.now();
const timezone = require('moment-timezone');
const curdate = timezone.tz(now, 'Europe/Prague').format('DD.MM.YYYY HH:mm:SS');
const auser = process.env['user'];
const apass = process.env['pass'];
//*midleware
app.use(fu({
createParentPath: true
}));
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan('dev'));
app.set('view engine', 'ejs');
//*Functions
const generateFolder = () => {
var length = 5,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
retVal = "";
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
return retVal;
}
//*requests
app.post('/upload', async (req, res) => {
try {
if (!req.files) {
res.format({
'text/plain': function () {
res.send('Bad Request | 400');
res.send('You need to upload a file');
},
'text/html': function () {
res.send('<center><h1>Bad Request | 400</h1>');
res.send('<br><hr><br>');
res.send('<p>You need to upload a file</p>');
},
'application/json': function () {
res.send({
code: 400,
title: "Bad Request",
description: "You need to upload a file"
});
},
default: function () {
// log the request and respond with 406
res.status(406).send('Not Acceptable')
}
});
} else {
let file = req.files.file;
let gen_folder = generateFolder();
let file_info = {
name: file.name,
size: file.size+" B",
sizeKB: file.size / 1000 + " KB",
type: file.mimeType,
uploaded: curdate,
generatedFolder: gen_folder,
type: file.mimetype
};
file.mv(__dirname+"/uploads/files/"+gen_folder+"___"+file.name);
fs.writeFileSync(__dirname+"/uploads/jsons/"+gen_folder+"___"+file.name+".json", JSON.stringify(file_info));
res.json({url: host+gen_folder+"/"+file.name, embed: host+"e/"+gen_folder+"/"+file.name});
console.log("New File Uploaded at "+curdate);
console.log("========================================================");
console.log("'"+host+gen_folder+"/"+file.name+"' || '"+gen_folder+"___"+file.name+"'");
}
} catch (err) {
res.status(500).send({
code: 500,
title: "Internal Server Error"
});
console.log(err);
};
});
app.get("/:folder/:file", async (req, res) => {
try {
var folder = req.params.folder;
var name = req.params.file;
var filecheck = fs.readFileSync(__dirname+"/uploads/jsons/"+folder+"___"+name+".json");
var file_info = JSON.parse(fs.readFileSync(__dirname+`/uploads/jsons/${folder}___${name}.json`));
if (!filecheck) res.status(404).json({code:404, title: "Not Found"});
if (folder !== file_info.generatedFolder) res.status(404).json({code:404, title: "Not Found"});
res.status(200).sendFile(__dirname+`/uploads/files/${folder}___${name}`);
} catch (err) {
res.status(404).json({code:404, title: "Not Found"});
console.log(err);
}
});
app.get("/e/:folder/:file", async(req, res) => {
try {
var folder = req.params.folder;
var name = req.params.file;
var imgurl = host+folder+"/"+name;
var filecheck = fs.readFileSync(__dirname+"/uploads/jsons/"+folder+"___"+name+".json");
var file_info = JSON.parse(fs.readFileSync(__dirname+`/uploads/jsons/${folder}___${name}.json`));
if (!filecheck) res.status(404).json({code:404, title: "Not Found"});
if (folder !== file_info.generatedFolder) res.status(404).json({code:404, title: "Not Found"});
if (file_info.type === "image/jpeg") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<img src='"+imgurl+"' style='background-color: #2c2c2c;border-radius: 10px;' height='512px'>",
what: "image"
});
} else if (file_info.type === "image/jpg") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<img src='"+imgurl+"' style='background-color: #2c2c2c;border-radius: 10px;' height='512px'>",
what: "image"
});
} else if (file_info.type === "image/png") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<img src='"+imgurl+"' style='background-color: #2c2c2c;border-radius: 10px;' height='512px'>",
what: "image"
});
} else if (file_info.type === "video/mp4") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<video controls style='background-color: #2c2c2c;border-radius: 10px;' height='512px'><source src='"+imgurl+"' type='video/mp4'>Your browser does not support HTML5 video.</video>",
what: "video"
});
} else if (file_info.type === "video/avi") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<video controls style='background-color: #2c2c2c;border-radius: 10px;' height='512px'><source src='"+imgurl+"' type='video/avi'>Your browser does not support HTML5 video.</video>",
what: "video"
});
} else if (file_info.type === "audio/mpeg") {
res.render(__dirname+"/embed.ejs", {
name: file_info.name,
size: file_info.sizeKB,
uDate: file_info.uploaded,
path: file_info.generatedFolder+"/"+file_info.name,
url: imgurl,
type: file_info.type,
element: "<audio controls style='background-color: #2c2c2c;border-radius: 10px;' height='512px'><source src='"+imgurl+"' type='audio/mpeg'>Your browser does not support HTML5 video.</audio>"
});
} else {
res.redirect(host+folder+"/"+name).end();
}
} catch (err) {
res.status(404).json({code:404, title: "Not Found"});
console.log(err);
}
});
app.get("*", async (req, res) => {
if (req.query.upload === "show") {
res.status(200).sendFile(__dirname+"/upload.html");
} else {
res.status(200).send("<style>body {display: flex;justify-content:center;align-items:center;} h1::hover {font-size:200%;transition:0.2s;}</style><body><h1>"+host+"upload</h1></body>");
}
});
app.listen(port, () => {
console.log("Web Started : " + port);
});
process.on('uncaughtException', (error, origin) => {
console.log('----- Uncaught exception -----')
console.log(error)
console.log('----- Exception origin -----')
console.log(origin)
});
process.on('unhandledRejection', (reason, promise) => {
console.log('----- Unhandled Rejection at -----')
console.log(promise)
console.log('----- Reason -----')
console.log(reason)
});