From 4f6bbfa8b8e0b100dc22e71881417e9fb9bc5991 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 11 Nov 2023 15:00:12 +0100 Subject: [PATCH] Little refactoring do_download --- cps/helper.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index bd219594..0e0c4de3 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -1013,38 +1013,33 @@ def tags_filters(): # in all calls the email address is checked for validity def check_valid_domain(domain_text): sql = "SELECT * FROM registration WHERE (:domain LIKE domain and allow = 1);" - result = ub.session.query(ub.Registration).from_statement(text(sql)).params(domain=domain_text).all() - if not len(result): + if not len(ub.session.query(ub.Registration).from_statement(text(sql)).params(domain=domain_text).all()): return False sql = "SELECT * FROM registration WHERE (:domain LIKE domain and allow = 0);" - result = ub.session.query(ub.Registration).from_statement(text(sql)).params(domain=domain_text).all() - return not len(result) + return not len(ub.session.query(ub.Registration).from_statement(text(sql)).params(domain=domain_text).all()) def get_download_link(book_id, book_format, client): book_format = book_format.split(".")[0] book = calibre_db.get_filtered_book(book_id, allow_show_archived=True) - data1= "" if book: data1 = calibre_db.get_book_format(book.id, book_format.upper()) + if data1: + # collect downloaded books only for registered user and not for anonymous user + if current_user.is_authenticated: + ub.update_download(book_id, int(current_user.id)) + file_name = book.title + if len(book.authors) > 0: + file_name = file_name + ' - ' + book.authors[0].name + file_name = get_valid_filename(file_name, replace_whitespace=False) + headers = Headers() + headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream") + headers["Content-Disposition"] = "attachment; filename=%s.%s; filename*=UTF-8''%s.%s" % ( + quote(file_name), book_format, quote(file_name), book_format) + return do_download_file(book, book_format, client, data1, headers) else: log.error("Book id {} not found for downloading".format(book_id)) - abort(404) - if data1: - # collect downloaded books only for registered user and not for anonymous user - if current_user.is_authenticated: - ub.update_download(book_id, int(current_user.id)) - file_name = book.title - if len(book.authors) > 0: - file_name = file_name + ' - ' + book.authors[0].name - file_name = get_valid_filename(file_name, replace_whitespace=False) - headers = Headers() - headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream") - headers["Content-Disposition"] = "attachment; filename=%s.%s; filename*=UTF-8''%s.%s" % ( - quote(file_name), book_format, quote(file_name), book_format) - return do_download_file(book, book_format, client, data1, headers) - else: - abort(404) + abort(404) def clear_cover_thumbnail_cache(book_id):