-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
73 lines (64 loc) · 2.03 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
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require('express');
var router = express.Router();
var app = express();
var fs = require("fs");
var port = process.env.PORT || 8080;
var multer = require('multer');
var path = require('path');
var p_ = require('path');
var functions = require('./js/functions')
//Variables
var path = __dirname + '/views/';
var OCRText = [];
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads/')
},
filename: function (req, file, cb) {
var ext = require('path').extname(file.originalname);
ext = ext.length>1 ? ext : "." + require('mime').extension(file.mimetype);
require('crypto').pseudoRandomBytes(16, function (err, raw) {
cb(null, (err ? undefined : raw.toString('hex') ) + ext);
});
}
});
var upload = multer({storage: storage});
app.use(express.static("views"));
app.post('/file_upload', upload.single('Image'), function (req,res,err) {
console.log(req.file);
var imgLocation = req.file.path
console.log(imgLocation);
var imagePath = __dirname + '/' + imgLocation;
app.get('/image',function(req,res) {
var img = fs.readFileSync(imagePath);
res.writeHead(200,{'Content-Type': 'image/png'});
res.end(img,'png');
})
res.sendFile(path + 'index.html');
functions.RunTesseract(req);
});
app.get('/retrive', function(req,res){
// Download mp3
var audioLocation = './audio/Text.mp3'
res.download(audioLocation);
// Cleanup images
fs.readdir('uploads', (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(p_.join('uploads', file), err => {
if (err) throw err;
});
}
});
// Clean mp3 file
fs.readdir('audio', (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(p_.join('audio', file), err => {
if (err) throw err;
});
}
});
})
app.listen(port);
console.log('The server is running on: ' + port);