Add CLI support for archiving original / unsafe PDFs

This commit is contained in:
deeplow 2022-11-21 15:02:35 +00:00
parent c6a0b59379
commit b4849995e3
No known key found for this signature in database
GPG key ID: 577982871529A52A
3 changed files with 25 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import click
from colorama import Back, Fore, Style from colorama import Back, Fore, Style
from . import args, container, errors from . import args, container, errors
from .document import SAFE_EXTENSION from .document import ARCHIVE_SUBDIR, SAFE_EXTENSION
from .logic import DangerzoneCore from .logic import DangerzoneCore
from .util import get_version from .util import get_version
@ -25,6 +25,12 @@ def print_header(s: str) -> None:
help=f"Default is filename ending with {SAFE_EXTENSION}", help=f"Default is filename ending with {SAFE_EXTENSION}",
) )
@click.option("--ocr-lang", help="Language to OCR, defaults to none") @click.option("--ocr-lang", help="Language to OCR, defaults to none")
@click.option(
"--archive",
"archive",
flag_value=True,
help=f"Archives the unsafe version in a subdirectory named '{ARCHIVE_SUBDIR}'",
)
@click.argument( @click.argument(
"filenames", "filenames",
required=True, required=True,
@ -34,20 +40,23 @@ def print_header(s: str) -> None:
) )
@errors.handle_document_errors @errors.handle_document_errors
def cli_main( def cli_main(
output_filename: Optional[str], ocr_lang: Optional[str], filenames: List[str] output_filename: Optional[str],
ocr_lang: Optional[str],
filenames: List[str],
archive: bool,
) -> None: ) -> None:
setup_logging() setup_logging()
dangerzone = DangerzoneCore() dangerzone = DangerzoneCore()
display_banner() display_banner()
if len(filenames) == 1 and output_filename: if len(filenames) == 1 and output_filename:
dangerzone.add_document_from_filename(filenames[0], output_filename) dangerzone.add_document_from_filename(filenames[0], output_filename, archive)
elif len(filenames) > 1 and output_filename: elif len(filenames) > 1 and output_filename:
click.echo("--output-filename can only be used with one input file.") click.echo("--output-filename can only be used with one input file.")
exit(1) exit(1)
else: else:
for filename in filenames: for filename in filenames:
dangerzone.add_document_from_filename(filename) dangerzone.add_document_from_filename(filename, archive=archive)
# Validate OCR language # Validate OCR language
if ocr_lang: if ocr_lang:
@ -76,6 +85,12 @@ def cli_main(
print_header("Safe PDF(s) created successfully") print_header("Safe PDF(s) created successfully")
for document in documents_safe: for document in documents_safe:
click.echo(document.output_filename) click.echo(document.output_filename)
if archive:
print_header(
f"Unsafe (original) documents moved to '{ARCHIVE_SUBDIR}' subdirectory"
)
if documents_failed != []: if documents_failed != []:
print_header("Failed to convert document(s)") print_header("Failed to convert document(s)")
for document in documents_failed: for document in documents_failed:

View file

@ -97,7 +97,7 @@ def handle_document_errors(func: F) -> F:
except DocumentFilenameException as e: except DocumentFilenameException as e:
if getattr(sys, "dangerzone_dev", False): if getattr(sys, "dangerzone_dev", False):
# Show the full traceback only on dev environments. # Show the full traceback only on dev environments.
msg = "An exception occured while validating a document filename" msg = "An exception occured while validating a document"
log.exception(msg) log.exception(msg)
click.echo(str(e)) click.echo(str(e))
exit(1) exit(1)

View file

@ -42,9 +42,12 @@ class DangerzoneCore(object):
self.documents: List[Document] = [] self.documents: List[Document] = []
def add_document_from_filename( def add_document_from_filename(
self, input_filename: str, output_filename: Optional[str] = None self,
input_filename: str,
output_filename: Optional[str] = None,
archive: bool = False,
) -> None: ) -> None:
doc = Document(input_filename, output_filename) doc = Document(input_filename, output_filename, archive=archive)
self.add_document(doc) self.add_document(doc)
def add_document(self, doc: Document) -> None: def add_document(self, doc: Document) -> None: