From cfdaec23c5896df56a29094f984142942ce09dcf Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Tue, 20 Jun 2023 19:46:33 +0300 Subject: [PATCH] Support multiple Python libraries for libmagic It seems that there are at least two Python libraries with libmagic support: * PyPI: python-magic (https://pypi.org/project/python-magic/) On Fedora it's `python3-magic` * PyPI: filemagic (https://pypi.org/project/filemagic/) On Fedora it's `python3-file-magic` The first package corresponds to the `py3-magic` package on Alpine Linux, and it's the one we install in the container. The second package uses a different API, and it's the only one we can use on Qubes. To make matters worse, we: * Can't install the first package on Fedora, because it installs the second under the hood: https://bugzilla.redhat.com/show_bug.cgi?id=1899279 * Can't install the second package on Alpine Linux (untested), due to Musl being used instead of libC: https://stackoverflow.com/a/53936722 Ultimately, we need to support both, by trying the first API, and on failure using the other API. --- dangerzone/conversion/doc_to_pixels.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/dangerzone/conversion/doc_to_pixels.py b/dangerzone/conversion/doc_to_pixels.py index aa92088..2151500 100644 --- a/dangerzone/conversion/doc_to_pixels.py +++ b/dangerzone/conversion/doc_to_pixels.py @@ -101,8 +101,11 @@ class DocumentToPixels(DangerzoneConverter): } # Detect MIME type - mime = magic.Magic(mime=True) - mime_type = mime.from_file("/tmp/input_file") + try: + mime = magic.Magic(mime=True) + mime_type = mime.from_file("/tmp/input_file") + except TypeError: + mime_type = magic.detect_from_filename("/tmp/input_file").mime_type # Validate MIME type if mime_type not in conversions: