-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
151 lines (129 loc) · 4.44 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require("express");
const multer = require("multer");
const ejs = require("ejs");
const path = require("path");
const { createPool } = require("mysql");
const { spawn } = require("child_process");
const bodyParser = require('body-parser');
const { dirname } = require("path");
const pool = createPool({
Host: "localhost",
Port: 3360,
user: "root",
password: "Anurag@123",
database: "sys",
});
const app = express();
// pool.query("select * from files", (err, result, fields) => {
// if (err) {
// return console.log(err);
// }
// return console.log(result);
// });
module.exports = pool;
app.use(express.static(__dirname +"/public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public/uploads");
},
filename: (req, file, cb) => {
cb(null, file.originalname);
},
});
const upload = multer({ storage });
app.get("/", (req, res) => {
res.render("index", { result: " ", imgsrc: " " });
});
app.post("/upload", upload.single("file"), (req, res) => {
const fileName = "./uploads/" + req.file.originalname;
const fileurl = "./public/uploads/" + req.file.originalname;
var imgsrc = "http://127.0.0.1:3000/public/uploads/" + req.file.filename;
var insertData = "INSERT INTO FILES(file_src)VALUES(?)";
// pool.query(insertData, [imgsrc], (err, result) => {
// if (err) throw err;
// console.log("file uploaded");
// });
const extension = path.extname(fileName);
// console.log(extension);
var dataToSend;
var python;
if (extension === ".jpeg" || extension === ".jpg" || extension === ".png") {
// python = spawn("python", ["./scripts/text.py", fileurl]); //for images of format jpg, jpeg, png
python = spawn("python", ["./scripts/cmdimg.py", fileurl]); //for images of format jpg, jpeg, png
} else if (extension === ".pdf") {
python = spawn("python", ["./scripts/pdftoword.py", fileurl]); //for pdf
} else if (extension === ".docx") {
python = spawn("python", ["./scripts/doc.py", fileurl]); //for docx
}
else if (extension === ".csv") {
python = spawn("python", ["./scripts/csv_try.py", fileurl]); //for docx
}
python.stdout.on("data", function (data) {
dataToSend = data.toString();
console.log(dataToSend);
});
python.on("close", (code) => {
// console.log(dataToSend);
res.render("index", { result: dataToSend, imgsrc: fileName });
var state = "INSERT INTO FILES(file_src,FILE_TEXT)VALUES(?,?)";
pool.query(state, [fileName, req.file.originalname], (err, result) => {
if (err) throw err;
const id = result.insertId;
python = spawn("python", ["./scripts/database.py", dataToSend, id]);
python.stdout.on("data", function (data) {
indexeddata = data.toString();
console.log(indexeddata);
});
console.log("uploaded");
});
});
});
app.get("/upload", (req, res)=>{
res.render("upload");
});
app.post("/", (req, res)=>{
const wordtosearch = req.body.name[0];
console.log(wordtosearch)
var arr = [];
const state = "SELECT word_id FROM inverted_index WHERE word_name = ?"
pool.query(state, [wordtosearch], (err, result) => {
try {
const result1 = Object.values(JSON.parse(JSON.stringify(result)));
const result2 = result1[0].word_id.replace(/[\])}[{(]/g, '').split(',');
console.log(result2);
const state1 = "SELECT file_text FROM files WHERE id IN (?)";
var i=0;
// result2.forEach((v) => pool.query(state1,[v],(err, result)=>{
// if(err) throw err;
// const result_name = Object.values(JSON.parse(JSON.stringify(result)));
// if(result_name!==[]){
// console.log(result_name[0].file_text);
// arr.push(result_name[0].file_text);
// i=i+1;
// }
// // if(i==)
// }));
pool.query(state1,[result2],(err, result)=>{
if(err) throw err;
const result_name = Object.values(JSON.parse(JSON.stringify(result)));
// if(result_name!==[]){
console.log(result_name);
// }
// if(i==)
});
console.log(arr);
console.log("---------------------");
res.render("index");
} catch (error) {
console.log("Word not Present!");
}
// .replace(/[\])}[{(]/g, '').split(',')
});
// res.render("search",{ result: arraydum });
});
const server = app.listen(3000, () => {
console.log("Server started on port 3000");
});
server.timeout = 300000;