From 38a255e0696ecf985241e6774237f1ec47365e72 Mon Sep 17 00:00:00 2001 From: Vincent Kriek Date: Sun, 18 Aug 2019 21:44:19 +0200 Subject: [PATCH] Add automatic epub to kepub conversion using kepubify --- cps/admin.py | 4 ++++ cps/config_sql.py | 4 ++++ cps/helper.py | 18 +++++++++++++++--- cps/templates/config_edit.html | 12 ++++++++++++ cps/web.py | 6 +++++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index b6dcf66a..4dcd3eaa 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -303,6 +303,10 @@ def _configuration_update_helper(): _config_string("config_calibre") _config_string("config_converterpath") + _config_checkbox_int("config_automatic_kepub") + _config_string("config_kepubify_path") + _config_string("config_kepub_cache_dir") + if _config_int("config_login_type"): reboot_required |= config.config_login_type != constants.LOGIN_STANDARD diff --git a/cps/config_sql.py b/cps/config_sql.py index 809e97d8..eaefb131 100644 --- a/cps/config_sql.py +++ b/cps/config_sql.py @@ -104,6 +104,10 @@ class _Settings(_Base): config_calibre = Column(String) config_rarfile_location = Column(String) + config_automatic_kepub = Column(Boolean, default=False) + config_kepubify_path = Column(String) + config_kepub_cache_dir = Column(String) + config_updatechannel = Column(Integer, default=constants.UPDATE_STABLE) def __repr__(self): diff --git a/cps/helper.py b/cps/helper.py index e909086e..606a2c7d 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -523,7 +523,7 @@ def save_cover(img, book_path): -def do_download_file(book, book_format, data, headers): +def do_download_file(book, book_format, client, data, headers): if config.config_use_google_drive: startTime = time.time() df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format) @@ -537,7 +537,18 @@ def do_download_file(book, book_format, data, headers): if not os.path.isfile(os.path.join(filename, data.name + "." + book_format)): # ToDo: improve error handling log.error('File not found: %s', os.path.join(filename, data.name + "." + book_format)) + + if client == "kobo" and book_format == "epub": + filename = config.config_kepub_cache_dir + os.system('{0} "{1}" -o {2}'.format( + config.config_kepubify_path, + os.path.join(filename, data.name + "." + book_format), + filename)) + book_format = "kepub.epub" + headers["Content-Disposition"] = headers["Content-Disposition"].replace(".epub", ".kepub.epub") + response = make_response(send_from_directory(filename, data.name + "." + book_format)) + response.headers = headers return response @@ -756,7 +767,7 @@ def get_cc_columns(): cc = tmpcc return cc -def get_download_link(book_id, book_format): +def get_download_link(book_id, book_format, client): book_format = book_format.split(".")[0] book = db.session.query(db.Books).filter(db.Books.id == book_id).filter(common_filters()).first() if book: @@ -776,7 +787,8 @@ def get_download_link(book_id, book_format): headers["Content-Type"] = mimetypes.types_map.get('.' + book_format, "application/octet-stream") headers["Content-Disposition"] = "attachment; filename*=UTF-8''%s.%s" % (quote(file_name.encode('utf-8')), book_format) - return do_download_file(book, book_format, data, headers) + + return do_download_file(book, book_format, client, data, headers) else: abort(404) diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html index 4a5b154d..56d7e996 100644 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -309,6 +309,18 @@ {% endif %} +
+ + +
+
+ + +
+
+ + +
diff --git a/cps/web.py b/cps/web.py index 15f0f976..3b33449d 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1010,7 +1010,11 @@ def serve_book(book_id, book_format): @login_required_if_no_ano @download_required def download_link(book_id, book_format, anyname): - return get_download_link(book_id, book_format) + if (config.config_automatic_kepub and + "Kobo" in request.headers.get('User-Agent')): + client = "kobo" + + return get_download_link(book_id, book_format, client) @web.route('/send///')