Skip to content

Commit

Permalink
Fix #170: delete only those images that were obtained as a result of …
Browse files Browse the repository at this point in the history
…the screenshot
  • Loading branch information
Andrey Maksimov committed Dec 21, 2023
1 parent 2bc579a commit a6f2316
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions frog/services/screenshot_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# authorization.
import os
from gettext import gettext as _

from gi.repository import GObject, Gio, Xdp

from frog.config import tessdata_config
Expand Down Expand Up @@ -97,15 +98,18 @@ def take_screenshot_finish(self, source_object, res: Gio.Task, user_data):
filename = self.portal.take_screenshot_finish(res)
# Remove file:// from the path
filename = filename[7:]
self.decode_image(lang, filename, copy)

def decode_image(
self, lang: str, file: str | Image.Image, copy: bool = False
) -> None:
self.decode_image(lang, filename, copy, True)

def decode_image(self,
lang: str,
file: str | Image.Image,
copy: bool = False,
remove_source: bool = False,
) -> None:
# Check if `file` is a filepath and mark it for deletion
need_remove = False
if isinstance(file, str) and os.path.exists(file):
need_remove = True
if not isinstance(file, str) or not os.path.exists(file):
remove_source = False
print('Remove source set to False')

print(f"Decoding with {lang} language.")
extracted = None
Expand Down Expand Up @@ -134,7 +138,8 @@ def decode_image(
self.emit("error", "Failed to decode data.")

finally:
if need_remove:
if remove_source:
print(f"Removing {file}")
os.unlink(file)

if extracted:
Expand All @@ -144,6 +149,5 @@ def decode_image(
else:
self.emit("error", "No text found.")


def capture_cancelled(self, cancellable: Gio.Cancellable) -> None:
self.emit("error", "Cancelled")

0 comments on commit a6f2316

Please sign in to comment.