diff options
Diffstat (limited to 'web.py')
-rw-r--r-- | web.py | 33 |
1 files changed, 19 insertions, 14 deletions
@@ -1,5 +1,6 @@ #!/usr/bin/python2.6 # coding: utf-8 +import os from StringIO import StringIO from whoosh.index import open_dir from whoosh.qparser import QueryParser @@ -21,23 +22,27 @@ def do_index(): @app.route("/download/file/<int:docnum>") def do_download_file(docnum): - document = index.reader().stored_fields(docnum) - filepath = document['path'] - return werkzeug.Response(open(filepath, "r"), mimetype="application/pdf") + document = index.reader().stored_fields(docnum) + r = werkzeug.Response(open(document['path'], "r"), mimetype="application/pdf",) + r.headers.add('Content-Disposition', 'attachment', + filename=os.path.basename(document['path'])) + return r @app.route("/download/page/<int:docnum>", methods=["GET"]) def do_download_page(docnum): - document = index.reader().stored_fields(docnum) - filepath = document['path'] - pagenumber = document['pagenumber'] - inputfile = pyPdf.PdfFileReader(file(filepath, 'r')) - page = inputfile.getPage(pagenumber) - outbuf = StringIO() - outfile = pyPdf.PdfFileWriter() - outfile.addPage(page) - outfile.write(outbuf) - outbuf.seek(0) - return werkzeug.Response(outbuf, mimetype="application/pdf") + document = index.reader().stored_fields(docnum) + inputfile = pyPdf.PdfFileReader(file(document['path'], 'r')) + page = inputfile.getPage(document['pagenumber']) + outbuf = StringIO() + outfile = pyPdf.PdfFileWriter() + outfile.addPage(page) + outfile.write(outbuf) + outbuf.seek(0) + r= werkzeug.Response(outbuf, mimetype="application/pdf") + client_filename = os.path.basename(document['path'])[:-3] + client_filename += u".Page-{0}".format(document['pagenumber']) + r.headers.add('Content-Disposition', 'attachment', filename=client_filename) + return r @app.route("/search/skip=<int:skip>/<path:term>",methods=["GET"]) @app.route("/search/<path:term>",methods=["GET"]) |