Clear temporary files after conversion

convert_cover() now always return a temporary file, which needs to be
removed after the cover is saved. Without this, these files would remain on
the system until reboot. Also remove the downloaded temporary files after
save.
This commit is contained in:
Balló György
2024-11-02 19:16:32 +01:00
parent 117055bf64
commit 4331b0d8c0
3 changed files with 30 additions and 9 deletions

View File

@@ -68,7 +68,8 @@ class DetailsDialog(Adw.Dialog):
# Make it so only one dialog can be open at a time
self.__class__.is_open = True
self.connect("closed", lambda *_: self.set_is_open(False))
self.tmp_cover_path = None
self.connect("closed", self.on_closed)
self.game: Optional[Game] = game
self.game_cover: GameCover = GameCover({self.cover})
@@ -160,11 +161,20 @@ class DetailsDialog(Adw.Dialog):
self.set_focus(self.name)
def delete_pixbuf(self, *_args: Any) -> None:
if self.tmp_cover_path:
self.tmp_cover_path.unlink(missing_ok=True)
self.game_cover.new_cover()
self.cover_button_delete_revealer.set_reveal_child(False)
self.cover_changed = True
def on_closed(self, *args):
if self.tmp_cover_path:
self.tmp_cover_path.unlink(missing_ok=True)
self.set_is_open(False)
def apply_preferences(self, *_args: Any) -> None:
final_name = self.name.get_text()
final_developer = self.developer.get_text()
@@ -296,17 +306,20 @@ class DetailsDialog(Adw.Dialog):
return
def thread_func() -> None:
new_path = None
is_animated = False
try:
with Image.open(path) as image:
if getattr(image, "is_animated", False):
new_path = convert_cover(path)
is_animated = True
except (UnidentifiedImageError, OSError, ValueError):
pass
if new_path:
self.game_cover.new_cover(new_path)
if is_animated:
if self.tmp_cover_path:
self.tmp_cover_path.unlink(missing_ok=True)
self.tmp_cover_path = convert_cover(path)
self.game_cover.new_cover(self.tmp_cover_path)
else:
self.game_cover.new_cover(
pixbuf=shared.store.managers[CoverManager].composite_cover(

View File

@@ -133,12 +133,13 @@ class CoverManager(Manager):
str(image_path)
)
except GLib.Error:
new_path = convert_cover(image_path, resize=False)
tmp_cover_path = convert_cover(image_path, resize=False)
if new_path:
if tmp_cover_path:
source = GdkPixbuf.Pixbuf.new_from_file(
str(new_path)
str(tmp_cover_path)
)
tmp_cover_path.unlink(missing_ok=True)
else:
return None
@@ -205,3 +206,6 @@ class CoverManager(Manager):
game.game_id,
pixbuf=self.composite_cover(image_path, **composite_kwargs),
)
if key == "online_cover_url":
image_path.unlink(missing_ok=True)

View File

@@ -134,7 +134,11 @@ class SgdbHelper:
tmp_file = Gio.File.new_tmp()[0]
tmp_file_path = tmp_file.get_path()
Path(tmp_file_path).write_bytes(response.content)
save_cover(game.game_id, convert_cover(tmp_file_path))
tmp_cover_path = convert_cover(tmp_file_path)
if tmp_cover_path:
save_cover(game.game_id, tmp_cover_path)
tmp_cover_path.unlink(missing_ok=True)
tmp_file_path.unlink(missing_ok=True)
except SgdbAuthError as error:
# Let caller handle auth errors
raise error