From 7ab8a5877be62e0dc3c8ed87f82fdabccc5660bb Mon Sep 17 00:00:00 2001 From: OzzieIsaacs <ozzie.fernandez.isaacs@gmail.com> Date: Thu, 25 May 2017 08:46:33 +0200 Subject: [PATCH] read PDF/TXT without temporary files (#197) --- cps/templates/readpdf.html | 2 +- cps/templates/readtxt.html | 2 +- cps/web.py | 32 ++++++++++++++++++++------------ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/cps/templates/readpdf.html b/cps/templates/readpdf.html index a7a79dbb..68b19d6e 100644 --- a/cps/templates/readpdf.html +++ b/cps/templates/readpdf.html @@ -48,7 +48,7 @@ See https://github.com/adobe-type-tools/cmap-resources <!--<script src="pdf.js"></script>--> <script type="text/javascript"> - var DEFAULT_URL = "{{ url_for('static', filename=pdffile) }}"; + var DEFAULT_URL = "{{ url_for('serve_book', book_id=pdffile,book_format='pdf') }}"; var PDFWORKER_LOCATION="{{ url_for('static', filename='js/libs/pdf.worker.js') }}"; // var IMAGE_LOCATION="{{ url_for('static', filename='css/../images') }}"; var IMAGE_LOCATION="{{ url_for('static', filename='/images/') }}"; diff --git a/cps/templates/readtxt.html b/cps/templates/readtxt.html index 5ea0c113..f45bd702 100644 --- a/cps/templates/readtxt.html +++ b/cps/templates/readtxt.html @@ -109,7 +109,7 @@ $("#area").width($("#area").width()); $("#content").width($("#content").width()); //bind text - $("#content").load("{{ url_for('static', filename=txtfile) }}",function(textStr) { + $("#content").load("{{ url_for('serve_book', book_id=txtfile,book_format='txt') }}",function(textStr) { $(this).height($(this).parent().height()*0.95); $(this).text(textStr); }); diff --git a/cps/web.py b/cps/web.py index 6f5dd8f9..73f87d53 100755 --- a/cps/web.py +++ b/cps/web.py @@ -1581,6 +1581,24 @@ def get_cover(cover_path): else: return send_from_directory(os.path.join(config.config_calibre_dir, cover_path), "cover.jpg") +@app.route("/show/<book_id>/<book_format>") +@login_required_if_no_ano +def serve_book(book_id,book_format): + book_format = book_format.split(".")[0] + book = db.session.query(db.Books).filter(db.Books.id == book_id).first() + data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first() + app.logger.info(data.name) + if config.config_use_google_drive: + headers = Headers() + try: + headers["Content-Type"] = mimetypes.types_map['.' + book_format] + except KeyError: + headers["Content-Type"] = "application/octet-stream" + df = gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, data.name + "." + book_format) + return do_gdrive_download(df, headers) + else: + return send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format) + @app.route("/opds/thumb_240_240/<path:book_id>") @app.route("/opds/cover_240_240/<path:book_id>") @@ -1680,19 +1698,9 @@ def read_book(book_id, book_format): zfile.close() return render_title_template('read.html', bookid=book_id, title=_(u"Read a Book")) elif book_format.lower() == "pdf": - all_name = str(book_id) + "/" + book.data[0].name + ".pdf" - tmp_file = os.path.join(book_dir, book.data[0].name) + ".pdf" - if not os.path.exists(tmp_file): - pdf_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".pdf" - copyfile(pdf_file, tmp_file) - return render_title_template('readpdf.html', pdffile=all_name, title=_(u"Read a Book")) + return render_title_template('readpdf.html', pdffile=book_id, title=_(u"Read a Book")) elif book_format.lower() == "txt": - all_name = str(book_id) + "/" + book.data[0].name + ".txt" - tmp_file = os.path.join(book_dir, book.data[0].name) + ".txt" - if not os.path.exists(all_name): - txt_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".txt" - copyfile(txt_file, tmp_file) - return render_title_template('readtxt.html', txtfile=all_name, title=_(u"Read a Book")) + return render_title_template('readtxt.html', txtfile=book_id, title=_(u"Read a Book")) elif book_format.lower() == "cbr": all_name = str(book_id) + "/" + book.data[0].name + ".cbr" tmp_file = os.path.join(book_dir, book.data[0].name) + ".cbr"