From bc96ff9a3985777f009e2a1b3f49e4e04bd00656 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 24 Apr 2022 11:14:39 +0200 Subject: [PATCH] Enable scheduled side deleting of thumbnails of deleted books --- cps/schedule.py | 3 ++- cps/tasks/thumbnail.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cps/schedule.py b/cps/schedule.py index 5500f1a9..1da52d6f 100644 --- a/cps/schedule.py +++ b/cps/schedule.py @@ -21,7 +21,7 @@ import datetime from . import config, constants from .services.background_scheduler import BackgroundScheduler from .tasks.database import TaskReconnectDatabase -from .tasks.thumbnail import TaskGenerateCoverThumbnails, TaskGenerateSeriesThumbnails +from .tasks.thumbnail import TaskGenerateCoverThumbnails, TaskGenerateSeriesThumbnails, TaskClearCoverThumbnailCache from .services.worker import WorkerThread @@ -35,6 +35,7 @@ def get_scheduled_tasks(reconnect=True): # Generate all missing book cover thumbnails if config.schedule_generate_book_covers: tasks.append([lambda: TaskGenerateCoverThumbnails(), 'generate book covers']) + tasks.append([lambda: TaskClearCoverThumbnailCache(0), 'delete superfluous book covers']) # Generate all missing series thumbnails if config.schedule_generate_series_covers: diff --git a/cps/tasks/thumbnail.py b/cps/tasks/thumbnail.py index d11ef50c..f6015420 100644 --- a/cps/tasks/thumbnail.py +++ b/cps/tasks/thumbnail.py @@ -16,7 +16,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from __future__ import division, print_function, unicode_literals import os from urllib.request import urlopen @@ -449,18 +448,24 @@ class TaskClearCoverThumbnailCache(CalibreTask): super(TaskClearCoverThumbnailCache, self).__init__(task_message) self.log = logger.create() self.book_id = book_id + self.calibre_db = db.CalibreDB(expire_on_commit=False) self.app_db_session = ub.get_new_session_instance() self.cache = fs.FileSystem() def run(self, worker_thread): if self.app_db_session: - if self.book_id > 0: # make sure all thumbnails aren't getting deleted due to a bug + if self.book_id == 0: # delete superfluous thumbnails + thumbnails = (self.calibre_db.session.query(ub.Thumbnail) + .join(db.Books, ub.Thumbnail.entity_id == db.Books.id, isouter=True) + .filter(db.Books.id == None) + .all()) + elif self.book_id > 0: # make sure single book is selected thumbnails = self.get_thumbnails_for_book(self.book_id) + if self.book_id < 0: + self.delete_all_thumbnails() + else: for thumbnail in thumbnails: self.delete_thumbnail(thumbnail) - else: - self.delete_all_thumbnails() - self._handleSuccess() self.app_db_session.remove() @@ -472,7 +477,6 @@ class TaskClearCoverThumbnailCache(CalibreTask): .all() def delete_thumbnail(self, thumbnail): - # thumbnail.expiration = datetime.utcnow() try: self.cache.delete_cache_file(thumbnail.filename, constants.CACHE_TYPE_THUMBNAILS) self.app_db_session \