Fix IsolationProvider.percentage variable reuse

If one converted more than one document, since the state of
IsolationProvider.percentage would be stored in the IsolationProvider
instance, it would get reused for the second document. The fix is to
keep it as a local variable, but we can explore having progress stored
on the document itself, for example. Or having one IsolationProvider per
conversion.
This commit is contained in:
deeplow 2024-02-05 14:27:43 +00:00
parent 0a099540c8
commit c991e530d0
No known key found for this signature in database
GPG key ID: 577982871529A52A

View file

@ -52,7 +52,6 @@ class IsolationProvider(ABC):
STARTUP_TIME_SECONDS = 0 # The maximum time it takes a the provider to start up. STARTUP_TIME_SECONDS = 0 # The maximum time it takes a the provider to start up.
def __init__(self) -> None: def __init__(self) -> None:
self.percentage = 0.0
self.proc: Optional[subprocess.Popen] = None self.proc: Optional[subprocess.Popen] = None
if getattr(sys, "dangerzone_dev", False) == True: if getattr(sys, "dangerzone_dev", False) == True:
@ -98,6 +97,7 @@ class IsolationProvider(ABC):
document.mark_as_failed() document.mark_as_failed()
def doc_to_pixels(self, document: Document, tempdir: str) -> None: def doc_to_pixels(self, document: Document, tempdir: str) -> None:
percentage = 0.0
with open(document.input_filename, "rb") as f: with open(document.input_filename, "rb") as f:
self.proc = self.start_doc_to_pixels_proc() self.proc = self.start_doc_to_pixels_proc()
try: try:
@ -125,7 +125,7 @@ class IsolationProvider(ABC):
sw.start() sw.start()
for page in range(1, n_pages + 1): for page in range(1, n_pages + 1):
text = f"Converting page {page}/{n_pages} to pixels" text = f"Converting page {page}/{n_pages} to pixels"
self.print_progress_trusted(document, False, text, self.percentage) self.print_progress_trusted(document, False, text, percentage)
width = read_int(self.proc.stdout, timeout=sw.remaining) width = read_int(self.proc.stdout, timeout=sw.remaining)
height = read_int(self.proc.stdout, timeout=sw.remaining) height = read_int(self.proc.stdout, timeout=sw.remaining)
@ -149,14 +149,14 @@ class IsolationProvider(ABC):
with open(f"{tempdir}/pixels/page-{page}.rgb", "wb") as f_rgb: with open(f"{tempdir}/pixels/page-{page}.rgb", "wb") as f_rgb:
f_rgb.write(untrusted_pixels) f_rgb.write(untrusted_pixels)
self.percentage += percentage_per_page percentage += percentage_per_page
# Ensure nothing else is read after all bitmaps are obtained # Ensure nothing else is read after all bitmaps are obtained
self.proc.stdout.close() self.proc.stdout.close()
# TODO handle leftover code input # TODO handle leftover code input
text = "Converted document to pixels" text = "Converted document to pixels"
self.print_progress_trusted(document, False, text, self.percentage) self.print_progress_trusted(document, False, text, percentage)
if getattr(sys, "dangerzone_dev", False): if getattr(sys, "dangerzone_dev", False):
assert self.proc.stderr is not None assert self.proc.stderr is not None