-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
124 lines (114 loc) · 5.16 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
import 'dotenv/config';
import Fastify, { FastifyReply, FastifyRequest } from 'fastify';
import { join } from 'path';
import fs from 'fs';
import api from './api';
const app = Fastify({ logger: false, });
interface DustbinDocument {
data?: any;
id: string;
language: string;
};
app.get('/tailwind.css', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'tailwind.css'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/css').send(fileBuffer);
});
});
app.get('/favicon.png', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'favicon.png'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('image/x-png').send(fileBuffer);
});
});
app.get('/script/new.js', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'script', 'new.js'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('application/javascript').send(fileBuffer);
});
});
app.get('/script/admin.js', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'script', 'admin.js'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('application/javascript').send(fileBuffer);
});
});
app.get('/', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'index.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
});
});
app.get('/new', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'new.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
});
});
app.get('/admin', async (_request: FastifyRequest, reply: FastifyReply) => {
fs.readFile(join(__dirname, '..', 'pages', 'admin.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(fileBuffer);
});
});
app.get('/paste/*', async (request: FastifyRequest, reply: FastifyReply) => {
let params: any = request.params;
var fileId: string = params['*']
if (fileId == '') {
return reply.redirect('/new');
}
const result: DustbinDocument | null = await api.getDocument(fileId.split('/')[0]);
if (!result) return reply.send({ error: 'DOCUMENT_NOT_FOUND' });
if (fileId.split('/').length > 1) {
return fileId.split('/')[1] == 'raw' // /paste/fileId/raw
? reply.type('text/plain').send(result?.data)
: reply // /paste/fileId/[download]
.type('application/octet-stream')
.header('Content-Disposition', `attachment; filename="${fileId.split('/')[0]}.${result?.language}"`)
.send(Buffer.from(result?.data, 'utf8'));
}
fs.readFile(join(__dirname, '..', 'pages', 'paste.html'), (_err: Error | null, fileBuffer: Buffer) => {
reply.type('text/html').send(
Buffer.from(fileBuffer.toString('utf8').replace('{{ %data% }}', result?.data).replace('{{ %language% }}', result!.language.toLowerCase()))
);
});
});
// -------------------
// ------ API's ------
// -------------------
app.post('/api/new', async (request: FastifyRequest, reply: FastifyReply) => {
let body: any = request.body;
let data: string = body.data;
let language: string = body.language;
if (data == '') return { error: 'DATA_IS_EMPTY' };
if (language == '') return { error: 'DATA_IS_EMPTY' };
if (![
"Bash", "C", "CSharp", "C++", "CSS", "Diff", "Go",
"HTML", "XML", "JSON", "Java", "JavaScript",
"Kotlin", "Less", "Lua", "Makefile", "Markdown",
"Objective-C", "PHP", "Perl", "PlainText",
"Python", "R", "Ruby", "Rust", "SCSS",
"SQL", "ShellSession", "Swift", "TOML",
"INI", "TypeScript", "VB.NET", "YAML"
].includes(language)) return { error: 'LANGUAGE_NOT_SUPPORTED' };
api.newDocument(data, language).then((result: DustbinDocument) => {
reply.send({ error: null, id: result.id, language: result.language });
});
});
app.post('/api/get', async (request: FastifyRequest, reply: FastifyReply) => {
let body: any = request.body;
let fileId: string = body.fileId;
api.getDocument(fileId).then((result: DustbinDocument | null) => {
if (!result) return reply.send({ error: 'DOCUMENT_NOT_FOUND' });
reply.send({ error: null, id: result.id, language: result.language, data: result.data.toString('utf8') });
});
});
app.post('/api/admin', async (request: FastifyRequest, reply: FastifyReply) => {
let body: any = request.body;
let username: string = body.username;
let password: string = body.password;
if (username != process.env.dustbinUsername || password != process.env.dustbinPassword) {
return reply.send({ error: 'INVALID_AUTH' });
}
api.getAllDocuments().then((result: Array<any>) => {
return reply.send({result: result, error: null});
});
});
// Run the server !
(async () => {
await app.listen(process.env.PORT || 1134, '0.0.0.0');
console.log(`[ Info ] Dustbin Init On https://localhost:${process.env.PORT || 1134}`);
})();