mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Make the CLI fully functional
This commit is contained in:
parent
73d412501c
commit
1144fd9f87
2 changed files with 98 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -133,4 +133,4 @@ dmypy.json
|
||||||
deb_dist
|
deb_dist
|
||||||
.DS_Store
|
.DS_Store
|
||||||
install/windows/Dangerzone.wxs
|
install/windows/Dangerzone.wxs
|
||||||
test-docs/sample-safe.pdf
|
test_docs/sample-safe.pdf
|
|
@ -1,3 +1,5 @@
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from .global_common import GlobalCommon
|
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()
|
global_common = GlobalCommon()
|
||||||
common = Common()
|
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:
|
if custom_container:
|
||||||
success, error_message = global_common.container_exists(custom_container)
|
success, error_message = global_common.container_exists(custom_container)
|
||||||
if not success:
|
if not success:
|
||||||
|
@ -65,7 +126,7 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam
|
||||||
if returncode != 0:
|
if returncode != 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Document to pixels
|
# Convert to pixels
|
||||||
click.echo("Converting document to pixels")
|
click.echo("Converting document to pixels")
|
||||||
returncode, output, _ = exec_container(
|
returncode, output, _ = exec_container(
|
||||||
global_common,
|
global_common,
|
||||||
|
@ -89,3 +150,37 @@ def cli_main(custom_container, safe_pdf_filename, ocr_lang, skip_update, filenam
|
||||||
if not success:
|
if not success:
|
||||||
click.echo(error_message)
|
click.echo(error_message)
|
||||||
return
|
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}")
|
||||||
|
|
Loading…
Reference in a new issue