summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2012-09-20 14:31:34 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2012-09-20 14:31:34 (GMT)
commitd041afa83bcea5b439907861765ffd87ced1a9fb (patch)
tree65ea4989ad05d470b5c56096aff3b322969ddba1
parent3830c8392b1ce1037742f2e40ce86b0494c1f9a6 (diff)
downloadpfwb-ged-proto-d041afa83bcea5b439907861765ffd87ced1a9fb.zip
pfwb-ged-proto-d041afa83bcea5b439907861765ffd87ced1a9fb.tar.gz
pfwb-ged-proto-d041afa83bcea5b439907861765ffd87ced1a9fb.tar.bz2
add possibility to add files
-rw-r--r--server/index.html9
-rwxr-xr-xserver/search_server.py60
-rw-r--r--server/upload.html38
3 files changed, 95 insertions, 12 deletions
diff --git a/server/index.html b/server/index.html
index 09c377b..df7eb64 100644
--- a/server/index.html
+++ b/server/index.html
@@ -13,7 +13,12 @@ function display_results(data) {
var items = [];
$('#debug').html(data['qtime']);
$.each(data['results'], function(key, val) {
- items.push('<li><span class="score">' + Math.min(5, parseInt(val['score']*5)) + '</span> <a href="pdfs/' + val['id'] + '">' + val['id'] + '</a></li>');
+ if (val['id'].indexOf('N-') == 0) {
+ url = 'pdfs-N/' + val['id'];
+ } else {
+ url = 'pdfs/' + val['id'];
+ }
+ items.push('<li><span class="score">' + Math.min(5, parseInt(val['score']*5)) + '</span> <a href="' + url + '">' + val['id'] + '</a></li>');
});
$('#results').html($('<ul/>', {html: items.join('')}));
var orig_offset = $('#offset').val()*1;
@@ -29,7 +34,6 @@ function display_results(data) {
}
$('#offset').val(orig_offset);
if ($('#offset').val()*1 + $('#count').val()*1 < data['hits']*1) {
- console.log('there');
var next_start = $('#offset').val()*1 + NUM_RESULTS;
$('#offset').val(next_start);
link = '<a class="nav next" href="?' + $('form').serialize() + '">Résultats suivants</a>';
@@ -91,6 +95,7 @@ $(function() {
<div id="appbar">
<h2>Prototype de moteur de recherche - GED PFWB</h2>
+ <a href="upload.html">Ajouter un fichier</a>
</div>
<form>
diff --git a/server/search_server.py b/server/search_server.py
index 33a11af..7853dea 100755
--- a/server/search_server.py
+++ b/server/search_server.py
@@ -32,6 +32,7 @@ import json
import cairo
import poppler
import hashlib
+import subprocess
class SearchHandler(SCGIHandler):
debug = False
@@ -57,14 +58,24 @@ class SearchHandler(SCGIHandler):
print 'Request received at', time.strftime('[%Y-%m-%d %H:%M]')
print ' - body size:', bodysize
- if bodysize != 0:
- return self.error_page(output, 'this server only supports GET')
-
uri = env.get('REQUEST_URI')[len('/search/'):]
if self.debug:
print ' - uri:', uri
- if uri.startswith('pdfs/'):
+ if uri == 'upload':
+ if bodysize == 0:
+ return self.redirect_home(env, output)
+ hdr = {'content-type': env.get('CONTENT_TYPE')}
+ field = cgi.FieldStorage(input, headers=hdr, environ=env).value[0]
+ filename = 'pdfs-N/N-%s' % os.path.basename(field.filename)
+ fd = file(filename, 'w')
+ fd.write(field.value)
+ fd.close()
+ if not self.index(filename=filename, id=os.path.basename(filename)):
+ return self.error_page(output, 'failed to index')
+ return self.redirect_home(env, output)
+
+ if uri.startswith('pdfs/') or uri.startswith('pdfs-N/'):
preview_filename = '/tmp/preview-%s.png' % hashlib.sha1(uri).hexdigest()
if uri.endswith('.png'):
uri = uri[:-4]
@@ -91,17 +102,13 @@ class SearchHandler(SCGIHandler):
return
if not env.get('QUERY_STRING'):
- # homepage
- print >> output, 'Content-type: text/html'
- print >> output, ''
- output.write(file('search.html').read())
- return
+ return self.redirect_home(env, output)
qs = cgi.parse_qs(env.get('QUERY_STRING'))
try:
query = unicode(qs['q'][0], 'utf-8')
except KeyError:
- return self.error_page('missing q parameter')
+ return self.error_page(output, 'missing q parameter')
if 'count' in qs:
count = int(qs['count'][0])
else:
@@ -123,6 +130,39 @@ class SearchHandler(SCGIHandler):
print >> output, ''
json.dump(response, output)
+ def redirect_home(self, env, output):
+ # go to homepage
+ root_url = 'http://%s' % env.get('SERVER_NAME')
+ if env.get('SERVER_PORT') != '80':
+ root_url = root_url + ':%s' % env.get('SERVER_PORT')
+
+ print >> output, 'Status: 302 Redirect'
+ print >> output, 'Content-type: text/plain'
+ print >> output, 'Location: ' + root_url
+ print >> output, ''
+ print >> output, 'Redirection'
+
+ def index(self, filename, id):
+ cmd = ['pdftotext', filename, '-']
+ try:
+ p = subprocess.Popen(cmd,
+ close_fds=True,
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ except OSError, e:
+ if self.debug:
+ print 'E: error running pdftotext'
+ return False
+ stdout, stderr = p.communicate()
+ if p.returncode != 0:
+ if self.debug:
+ print 'E: error running pdftotext (rc:%d)' % p.returncode
+ return False
+ self.solr.add([{'id': id, 'text': unicode(stdout, 'utf-8')}])
+ self.solr.commit()
+ return True
+
def error_page(self, output, message):
print >> output, 'Content-type: text/plain'
print >> output, ''
diff --git a/server/upload.html b/server/upload.html
new file mode 100644
index 0000000..1f74944
--- /dev/null
+++ b/server/upload.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html lang="fr">
+ <head>
+ <meta charset="utf-8"/>
+ <link rel="stylesheet" type="text/css" media="all" href="style.css"/>
+ <script src="js/jquery-1.7.2.min.js"></script>
+ <title>Recherche</title>
+ </head>
+ <body>
+
+ <div id="wrap">
+ <div id="header">
+ <h1>PFWB</h1>
+ <span>Prototype</span>
+ </div>
+ <div id="splash">
+ </div>
+ <div id="content">
+
+ <div id="appbar">
+ <h2>Prototype de moteur de recherche - GED PFWB</h2>
+ <a href="./">Retourner à la recherche</a>
+ </div>
+
+ <form method="post" action="search/upload" enctype="multipart/form-data">
+ Fichier à ajouter : <input name="file" type="file"/>
+ <input type="submit" value="Uploader"/>
+ </form>
+
+ <div id="footer">
+ Entr'ouvert — 2012
+ </div>
+ </div>
+
+
+ </body>
+</html>
+