Fix a crash when fetching images during auto-tag in the gui

This commit is contained in:
Timmy Welch 2023-12-17 16:24:13 -08:00
parent 9571020217
commit 5b1c92e7b8
3 changed files with 9 additions and 15 deletions

View File

@ -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:

View File

@ -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])

View File

@ -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: