This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
196 lines (162 loc) · 4 KB
/
index.ts
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
const { existsSync, unlinkSync, createWriteStream } = require('fs');
const { get: httpGet } = require('https');
const { createServer } = require('http');
const express = require('express');
const { nsfw } = require('unscan');
const { join } = require('path');
const app = express();
const server = createServer(app);
const port = process.env.PORT || 4000;
const { apiKey } = require('./config');
// Settings
app.set('port', port);
app.set('json spaces', 2);
// Middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/attachments', express.static('./attachments'));
app.use('/assets', express.static('./assets'));
// Endpoints
app.get('/', async (req: any, res: any) => {
res.redirect('https://chatglobal.ml/');
});
app.post('/post', async (req: any, res: any) => {
function createID(
length: number,
createFileName: string,
createShortPath: string
): string {
const characters = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z'
];
const resultArray = [];
for (let i = 0; i < length; i++) {
resultArray.push(characters[~~(Math.random() * characters.length)]);
}
const result = `${resultArray.join('')}-${createFileName}`;
if (existsSync(`${createShortPath}${result}`))
return createID(
~~(Math.random() * characters.length) + length,
createFileName,
createShortPath
);
return result;
}
const headers = req.headers;
const body = req.body;
if (!headers.api_key)
return res.status(400).json({
error: 'API_KEY is a required field.',
status: 400
});
if (headers.api_key != apiKey)
return res.status(401).json({
error: 'Invalid API_KEY provided.',
status: 401
});
if (!body.url)
return res.status(400).json({
error: 'URL is a required field.',
status: 400
});
const fileName = body.url.substring(body.url.lastIndexOf('/') + 1);
const shortPath = './attachments/';
const fullFile = createID(
Math.floor(Math.random() * 4 + 2),
fileName,
shortPath
);
const path = `${shortPath}${fullFile}`;
const download = (url: string, dest: string) =>
new Promise((resolve, reject) => {
const file = createWriteStream(dest, { flags: 'wx' });
const request = httpGet(url, (response: any) => {
if (response.statusCode === 200) {
response.pipe(file);
} else {
file.close();
unlinkSync(dest);
reject(
`Server responded with ${response.statusCode}: ${response.statusMessage}`
);
}
});
request.on('error', (err: any) => {
file.close();
unlinkSync(dest);
reject(err.message);
});
file.on('finish', () => {
resolve('Success');
});
file.on('error', (err: any) => {
file.close();
if (err.code === 'EEXIST') {
reject('File already exists');
} else {
unlinkSync(dest);
reject(err.message);
}
});
});
const downloadResponse = await download(body.url, path).catch(() => false);
const nsfwRes = await nsfw.file(path).catch(() => false);
if (nsfwRes && nsfwRes.nsfw) {
console.log(`NSFW Content detected ==> ${fullFile}`);
return res.status(200).json({
text: 'NSFW Content detected.',
url: 'https://cdn.chatglobal.ml/assets/navi.png',
status: 200
});
}
if (downloadResponse) {
return res.status(200).json({
text: 'Successfully uploaded.',
url: `https://cdn.chatglobal.ml/attachments/${fullFile}`,
status: 200
});
} else {
return res.status(500).json({
text: 'Internal server error.',
url: `https://cdn.chatglobal.ml/attachments/${fullFile}`,
status: 500
});
}
});
app.use((req: any, res: any, next: any) => {
const error: any = new Error('Not Found.');
error.status = 404;
next(error);
});
app.use((error: any, req: any, res: any, next: any) => {
res.status(404).send('404: Not found.');
});
// Starting the server
server.listen(app.get('port'), () => {
console.log(`Server listening on port ${app.get('port')}`);
});