-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (48 loc) · 1.86 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
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();
const uniqid = require('uniqid');
const config = require('./config.json');
let PORT = config.port || 8080;
let uploadsPath = `/${config.uploadsPath}`;
let localPath = `/${config.localPath}`;
let allowedExtensions = config.allowedExtensions;
let userBase = config.userBase;
app.use(uploadsPath, express.static(`${__dirname}${localPath}`));
app.use(fileUpload());
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.post('/upload', function (req, res) {
let sampleFile;
let uploadPath;
let user = req.body.username;
let password = req.body.password;
let files = req.files;
let fileName;
let extension;
let u = userBase.filter(x => x.username == user);
if(!u || Object.keys(u).length === 0)
return res.status(400).send('Invalid user.');
if(u[0].password !== password)
return res.status(400).send('Invalid credentials.');
if (!files || Object.keys(files).length === 0)
return res.status(400).send('No files were uploaded.');
sampleFile = req.files.sampleFile;
fileName = uniqid.time(); // Generate fileName via time-uuid
extension = sampleFile.name.substring(sampleFile.name.lastIndexOf('.') + 1); // File extension
if (!allowedExtensions.includes(extension))
return res.status(401).send('Invalid file extension type.');
uploadPath = `${__dirname}${localPath}/${fileName}.${extension}`;
sampleFile.mv(uploadPath, function (err) {
if (err)
return res.status(500).send(err);
res.redirect(`${uploadsPath}/${fileName}.${extension}`);
});
});
app.get('*', function(req, res) {
res.redirect('/') // Redirect user on Invalid Path
});
app.listen(PORT, function() {
console.log(`File-Uploader working on :${PORT}`);
});