Skip to content

Commit

Permalink
fix: remove folder thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed Nov 28, 2024
1 parent 44b1d94 commit ea21d21
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
62 changes: 35 additions & 27 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,14 @@ def get_thumbnail(self, entry: Entry, size: ThumbSize) -> tuple[bool, Image.Imag

return False, None

def get_folder_thumbnail_path(self, folder_id: int) -> Path:
"""Return path to thumbnail for given folder."""
assert isinstance(self.storage_path, Path)
return self.storage_path / self.DATADIR / "thumbnails" / str(folder_id)

def get_thumbnail_path(self, entry: Entry, size: ThumbSize) -> Path:
"""Return path to thumbnail for given Entry."""
assert isinstance(self.storage_path, Path)
return (
self.storage_path
/ self.DATADIR
/ "thumbnails"
/ str(entry.folder_id)
/ size.name
/ f"{entry.id}.png"
)
return self.get_folder_thumbnail_path(entry.folder_id) / size.name / f"{entry.id}.png"

def save_thumbnail(self, entry: Entry, size: ThumbSize, image: Image.Image):
"""Save thumbnail for given Entry."""
Expand Down Expand Up @@ -219,9 +216,7 @@ def init_db(self, use_migrations: bool, connection_string: URL, is_new: bool):
conn.execute(text("DELETE FROM tags WHERE id = 999"))
conn.commit()

def open_library(
self, storage_path: Path | str, library_name: str | None = None, use_migrations: bool = True
) -> LibraryStatus:
def open_library(self, storage_path: Path | str, use_migrations: bool = True) -> LibraryStatus:
if storage_path == ":memory:":
self.storage_path = storage_path
is_new = True
Expand Down Expand Up @@ -281,21 +276,21 @@ def _fk_pragma_on_connect(dbapi_con, con_record):
logger.debug("preference already exists", pref=pref)
session.rollback()

for field in _FieldID:
try:
session.add(
ValueType(
key=field.name,
name=field.value.name,
type=field.value.type,
position=field.value.id,
is_default=field.value.is_default,
if is_new:
for field in _FieldID:
try:
session.add(
ValueType(
key=field.name,
name=field.value.name,
type=field.value.type,
position=field.value.id,
is_default=field.value.is_default,
)
)
)
session.commit()
except IntegrityError:
logger.debug("ValueType already exists", field=field)
session.rollback()
session.commit()
except IntegrityError:
session.rollback()

"""
# check if folder matching current path exists already
Expand Down Expand Up @@ -408,16 +403,29 @@ def get_folders(self) -> list[Folder]:
return folders

def remove_folder(self, folder: Folder) -> bool:
folder_id = folder.id

with Session(self.engine) as session:
session.delete(folder)
try:
session.commit()
return True
except IntegrityError as e:
logger.exception(e)
session.rollback()
return False

try:
thumb_path = self.get_folder_thumbnail_path(folder_id)
logger.info("removing folder thumbnails", path=thumb_path)
assert self.storage_path
assert self.DATADIR

shutil.rmtree(thumb_path)
except Exception as e:
logger.exception(e)

return True

@property
def entries_count(self) -> int:
with Session(self.engine) as session:
Expand Down
24 changes: 19 additions & 5 deletions tagstudio/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import Mock, patch

import pytest
import structlog

CWD = pathlib.Path(__file__).parent
# this needs to be above `src` imports
Expand All @@ -16,6 +17,8 @@
from src.core.library.alchemy.library import MissingFieldAction
from src.qt.ts_qt import QtDriver

logger = structlog.get_logger()


@pytest.fixture
def cwd():
Expand All @@ -24,16 +27,24 @@ def cwd():

@pytest.fixture
def library(request):
"""WARNING: the library content is removed on end of the test"""
# when no param is passed, use the default
folder_path = "/dev/null/"
storage_path = ":memory:"
temp_dir = None
if hasattr(request, "param"):
if isinstance(request.param, TemporaryDirectory):
folder_path = request.param.name
else:
folder_path = request.param
assert isinstance(request.param, TemporaryDirectory)
temp_dir = request.param
storage_path = folder_path = pathlib.Path(temp_dir.name)
# check the folder is empty
if storage_path.exists() and list(storage_path.iterdir()):
raise ValueError(
f"Temporary directory {storage_path} is not empty. "
"Please use a clean temporary directory for the test."
)

lib = Library()
status = lib.open_library(":memory:", folder_path, use_migrations=False)
status = lib.open_library(storage_path, use_migrations=False)
assert status.success

tag = Tag(
Expand Down Expand Up @@ -113,6 +124,9 @@ def library(request):

yield lib

if isinstance(temp_dir, TemporaryDirectory):
temp_dir.cleanup()


@pytest.fixture
def entry_min(library):
Expand Down
8 changes: 8 additions & 0 deletions tagstudio/tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,21 @@ class TestPrefs(DefaultEnum):
assert TestPrefs.BAR.value


@pytest.mark.parametrize("library", [TemporaryDirectory()], indirect=True)
def test_remove_folder(library):
# Given
folder = library.get_folders()[0]
folder_id = folder.id

thumbs_dir = library.get_folder_thumbnail_path(folder_id)
thumbs_dir.mkdir(parents=True)
assert thumbs_dir.exists()

# When
assert library.remove_folder(folder)

# Then
# check the folder of thumbnails was removed
assert not thumbs_dir.exists()

assert not list(library.get_entries(FilterState(include_folders={folder_id})))

0 comments on commit ea21d21

Please sign in to comment.