-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
115 lines (89 loc) · 2.42 KB
/
main.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
const express = require("express");
const fileUpload = require("express-fileupload");
const FormData = require("form-data");
const axios = require("axios");
const cors = require("cors");
const app = express();
const port = 3000;
require("dotenv").config();
app.use(
fileUpload({
limits: { fileSize: 8 * 1024 * 1024 },
})
);
app.use(cors());
app.use(express.urlencoded({ extended: true }));
const uploadToDiscord = async (file) => {
const discordWebhookUrl = process.env.DISCORD_WEBHOOK_URL;
if (!discordWebhookUrl)
throw new Error(
'Please specify your discord webhook in .env file by "DISCORD_WEBHOOK_URL" key'
);
const formData = new FormData();
if (Array.isArray(file)) {
file.forEach((file, index) => {
formData.append(`files[${index}]`, file.data, file.name);
});
} else {
formData.append("file", file.data, file.name);
}
const { data } = await axios.post(discordWebhookUrl, formData, {
headers: { "Content-Type": "multipart/form-data" },
});
if (!data?.attachments?.length) throw new Error("No attachments found");
return data?.attachments;
};
const urlToFile = async (url) => {
const filename = url
.substring(url.lastIndexOf("/") + 1)
.replace(/((\?|#).*)?$/, "");
const response = await axios.get(url, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data, "utf-8");
return {
name: filename,
data: buffer,
};
};
const urlsToFiles = async (url) => {
if (Array.isArray(url)) {
const promises = url.map((url) => urlToFile(url));
return Promise.all(promises);
}
return urlToFile(url);
};
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/upload", async (req, res) => {
try {
if (!req.files?.file && !req.body?.url) {
res.statusCode = 400;
res.json({
success: false,
error: "Please specify files or urls",
});
return;
}
let attachments = [];
if (req.files?.file) {
attachments = await uploadToDiscord(req.files.file);
} else {
const files = await urlsToFiles(req.body.url);
attachments = await uploadToDiscord(files);
}
res.json({
success: true,
attachments,
});
} catch (err) {
console.log(err);
res.statusCode = 500;
res.json({
success: false,
error: err.message,
});
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});