-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
182 lines (168 loc) · 5.52 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const express = require('express')
const path = require('path')
const favicon = require('serve-favicon')
const bodyParser = require('body-parser')
const expressValidator=require('express-validator');
const custom =require('./public/js/custom.js');
var pdfjsLib = require('pdfjs-dist');
var app = express()
app.set('port', process.env.PORT || 3000)
app.set('env', 'development')
app.set('views', path.join(__dirname, 'views'))
app.set('view engine','ejs')
app.use(favicon(path.join(__dirname, '/public/favicon.ico')))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, 'public')))
app.use(expressValidator())
app.disable( 'x-powered-by' ) ;
pdfjsLib.workerSrc ="public/build/pdf.worker.js";
var pdfPath = 'public/web/pdfs/';
app.get('/', function (req, res) {
res.render('index');
});
app.get('/search/:page', function (req, res) {
var page = req.params.page;
console.log("now-- "+page);
res.render('search');
});
app.get('/search', function (req, res) {
var displaydata=custom.getData();
res.render('search',{keywords:displaydata[0],pdfFiles:displaydata[1],msg:"",color:""});
});
app.post('/search', function (req, res) {
searchKey=req.body.searchKey;
fileSelect=req.body.fileSelect;
console.log(searchKey+" - "+fileSelect);
req.checkBody('fileSelect','Please select a file').notEmpty();
req.checkBody('searchKey','Please enter a keyword').notEmpty();
var err=req.validationErrors();
if(err){
var displaydata=custom.getData();
console.log("empty- "+err[0].msg);
res.render('search',{keywords:displaydata[0],pdfFiles:displaydata[1],msg:err[0].msg,color:"danger"});
}
else{
var message="",color="";
var displaydata=custom.getData(1);
var varpdf=pdfPath+displaydata[1][fileSelect];
console.log("varpdf-"+varpdf);
var indices=[]
var numPages=0
pdfjsLib.getDocument(varpdf).then(function (doc) {
numPages = doc.numPages;
console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages);
console.log();
var lastPromise; // will be used to chain promises
lastPromise = doc.getMetadata().then(function (data) {
console.log('# Metadata Is Loaded');
console.log('## Info');
console.log(JSON.stringify(data.info, null, 2));
console.log();
if (data.metadata) {
console.log('## Metadata');
console.log(JSON.stringify(data.metadata.getAll(), null, 2));
console.log();
}
});
var loadPage = function (pageNum) {
return doc.getPage(pageNum).then(function (page) {
console.log('# Page ' + pageNum);
var viewport = page.getViewport(1.0 /* scale */);
console.log('Size: ' + viewport.width + 'x' + viewport.height);
console.log(" -- ");
return page.getTextContent().then(function (content) {
// Content contains lots of information about the text layout and
// styles, but we need only strings at the moment
var strings = content.items.map(function (item) {
return item.str;
});
//console.log('## Text Content');
f_strings=strings.join('');
//console.log(f_strings);
var temp=custom.getIndicesOf(searchKey,f_strings);
if(!( temp === undefined || temp.length == 0 )) {
indices.push(pageNum);
}
console.log("i--"+indices);
}).then(function () {
console.log(" - ");
});
})
};
for (var i = 1; i <= numPages; i++) {
lastPromise = lastPromise.then(loadPage.bind(null, i));
}
return lastPromise;
}).then(function () {
console.log('# End of Document');
console.log(indices);
app.locals.pages=indices;
console.log(indices.length);
if(indices.length>0){
message="Keyword found in "+indices.length+" out of "+numPages+ " pages."
color="success";
}
else{message="Keyword not found.";color="danger";}
console.log("m-"+message);
res.render('search',{keywords:displaydata[0],pdfFiles:displaydata[1],msg:message,color:color,pdfPath:varpdf.replace('public/web/', '')});
}, function (err) {
console.error('Error: ' + err);
console.log("m-"+message);
res.render('search',{keywords:displaydata[0],pdfFiles:displaydata[1],msg:err,color:"danger"});
});
///////////////////////////////////
}
});
app.post('/ajax', function (req, res) {
var result = {};
result.disable=false;
indices=app.locals.pages;
nxtPage=0;flag=false;
endPage=0;
if(req.body.action==="NEXT"){
nxtPage=req.body.currPage+1;
endPage=indices[indices.length-1];
for(i=nxtPage;i<=endPage;i++)
{
console.log(i);
if(indices.includes(i))
{
flag=true;
result.pge=i;
break;
}
}
}
else if(req.body.action==="PREV"){
nxtPage=req.body.currPage-1;
for(i=nxtPage;i>endPage;i--)
{
console.log(i);
if(indices.includes(i))
{
flag=true;
result.pge=i;
break;
}
}
}
if(flag){result.disable=false;}else{result.disable=true;}
console.log('body: ' + JSON.stringify(req.body));
console.log('result: ' + JSON.stringify(result));
res.send(result);
});
//Route Files
let upload=require('./routes/upload');
app.use('/upload',upload);
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'))
})
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
});