-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
170 lines (163 loc) · 4.81 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const path = require("path");
const basicAuth = require("express-basic-auth");
const multer = require("multer");
const request = require("request");
var url = process.env.url;
const app = express();
app.set("port", process.env.PORT || 3000);
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
var limits = {
files: 1, // allow only 1 file per request
fileSize: 10485760, // 1 MB (max file size)
};
// multer middleware
const upload = multer({
dest: path.join(__dirname, "./public/images"),
limits: limits,
});
const handleError = (err, res) => {
res.status(500).contentType("text/plain").end("error");
};
app.get(
"/",
basicAuth({ users: { admin: "supersecret" }, challenge: true }),
(req, res) => {
const fp = path.join(__dirname, `./public/images/`);
fs.readdir(fp, (err, contents) => {
// unexpected error handler
if (err) return handleError(err, res);
console.log(contents);
return res.render("index", { data: contents });
});
}
);
app.get("/api/images/:image", (req, res) => {
res.sendFile(path.join(__dirname, `./public/images/${req.params.image}`));
});
app.get("/api/:file", (req, res) => {
res.sendFile(path.join(__dirname, `./public/files/${req.params.file}`));
});
app.post(
"/upload",
basicAuth({ users: { admin: "supersecret" } }),
upload.single("file"),
(req, res) => {
if (req.file) {
console.log(req.file.path);
const filePath = req.file.path;
const storagePath = path.join(
__dirname,
`./public/images/${req.file.originalname}`
);
const storagePath2 = path.join(
__dirname,
`./public/files/${req.file.originalname}`
);
if (
path.extname(req.file.originalname).toLowerCase() === ".png" ||
path.extname(req.file.originalname).toLowerCase() === ".jpg" ||
path.extname(req.file.originalname).toLowerCase() === ".gif" ||
path.extname(req.file.originalname).toLowerCase() === ".svg"
) {
fs.rename(filePath, storagePath, (err) => {
if (err) return handleError(err, res);
console.log("File uploaded!");
var options = {
method: "post",
body: {
embeds: [
{
title: "File Uploaded to CDN",
image: {
url: `https://cdn.cnsp.eu.org/api/images/${req.file.originalname}`,
},
footer: {
text: `IP: ${req.connection.remoteAddress}`,
},
},
],
},
json: true,
url: url,
};
if (process.env.url) {
request(options);
}
return res.redirect(`/api/images/${req.file.originalname}`);
});
} else {
fs.rename(filePath, storagePath2, (err) => {
if (err) return handleError(err, res);
console.log("File uploaded!");
var options = {
method: "post",
body: {
embeds: [
{
title: "File Uploaded to CDN",
fields: [
{
name: "URL",
value: `https://cdn.cnsp.eu.org/api/${req.file.originalname}`,
},
],
footer: {
text: `IP: ${req.connection.remoteAddress}`,
},
},
],
},
json: true,
url: url,
};
if (process.env.url) {
request(options);
}
return res.redirect(`/api/${req.file.originalname}`);
});
}
} else {
res.status(403).contentType("text/plain").end("Please select a file!");
}
}
);
app.get(
"/delete/:image",
basicAuth({ users: { admin: "supersecret" }, challenge: true }),
async (req, res) => {
console.log(req.params.image);
const fp = path.join(__dirname, `./public/images/${req.params.image}`);
await fs.unlink(fp, (err) => {
// unexpected error handler
if (err) return handleError(err, res);
var options = {
method: "post",
body: {
embeds: [
{
title: "File Removed from CDN",
description: `${req.params.image}`,
},
],
},
json: true,
url: url,
};
if (process.env.url) {
request(options);
}
return console.log(`${req.params.image} was removed from the server.`);
});
res.redirect("/");
}
);
app.listen(3000),
() => {
console.log(`CDN ready on port: ${app.get("port")}`);
};