diff --git a/.gitignore b/.gitignore index b4cc8ea..465d3eb 100644 --- a/.gitignore +++ b/.gitignore @@ -133,4 +133,4 @@ dmypy.json deb_dist .DS_Store install/windows/Dangerzone.wxs -test-docs/sample-safe.pdf \ No newline at end of file +test_docs/sample-safe.pdf \ No newline at end of file diff --git a/dangerzone/cli.py b/dangerzone/cli.py index c9aa893..a9ed940 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -1,3 +1,5 @@ +import os +import shutil import click from .global_common import GlobalCommon @@ -38,7 +40,66 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam global_common = GlobalCommon() common = Common() - # Make sure custom container exists + # Validate filename + valid = True + try: + with open(os.path.abspath(filename), "rb") as f: + pass + except: + valid = False + + if not valid: + click.echo("Invalid filename") + return + + common.document_filename = os.path.abspath(filename) + + # Validate safe PDF output filename + if safe_pdf_filename: + valid = True + if not safe_pdf_filename.endswith(".pdf"): + click.echo("Safe PDF filename must end in '.pdf'") + return + + try: + with open(os.path.abspath(safe_pdf_filename), "wb") as f: + pass + except: + valid = False + + if not valid: + click.echo("Safe PDF filename is not writable") + return + + common.save_filename = os.path.abspath(safe_pdf_filename) + + else: + common.save_filename = ( + f"{os.path.splitext(common.document_filename)[0]}-safe.pdf" + ) + try: + with open(common.save_filename, "wb") as f: + pass + except: + click.echo( + f"Output filename {common.save_filename} is not writable, use --safe-pdf-filename" + ) + return + + # Validate OCR language + if ocr_lang: + valid = False + for lang in global_common.ocr_languages: + if global_common.ocr_languages[lang] == ocr_lang: + valid = True + break + if not valid: + click.echo("Invalid OCR language code. Valid language codes:") + for lang in global_common.ocr_languages: + click.echo(f"{global_common.ocr_languages[lang]}: {lang}") + return + + # Validate custom container if custom_container: success, error_message = global_common.container_exists(custom_container) if not success: @@ -65,7 +126,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam if returncode != 0: return - # Document to pixels + # Convert to pixels click.echo("Converting document to pixels") returncode, output, _ = exec_container( global_common, @@ -89,3 +150,37 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam if not success: click.echo(error_message) return + + # Convert to PDF + click.echo("Converting pixels to safe PDF") + + if ocr_lang: + ocr = "1" + else: + ocr = "0" + ocr_lang = "" + + returncode, _, _ = exec_container( + global_common, + [ + "pixelstopdf", + "--pixel-dir", + common.pixel_dir.name, + "--safe-dir", + common.safe_dir.name, + "--container-name", + global_common.get_container_name(), + "--ocr", + ocr, + "--ocr-lang", + ocr_lang, + ], + ) + + if returncode != 0: + return + + # Save the safe PDF + source_filename = f"{common.safe_dir.name}/safe-output-compressed.pdf" + shutil.move(source_filename, common.save_filename) + print(f"Success! {common.save_filename}")