-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (55 loc) · 1.78 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
63
64
65
66
const express = require('express');
const net = require('net');
const qrcode = require('qrcode-terminal');
const config = require('./config/config');
const storeToFile = require('./helpers/storeToFile');
const bodyParser = require('body-parser');
const parseData = function(payload){
if( payload.length > config.MAX_PAYLOAD_LEN){
console.log(`invalid payload size${payload.length}`);
return;
}
const fileName = storeToFile(payload, config.FILES.ROOT_FOLDER, config.TOKEN_LEN);
const fileLink = `http://${config.DOMAIN}/${fileName}`;
return fileLink;
}
/*********************************
HTTP - ENDPOINTS
**********************************/
const app = express();
app.disable('x-powered-by');
app.disable('etag');
app.use(bodyParser.text({limit: config.FILES.BIN_LIMIT}));
app.use(bodyParser.raw({limit:config.FILES.BIN_LIMIT}));
// DONWLOAD BIN's
express.static.mime.default_type = "text/html";
app.use(express.static(config.FILES.ROOT_FOLDER));
app.use(express.static("www"));
// MISC ENDPOINTS
app.post('/', (req, res) => {
if(!req.body){
res.status(422).send("Empty or invalid payload");
}
const fileLink = parseData(req.body);
res.status(200).send(fileLink + "\n");
});
/*********************************
RAW TCP - ENDPOINTS
**********************************/
app.qrserver = net.createServer((socket) => {
socket.on('data', (payload) => {
const fileLink = parseData(payload);
qrcode.generate(fileLink, (qr) => {
socket.write(`${fileLink}\n${qr}\n`);
socket.destroy();
});
});
}).listen(config.INGOING.PORT_DEFAULT);
app.qrserver = net.createServer((socket) => {
socket.on('data', (payload) => {
const fileLink = parseData(payload);
socket.write(`${fileLink}\n`);
socket.destroy();
})
}).listen(config.INGOING.PORT_URL_ONLY);
module.exports = app;