-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
171 lines (151 loc) · 6.25 KB
/
app.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
/* modules */
const Discord = require('discord.js');
const { MessageAttachment } = require('discord.js');
const cookieParser = require('cookie-parser');
const fileUpload = require("express-fileupload");
const moment = require('moment');
const tz = require('moment-timezone');
const ejs = require('ejs');
const express = require('express');
const axios = require('axios');
/* global variables */
const Global = require('./Global.js').config;
const app = express();
const client = new Discord.Client();
const knex = require("knex")({
client: "postgres",
connection: process.env["DATABASE_URL"],
pool: { min: 0, max: 80 },
});
/* discord client */
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
/* middleware */
app.use(cookieParser());
app.use(fileUpload());
/* static files */
app.use(express.static('public'));
// app.use('/js', express.static(__dirname + 'public/js'));
app.use('/css', express.static(__dirname + 'public/css'));
// app.use('/imgs', express.static(__dirname + 'public/imgs'));
/* set views */
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
/* routes */
app.get('/', (req, res) => {
res.render('index', {
humanFileSize: Global.maxFileSize.human,
byteFileSize: Global.maxFileSize.byte
});
});
app.get('/results', (req, res) => {
const uploadResults = req.cookies.results;
res.render('results', {
url: uploadResults.cdn,
proxyURL: uploadResults.proxy,
customURL: uploadResults.custom,
messageId: uploadResults.id,
uploadDate: uploadResults.uploaded,
fileType: uploadResults.mime
});
});
app.get('/attachments/:messageId/:attachmentId/:filename', async (req, res) => {
const channelId = Global.fileChannel;
const attachmentId = req.params.attachmentId;
const filename = req.params.filename;
try {
const query = await knex('message_ids')
.where('ahid', BigInt(attachmentId))
.first();
if (!query.mid) {
return res.status(404).send('Message ID not found for the given channel.');
}
client.channels.cache.get(channelId).messages.fetch(query.mid.toString())
.then(message => {
const cdnUrl = message.attachments.first()?.url;
if (!cdnUrl) {
return res.status(404).send('CDN URL not found for the given message.');
}
axios.get(cdnUrl, { responseType: 'stream' })
.then(response => {
// Check for range header
let range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : response.data.size - 1;
const chunksize = (end - start) + 1;
res.writeHead(206, { // Partial content
'Content-Range': `bytes ${start}-${end}/${response.data.size}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': response.headers['content-type'],
'Content-Disposition': `attachment; filename="${filename}"`,
});
response.data.pipe(res);
} else {
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`);
res.setHeader('Content-Type', response.headers['content-type']);
response.data.pipe(res);
}
})
.catch(error => {
console.error('Error streaming file from CDN:', error);
res.status(500).send('Internal Server Error');
});
});
} catch (error) {
console.error('Error fetching message ID:', error);
res.status(500).send('Internal Server Error');
}
});
app.post('/api/upload', async (req, res) => {
// if (!req.files || !req.files.file || !req.body.mid) return res.status(400).send('No value was provided.');
if (req.body?.mid) {
// Fetch the message by ID and then set cookies
client.channels.cache.get(Global.fileChannel).messages.fetch(req.body.mid)
.then(message => {
setCookies(req, res, message).then(() => {
res.redirect('/results');
}).catch(err => console.log(err));
})
.catch(err => res.status(404).send('Message not found.'));
return;
}
if (req.files.file.size > Global.maxFileSize) return res.status(413).send('File too large.');
const buffer = Buffer.from(req.files.file.data);
const attachment = new MessageAttachment(buffer, req.files.file.name);
client.channels.cache.get(Global.fileChannel).send(attachment);
client.on('message', message => {
if (message.author.id !== client.user.id) return;
if (message.channel.id !== Global.fileChannel) return;
if (!message.attachments.first()) return;
if (req.headers['upload-source'] === 'API') return res.send(message.attachments.first()?.url);
setCookies(req, res, message).then(async () => {
await knex("message_ids").insert({
ahid: BigInt(message.attachments.first().id),
mid: BigInt(message.id),
});
res.redirect('/results');
}).catch(err => console.log(err));
});
});
app.listen(Global.port, () => {
console.clear();
console.log(`Server is running @ http://${Global.host}:${Global.port}`);
client.login(Global.token);
});
function setCookies(req, res, message) {
return new Promise((resolve, reject) => {
res.cookie('results', {
cdn: message.attachments.first()?.url,
proxy: message.attachments.first()?.proxyURL,
custom: message.attachments.first()?.url.split("?")[0].replace("cdn.discordapp.com", "cdn-slave.spin.rip"),
id: message.id,
uploaded: moment.tz('America/Los_Angeles').format("[File uploaded on ] LL [PST]"),
mime: req.files?.file?.mimetype || ""
});
resolve();
});
}