-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuploadRoute.js
41 lines (35 loc) · 1.23 KB
/
uploadRoute.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
const router = require('express').Router();
var multer = require('multer');
const path = require("path");
const { spawn } = require('child_process');
const fs = require('fs');
const storage = multer.diskStorage({
destination: "./images",
filename: function(req, file, cb){
cb(null,Date.now() + path.extname(file.originalname));
}
});
const upload = multer({
storage: storage,
}).single("myImage");
router.post("/upload", (req, res)=>{
upload(req, res, (err) => {
if(!err){
const python = spawn('python3', ['client.py',req.file['path']]);
python.stdout.on('data', function (data) {
fs.readFile('captions.json', function (errr, info) {
var json = JSON.parse(info);
let caption=data.toString()+`${req.body['tag']}`
caption=caption.replace(/(\r\n|\n|\r)/gm, " ");
json.push({"id":req.file["filename"],"caption":caption});
fs.writeFile("captions.json", JSON.stringify(json), function(error){
if (error) throw error;
console.log(caption);
});
})
return res.sendStatus(200).end();
});
}
});
});
module.exports=router;