Save covers directly into covers dir

Instead of saving the pixbuf of the new cover into a temporary file and
then copy into covers dir, save it directly to there. Without this, a lot
of temporary files are created on import, which remain on the system even
after the application is closed.
This commit is contained in:
Balló György
2024-11-02 10:13:12 +01:00
parent 95c101d55e
commit 31c2a1dfee
4 changed files with 33 additions and 17 deletions

View File

@@ -240,6 +240,7 @@ class DetailsDialog(Adw.Dialog):
save_cover(
self.game.game_id,
self.game_cover.path,
self.game_cover.pixbuf,
)
shared.store.add_game(self.game, {}, run_pipeline=False)
@@ -304,17 +305,17 @@ class DetailsDialog(Adw.Dialog):
except UnidentifiedImageError:
pass
if not new_path:
new_path = convert_cover(
if new_path:
self.game_cover.new_cover(new_path)
else:
self.game_cover.new_cover(
pixbuf=shared.store.managers[CoverManager].composite_cover(
Path(path)
)
)
if new_path:
self.game_cover.new_cover(new_path)
self.cover_button_delete_revealer.set_reveal_child(True)
self.cover_changed = True
self.cover_button_delete_revealer.set_reveal_child(True)
self.cover_changed = True
self.toggle_loading()

View File

@@ -45,12 +45,22 @@ class GameCover:
self.pictures = pictures
self.new_cover(path)
def new_cover(self, path: Optional[Path] = None) -> None:
def new_cover(
self,
path: Optional[Path] = None,
pixbuf: Optional[GdkPixbuf.Pixbuf] = None
) -> None:
self.animation = None
self.texture = None
self.blurred = None
self.luminance = None
self.path = path
self.pixbuf = pixbuf
if pixbuf:
self.texture = Gdk.Texture.new_for_pixbuf(pixbuf)
self.set_texture(self.texture)
return
if path:
if path.suffix == ".gif":

View File

@@ -192,7 +192,5 @@ class CoverManager(Manager):
save_cover(
game.game_id,
convert_cover(
pixbuf=self.composite_cover(image_path, **composite_kwargs)
),
pixbuf=self.composite_cover(image_path, **composite_kwargs),
)

View File

@@ -30,7 +30,6 @@ from cartridges import shared
def convert_cover(
cover_path: Optional[Path] = None,
pixbuf: Optional[GdkPixbuf.Pixbuf] = None,
resize: bool = True,
) -> Optional[Path]:
if not cover_path and not pixbuf:
@@ -44,10 +43,6 @@ def convert_cover(
if not resize and cover_path and cover_path.suffix.lower()[1:] in pixbuf_extensions:
return cover_path
if pixbuf:
cover_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path())
pixbuf.savev(str(cover_path), "tiff", ["compression"], ["1"])
try:
with Image.open(cover_path) as image:
if getattr(image, "is_animated", False):
@@ -88,7 +83,11 @@ def convert_cover(
return tmp_path
def save_cover(game_id: str, cover_path: Path) -> None:
def save_cover(
game_id: str,
cover_path: Optional[Path] = None,
pixbuf: Optional[GdkPixbuf.Pixbuf] = None,
) -> None:
shared.covers_dir.mkdir(parents=True, exist_ok=True)
animated_path = shared.covers_dir / f"{game_id}.gif"
@@ -98,7 +97,15 @@ def save_cover(game_id: str, cover_path: Path) -> None:
animated_path.unlink(missing_ok=True)
static_path.unlink(missing_ok=True)
if not cover_path:
if not cover_path and not pixbuf:
return
if pixbuf:
pixbuf.savev(str(static_path), "tiff", ["compression"], ["1"])
if game_id in shared.win.game_covers:
shared.win.game_covers[game_id].new_cover(static_path)
return
copyfile(