From 399dffba5a0c806628fd36b2944a95e13bb60576 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sat, 3 Nov 2018 18:37:38 +0100 Subject: [PATCH] log file name in case of filename not found (#691) --- cps/helper.py | 6 +++++- cps/web.py | 19 ++++++------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index 3c7c80a6..597198fd 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -377,7 +377,11 @@ def do_download_file(book, book_format, data, headers): else: abort(404) else: - response = make_response(send_from_directory(os.path.join(ub.config.config_calibre_dir, book.path), data.name + "." + book_format)) + filename = os.path.join(ub.config.config_calibre_dir, book.path) + if not os.path.isfile(os.path.join(filename, data.name + "." + book_format)): + # ToDo: improve error handling + web.app.logger.error('File not found: %s' % os.path.join(filename, data.name + "." + book_format)) + response = make_response(send_from_directory(filename, data.name + "." + book_format)) response.headers = headers return response diff --git a/cps/web.py b/cps/web.py index c6b8a939..6e52da2b 100644 --- a/cps/web.py +++ b/cps/web.py @@ -2255,7 +2255,8 @@ def read_book(book_id, book_format): extensionList = ["cbt","cbz"] for fileext in extensionList: if book_format.lower() == fileext: - return render_title_template('readcbr.html', comicfile=book_id, extension=fileext, title=_(u"Read a Book"), book=book) + return render_title_template('readcbr.html', comicfile=book_id, + extension=fileext, title=_(u"Read a Book"), book=book) flash(_(u"Error opening eBook. File does not exist or file is not accessible."), category="error") return redirect(url_for("index"))''' @@ -2266,7 +2267,8 @@ def read_book(book_id, book_format): def get_download_link(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() + data = db.session.query(db.Data).filter(db.Data.book == book.id)\ + .filter(db.Data.format == book_format.upper()).first() if data: # collect downloaded books only for registered user and not for anonymous user if current_user.is_authenticated: @@ -2280,18 +2282,9 @@ def get_download_link(book_id, book_format): headers["Content-Type"] = mimetypes.types_map['.' + book_format] except KeyError: headers["Content-Type"] = "application/octet-stream" - headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')), book_format) + headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')), + book_format) return helper.do_download_file(book, book_format, data, headers) - #if config.config_use_google_drive: - # df = gdriveutils.getFileFromEbooksFolder(book.path, '%s.%s' % (data.name, book_format)) - # if df: - # return do_gdrive_download(df, headers) - # else: - # abort(404) - #else: - # response = make_response(send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format)) - # response.headers = headers - # return response else: abort(404)