Skip to content

Accept files from stdin, write safe PDFs to stdout - #1528

Open
apyrgio wants to merge 6 commits into
mainfrom
1522-stdin-stdout
Open

Accept files from stdin, write safe PDFs to stdout#1528
apyrgio wants to merge 6 commits into
mainfrom
1522-stdin-stdout

Conversation

@apyrgio

@apyrgio apyrgio commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Dangerzone CLI now accepts documents via stdin and writes safe PDFs to stdout, following standard Unix pipe conventions:

cat file.pdf | dangerzone > safe.pdf
dangerzone - < file.pdf > safe.pdf
dangerzone < file.pdf > safe.pdf

All status output (banner, progress, errors) goes to stderr so stdout stays clean for PDF data. We also take extra care to detect when we can read from stdin and when we can write to stdout, so that we don't mess with the user's TTY.

Fixes #1522
Refs #206

Remove the private _validate_* functions and their public wrapper
functions that existed to work around a Click 7.x bug (issue #206).
Click 7.x is no longer supported, so these wrappers are unnecessary.

Promote the @errors.handle_document_errors-decorated functions directly
to public names.
apyrgio added 5 commits July 21, 2026 12:43
The ASCII art banner was printing to stdout, which would interfere with
piping safe PDF data. Route all banner output to stderr instead, and
update the corresponding test to read from stderr.
All human-readable status output (print_header, settings messages, OCR
language validation, container image warnings, failed document display)
now goes to stderr via click.echo(..., err=True), keeping stdout clean
for potential PDF data output.

Also use str(document) for failed doc display instead of
document.input_filename, which is preparatory for data-based documents
where input_filename would raise.
Extend the Document class to accept raw bytes via a new 'data'
parameter instead of requiring a file path. This is the foundation
for stdin support.

- New 'data' parameter in __init__ (default None, backward-compatible)
- Constructor requires either input_filename or data
- New Document.from_stdin() classmethod reads from sys.stdin.buffer
- New open() method returns BinaryIO (file or BytesIO)
- New write() method writes to stdout or output file
- New sanitized_input_filename property
- announce_id() handles both file and stdin documents
- __eq__, __hash__, __str__ handle data-based documents
- Add DangerzoneCore.add_document_from_stdin()
- Update tests for new constructor requirements
Decouple the isolation provider from file-based I/O by using the
Document abstraction methods:
- document.open() instead of open(document.input_filename, 'rb')
- document.write() instead of safe_doc.save() + os.replace()

This enables the provider to work with data-based documents (stdin)
without modification.
Implement stdin/stdout I/O for the Dangerzone CLI.

- Add _initialize_documents() handling stdin detection, validation,
  and document creation
- Accept '-' as a filename to explicitly read from stdin
- Accept implicit stdin when no filenames given and stdin is piped
- Write safe PDF to stdout when no --output-filename is specified
- Refuse to write to a terminal without --output-filename
- Reject --archive with stdin input
- Update @click.command() help text mentioning stdin
- Add STDIO_DESCRIPTOR constant and skip validation for '-'
- Add TestCliIO test class covering all stdin/stdout scenarios

Fixes #1522
@apyrgio
apyrgio force-pushed the 1522-stdin-stdout branch from 5d37a5a to d5e3c95 Compare July 21, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Accept files from stdin

1 participant