mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
container: Do not leave stale temporary dirs
Do not leave stale temporary directories when conversion fails unexpectedly. Instead, wrap the conversion operation in a context manager that wipes the temporary dir afterwards. Fixes #317
This commit is contained in:
parent
18bc77332d
commit
d733890ca0
2 changed files with 33 additions and 10 deletions
|
@ -2,6 +2,7 @@ import gzip
|
|||
import json
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import pipes
|
||||
import platform
|
||||
import shutil
|
||||
|
@ -215,6 +216,34 @@ class Container(IsolationProvider):
|
|||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
# Create a temporary directory inside the cache directory for this run. Then,
|
||||
# create some subdirectories for the various stages of the file conversion:
|
||||
#
|
||||
# * pixel: Where the RGB data will be stored
|
||||
# * safe: Where the final PDF file will be stored
|
||||
with tempfile.TemporaryDirectory(dir=get_tmp_dir()) as t:
|
||||
tmp_dir = pathlib.Path(t)
|
||||
pixel_dir = tmp_dir / "pixels"
|
||||
pixel_dir.mkdir()
|
||||
safe_dir = tmp_dir / "safe"
|
||||
safe_dir.mkdir()
|
||||
|
||||
return self._convert_with_tmpdirs(
|
||||
document=document,
|
||||
pixel_dir=pixel_dir,
|
||||
safe_dir=safe_dir,
|
||||
ocr_lang=ocr_lang,
|
||||
stdout_callback=stdout_callback,
|
||||
)
|
||||
|
||||
def _convert_with_tmpdirs(
|
||||
self,
|
||||
document: Document,
|
||||
pixel_dir: pathlib.Path,
|
||||
safe_dir: pathlib.Path,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
success = False
|
||||
|
||||
|
@ -223,13 +252,6 @@ class Container(IsolationProvider):
|
|||
else:
|
||||
ocr = "0"
|
||||
|
||||
dz_tmp = get_tmp_dir()
|
||||
tmpdir = tempfile.TemporaryDirectory(dir=dz_tmp)
|
||||
pixel_dir = os.path.join(tmpdir.name, "pixels")
|
||||
safe_dir = os.path.join(tmpdir.name, "safe")
|
||||
os.makedirs(pixel_dir, exist_ok=True)
|
||||
os.makedirs(safe_dir, exist_ok=True)
|
||||
|
||||
# Convert document to pixels
|
||||
command = [
|
||||
"/usr/bin/python3",
|
||||
|
@ -284,9 +306,6 @@ class Container(IsolationProvider):
|
|||
# We did it
|
||||
success = True
|
||||
|
||||
# Clean up
|
||||
tmpdir.cleanup()
|
||||
|
||||
return success
|
||||
|
||||
def get_max_parallel_conversions(self) -> int:
|
||||
|
|
|
@ -150,6 +150,10 @@ class TestCli(TestBase):
|
|||
if tmp_path is not None:
|
||||
os.chdir(cwd)
|
||||
|
||||
if tmp_dir.exists():
|
||||
stale_files = list(tmp_dir.iterdir())
|
||||
assert not stale_files
|
||||
|
||||
return CLIResult.reclass_click_result(result, args)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue