-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (52 loc) · 1.77 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
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const { PythonShell } = require('python-shell');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 3000;
const upload = multer({
limits: {
fileSize: 100 * 1024 * 1024,
},
'dest': './upload',
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
app.post('/blur', upload.single('qrcode'), (req, res) => {
if (!req.file) {
return res.status(400).send('File missing!');
}
const options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
args: ['-i', `upload/${req.file.filename}`, '-o', `upload/${req.file.filename}.new.jpg`] //An argument which can be accessed in the script using sys.argv[1]
};
PythonShell.run('./main.py', options, async function (err, result){
if (err) {
console.error(err);
res.status(500).send('Unknown server error');
return;
}
if (result && result[0] === 'No QR code found') {
res.header('Content-Type', 'image/jpeg').sendFile(`${__dirname}/upload/${req.file.filename}`, () => {
fs.unlink(`${__dirname}/upload/${req.file.filename}`, (err => {
if (err) console.error(err);
}));
});
} else {
res.sendFile(`${__dirname}/upload/${req.file.filename}.new.jpg`, () => {
fs.unlink(`${__dirname}/upload/${req.file.filename}.new.jpg`, (err => {
if (err) console.error(err);
}))
fs.unlink(`${__dirname}/upload/${req.file.filename}`, (err => {
if (err) console.error(err);
}));
});
}
});
});
app.listen(port, function () {
console.log('Server is running on PORT',port);
});