-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (51 loc) · 1.72 KB
/
server.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
/* eslint-disable no-console */
require('dotenv').config();
const multer = require('multer');
const express = require('express');
const fs = require('fs');
const path = require('path');
const ip = require('ip');
const data = require('./database/data');
const app = express();
const upload = multer({ dest: 'uploads' });
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index');
});
app.post('/upload', upload.single('file'), async (req, res) => {
// eslint-disable-next-line no-shadow
const { path, originalname } = req.file;
const id = path.replace('uploads\\', '');
const fileData = {
path, originalname, id,
};
data.push(fileData);
res.render('index', { fileLink: `${req.headers.origin}/file/${id}`, originalName: `${req.file.originalname}` });
});
function deleteUploads() {
const directory = 'uploads';
fs.readdir(directory, (err, files) => {
if (err) throw err;
// eslint-disable-next-line no-restricted-syntax
for (const file of files) {
// eslint-disable-next-line no-shadow
fs.unlink(path.join(directory, file), (err) => {
if (err) throw err;
});
}
});
}
async function handleDownload(req, res) {
try {
const file = await data.find((x) => x.id === req.params.id);
res.download(file.path, file.originalname);
} catch {
res.status(400).json({ error: 'File not found' });
}
}
app.route('/file/:id').get(handleDownload).post(handleDownload);
app.listen(process.env.PORT, '0.0.0.0', () => {
deleteUploads();
console.log(`Express app available at http://${ip.address()}:${process.env.PORT}`);
});