-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (144 loc) · 4.21 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
171
import fs from 'fs';
import path from 'path';
import mime from 'mime';
const { getType: extMime } = mime;
import { getMimeType as streamMime } from 'stream-mime-type';
import { isBinaryFileSync } from 'isbinaryfile';
import sanitize from 'sanitize-filename';
import express from 'express';
const app = express();
import cors from 'cors';
import cookieParser from 'cookie-parser';
import fileUpload from 'express-fileupload';
import compression from 'compression';
import morgan from 'morgan';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Add your Replit username, and those with whom you'd like to share
// to provide access to your dashboard. Use an empty array to allow
// open access to everyone.
const WHITELISTED_USERS = ['RayhanADev'];
import 'dotenv/config';
const dev = process.env.NODE_ENV !== 'production';
const isAllowed = (req) => {
if (WHITELISTED_USERS.length === 0) return true;
else if (WHITELISTED_USERS.includes(req.headers['x-replit-user-name']))
return true;
return false;
};
app.use(express.static('assets'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(fileUpload());
app.use(morgan('tiny'));
if (!dev) {
app.use(cors());
app.use(compression());
}
app.get('/', (req, res) => {
if (isAllowed(req)) return res.status(307).redirect('/~');
return res.status(200).sendFile('./views/index.html', { root: __dirname });
});
app.get('/auth', (req, res) => {
res.status(200).sendFile('./views/auth.html', { root: __dirname });
});
app.get('/~', (req, res) => {
if (!isAllowed(req)) return res.status(307).redirect('/auth');
return res
.status(200)
.sendFile('./views/dashboard.html', { root: __dirname });
});
app.post('/api', async (req, res) => {
if (!isAllowed(req)) {
return res.status(403).send({
status: 403,
message: 'You are not logged in.',
});
}
try {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send({
status: 400,
message: 'No file uploaded.',
});
} else {
const name = req.body.name;
const upload = req.files.upload;
if (sanitize(name) !== name) {
return res.status(400).send({
status: 400,
message: 'Filename contains unsafe content.',
});
}
const folder =
__dirname + '/files/' + name + path.extname(upload.name);
if (fs.existsSync(folder)) {
return res.status(409).send({
status: 409,
message: 'Resource already exists at given location.',
});
}
upload.mv(folder);
return res.status(200).send({
status: 200,
message: 'File is uploaded.',
data: {
name: name,
mimetype: upload.mimetype,
size: upload.size,
location: `${req.hostname}/f/${
name + path.extname(upload.name)
}`,
},
});
}
} catch (err) {
return res.status(500).send(err);
}
});
app.get('/f/:path', async (req, res) => {
try {
const localPath = './files/' + req.params.path;
if (fs.existsSync(localPath)) {
const readStream = fs.createReadStream(localPath);
const { stream, mime: streamMimeType } = await streamMime(
readStream,
{
fileName: req.params.path.split('/').slice(-1)[0],
},
);
const extMimeType = extMime(localPath);
const binaryFile = isBinaryFileSync(localPath);
let finalMimeType = 'text/plain';
if (streamMimeType === extMimeType) {
finalMimeType = extMimeType;
} else if (streamMimeType !== 'application/octet-stream') {
finalMimeType = streamMimeType;
} else if (binaryFile === true) {
finalMimeType = 'application/octet-stream';
} else {
finalMimeType = 'text/plain';
}
res.status(200);
res.header('Content-Type', finalMimeType);
res.header('Content-Disposition', 'inline');
res.header(
'Cache-Control',
'public, max-age=1, stale-while-revalidate=59',
);
stream.pipe(res);
} else {
res.status(404).sendFile('./views/404.html', { root: __dirname });
}
} catch (error) {
console.log(error);
res.status(500).sendFile('./views/500.html', { root: __dirname });
}
});
app.get('*', (req, res) => {
res.status(404).sendFile('./views/404.html', { root: __dirname });
});
app.listen(3000, () => {
console.log('Application running on Port:', 3000);
});