diff --git a/cps.py b/cps.py
index 50ab00763..d63771eb7 100755
--- a/cps.py
+++ b/cps.py
@@ -43,6 +43,7 @@ from cps.gdrive import gdrive
from cps.editbooks import editbook
from cps.remotelogin import remotelogin
from cps.error_handler import init_errorhandler
+from cps.schedule import register_jobs
try:
from cps.kobo import kobo, get_kobo_activated
@@ -78,6 +79,10 @@ def main():
app.register_blueprint(kobo_auth)
if oauth_available:
app.register_blueprint(oauth)
+
+ # Register scheduled jobs
+ register_jobs()
+
success = web_server.start()
sys.exit(0 if success else 1)
diff --git a/cps/__init__.py b/cps/__init__.py
index fa85e15c5..6a6d361a6 100644
--- a/cps/__init__.py
+++ b/cps/__init__.py
@@ -36,8 +36,6 @@ from flask_principal import Principal
from . import config_sql, logger, cache_buster, cli, ub, db
from .reverseproxy import ReverseProxied
from .server import WebServer
-from .services.background_scheduler import BackgroundScheduler
-from .tasks.thumbnail import TaskThumbnail
mimetypes.init()
@@ -117,10 +115,6 @@ def create_app():
config.config_goodreads_api_secret,
config.config_use_goodreads)
- scheduler = BackgroundScheduler()
- # Generate 100 book cover thumbnails every 5 minutes
- scheduler.add_task(user=None, task=lambda: TaskThumbnail(config=config, limit=100), trigger='interval', minutes=5)
-
return app
diff --git a/cps/helper.py b/cps/helper.py
index 6fc6b02a4..d3420a11a 100644
--- a/cps/helper.py
+++ b/cps/helper.py
@@ -52,7 +52,7 @@ except ImportError:
from . import calibre_db
from .tasks.convert import TaskConvert
-from . import logger, config, get_locale, db, ub
+from . import logger, config, get_locale, db, thumbnails, ub
from . import gdriveutils as gd
from .constants import STATIC_DIR as _STATIC_DIR
from .subproc_wrapper import process_wait
@@ -538,24 +538,27 @@ def get_cover_on_failure(use_generic_cover):
return None
-def get_book_cover(book_id):
+def get_book_cover(book_id, resolution=1):
book = calibre_db.get_filtered_book(book_id, allow_show_archived=True)
- return get_book_cover_internal(book, use_generic_cover_on_failure=True)
+ return get_book_cover_internal(book, use_generic_cover_on_failure=True, resolution=resolution)
-def get_book_cover_with_uuid(book_uuid,
- use_generic_cover_on_failure=True):
+def get_book_cover_with_uuid(book_uuid, use_generic_cover_on_failure=True):
book = calibre_db.get_book_by_uuid(book_uuid)
return get_book_cover_internal(book, use_generic_cover_on_failure)
-def get_book_cover_internal(book, use_generic_cover_on_failure):
+def get_book_cover_internal(book, use_generic_cover_on_failure, resolution=1, disable_thumbnail=False):
if book and book.has_cover:
- # if thumbnails.cover_thumbnail_exists_for_book(book):
- # thumbnail = ub.session.query(ub.Thumbnail).filter(ub.Thumbnail.book_id == book.id).first()
- # return send_from_directory(thumbnails.get_thumbnail_cache_dir(), thumbnail.filename)
- # else:
- # WorkerThread.add(None, TaskThumbnail(book, _(u'Generating cover thumbnail for: ' + book.title)))
+
+ # Send the book cover thumbnail if it exists in cache
+ if not disable_thumbnail:
+ thumbnail = get_book_cover_thumbnail(book, resolution)
+ if thumbnail:
+ if os.path.isfile(thumbnails.get_thumbnail_cache_path(thumbnail)):
+ return send_from_directory(thumbnails.get_thumbnail_cache_dir(), thumbnail.filename)
+
+ # Send the book cover from Google Drive if configured
if config.config_use_google_drive:
try:
if not gd.is_gdrive_ready():
@@ -569,6 +572,8 @@ def get_book_cover_internal(book, use_generic_cover_on_failure):
except Exception as ex:
log.debug_or_exception(ex)
return get_cover_on_failure(use_generic_cover_on_failure)
+
+ # Send the book cover from the Calibre directory
else:
cover_file_path = os.path.join(config.config_calibre_dir, book.path)
if os.path.isfile(os.path.join(cover_file_path, "cover.jpg")):
@@ -579,6 +584,16 @@ def get_book_cover_internal(book, use_generic_cover_on_failure):
return get_cover_on_failure(use_generic_cover_on_failure)
+def get_book_cover_thumbnail(book, resolution=1):
+ if book and book.has_cover:
+ return ub.session\
+ .query(ub.Thumbnail)\
+ .filter(ub.Thumbnail.book_id == book.id)\
+ .filter(ub.Thumbnail.resolution == resolution)\
+ .filter(ub.Thumbnail.expiration > datetime.utcnow())\
+ .first()
+
+
# saves book cover from url
def save_cover_from_url(url, book_path):
try:
diff --git a/cps/schedule.py b/cps/schedule.py
new file mode 100644
index 000000000..5d2c94b95
--- /dev/null
+++ b/cps/schedule.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
+# Copyright (C) 2020 mmonkey
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see