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.
This commit is contained in:
Alex Pyrgiotis 2023-06-20 19:46:33 +03:00
parent 9410da762c
commit cfdaec23c5
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -101,8 +101,11 @@ class DocumentToPixels(DangerzoneConverter):
}
# Detect MIME type
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: