-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
79 lines (70 loc) · 2.43 KB
/
main.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
import express from "express"
import path from "path"
import favicon from "serve-favicon"
import download from "image-downloader"
import multer from "multer"
import once from "once"
const app = express()
const port = process.env.PORT || 3000
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/public'))
app.use(favicon(path.join(__dirname, 'public', 'img', 'favicon.ico')))
const storage = multer.diskStorage(
{
destination: './public/uploads/',
filename: function (_req, file, cb) { cb(null, file.originalname) }
}
)
const upload = multer({ storage: storage })
app.get('/', (_req, res) => { res.render("index") })
app.get('/getImage', (req, res) => {
let imageUrl = req.query.url
if (!imageUrl) { return }
let imageFileName = `/memes/${new Date().getTime()}.jpg`
let opts = {
url: imageUrl,
dest: `public/${imageFileName}`
}
download.image(opts).then(() => res.json({ path: imageFileName }))
})
// upload file route with handling of non-images
app.post('(/upload)', upload.single("file"), (req, res, next) => {
if (!req.file.mimetype.startsWith('image/')) {
return res.status(422).json({ error: 'The uploaded file must be an image' })
}
return res.status(200).send(req.file)
})
app.get('/decodeImage', async (req, res) => {
console.log("decode image hit!")
let imgUrl = req.query.url
if (!imgUrl) return
console.log(`decoding ${imgUrl}`)
// hack, only support uploading images so they're all in the uploads folder
let upImgUrl = `./public${imgUrl.substring(imgUrl.indexOf("/uploads"))}`
let imgData = await getQR(upImgUrl)
res.status(200)
res.send(imgData)
res.end()
function getQR(url) {
return new Promise((resolve, reject) => {
console.log("getQR triggered")
console.log("decoding" + url)
let output = ''
let spawn = require('child_process').spawn
let py = spawn('pipenv', ['run', './vacdec', `${url}`])
py.stdin.setEncoding = 'utf-8'
py.stdout.on('data', (data) => { output += data.toString() })
py.stderr.on('data', (data) => { console.log('error:' + data) })
py.stdout.on('end', async function (code) {
console.log("finished")
resolve(output)
})
once(py, 'close')
return
})
}
})
// 404 route, for all routes not hit by any other endpoints
app.get('(/*)?', (_req, res) => res.render("404"))
// Start App
app.listen(port, () => console.log(`VACDECWEB running, listening on port ${port}!`))