From 5b1c92e7b8b5becf40194141ee94d61b1cccb771 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Sun, 17 Dec 2023 16:24:13 -0800 Subject: [PATCH] Fix a crash when fetching images during auto-tag in the gui --- comictaggerlib/coverimagewidget.py | 3 ++- comictaggerlib/imagefetcher.py | 13 ++++--------- comictaggerlib/settingswindow.py | 8 +++----- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/comictaggerlib/coverimagewidget.py b/comictaggerlib/coverimagewidget.py index 43d87f4..c49d232 100644 --- a/comictaggerlib/coverimagewidget.py +++ b/comictaggerlib/coverimagewidget.py @@ -236,7 +236,8 @@ class CoverImageWidget(QtWidgets.QWidget): self.load_default() self.cover_fetcher = ImageFetcher(self.cache_folder) ImageFetcher.image_fetch_complete = self.image_fetch_complete.emit - self.cover_fetcher.fetch(self.url_list[self.imageIndex]) + if data := self.cover_fetcher.fetch(self.url_list[self.imageIndex]): + self.cover_remote_fetch_complete(self.url_list[self.imageIndex], data) # called when the image is done loading from internet def cover_remote_fetch_complete(self, url: str, image_data: bytes) -> None: diff --git a/comictaggerlib/imagefetcher.py b/comictaggerlib/imagefetcher.py index 02bcbc0..c014ad5 100644 --- a/comictaggerlib/imagefetcher.py +++ b/comictaggerlib/imagefetcher.py @@ -70,6 +70,7 @@ class ImageFetcher: os.unlink(self.db_file) if os.path.isdir(self.cache_folder): shutil.rmtree(self.cache_folder) + self.cache_folder.mkdir(parents=True, exist_ok=True) def fetch(self, url: str, blocking: bool = False) -> bytes: """ @@ -93,7 +94,6 @@ class ImageFetcher: except Exception as e: logger.exception("Fetching url failed: %s") raise ImageFetcherException("Network Error!") from e - ImageFetcher.image_fetch_complete(url, image_data) return image_data if self.qt_available: @@ -130,18 +130,14 @@ class ImageFetcher: shutil.rmtree(self.cache_folder) os.makedirs(self.cache_folder) - con = lite.connect(self.db_file) - # create tables - with con: + with lite.connect(self.db_file) as con: cur = con.cursor() cur.execute("CREATE TABLE Images(url TEXT,filename TEXT,timestamp TEXT,PRIMARY KEY (url))") def add_image_to_cache(self, url: str, image_data: bytes | QtCore.QByteArray) -> None: - con = lite.connect(self.db_file) - - with con: + with lite.connect(self.db_file) as con: cur = con.cursor() timestamp = datetime.datetime.now() @@ -153,8 +149,7 @@ class ImageFetcher: cur.execute("INSERT or REPLACE INTO Images VALUES(?, ?, ?)", (url, filename, timestamp)) def get_image_from_cache(self, url: str) -> bytes: - con = lite.connect(self.db_file) - with con: + with lite.connect(self.db_file) as con: cur = con.cursor() cur.execute("SELECT filename FROM Images WHERE url=?", [url]) diff --git a/comictaggerlib/settingswindow.py b/comictaggerlib/settingswindow.py index c40d81e..ef29b7c 100644 --- a/comictaggerlib/settingswindow.py +++ b/comictaggerlib/settingswindow.py @@ -20,6 +20,7 @@ import logging import os import pathlib import platform +import shutil from typing import Any, cast import settngs @@ -30,11 +31,8 @@ from comicapi import utils from comicapi.genericmetadata import md_test from comictaggerlib import ctsettings from comictaggerlib.ctsettings import ct_ns -from comictaggerlib.ctversion import version from comictaggerlib.filerenamer import FileRenamer, Replacement, Replacements -from comictaggerlib.imagefetcher import ImageFetcher from comictaggerlib.ui import ui_path -from comictalker.comiccacher import ComicCacher from comictalker.comictalker import ComicTalker logger = logging.getLogger(__name__) @@ -552,8 +550,8 @@ class SettingsWindow(QtWidgets.QDialog): self.select_file(self.leRarExePath, "RAR") def clear_cache(self) -> None: - ImageFetcher(self.config[0].Runtime_Options__config.user_cache_dir).clear_cache() - ComicCacher(self.config[0].Runtime_Options__config.user_cache_dir, version).clear_cache() + shutil.rmtree(self.config[0].Runtime_Options__config.user_cache_dir, ignore_errors=True) + self.config[0].Runtime_Options__config.user_cache_dir.mkdir(parents=True, exist_ok=True) QtWidgets.QMessageBox.information(self, self.name, "Cache has been cleared.") def reset_settings(self) -> None: