diff options
author | Philipp Hörist <philipp@hoerist.com> | 2023-04-16 14:03:25 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2023-04-16 14:05:47 +0300 |
commit | 5e2011fee11237264b643e338da1b34b442f4656 (patch) | |
tree | 4b3b0dc9c634bbc3f28f5a91d96630f1b930664e | |
parent | 60be989f5ac21fbf02e938e7f989d322c0294600 (diff) |
fix: Preview: Display webp and avif images on Windows correctly
- Gio.content_type_guess() returns a file extension,
use Gio.content_type_get_mime_type() to get the mime type
-rw-r--r-- | gajim/common/preview_helpers.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/gajim/common/preview_helpers.py b/gajim/common/preview_helpers.py index a4afeccf5..f834eca3a 100644 --- a/gajim/common/preview_helpers.py +++ b/gajim/common/preview_helpers.py @@ -450,12 +450,24 @@ def guess_mime_type(file_path: Union[Path, str], data: Optional[bytes] = None ) -> str: file_path = str(file_path) + + if not mimetypes.inited: + # On Windows both mime types are only available + # with python 3.11, so this can be removed once + # the Windows build uses python 3.11 + mimetypes.add_type('image/webp', '.webp') + mimetypes.add_type('image/avif', '.avif') + + # The mimetypes module maps extensions to mime types + # it does no guessing based on file content mime_type, _ = mimetypes.guess_type(file_path) if mime_type is None: - # Try to guess MIME type by file name - mime_type, _ = Gio.content_type_guess(file_path, data) + # Gio does also guess based on file content + extension, _ = Gio.content_type_guess(file_path, data) + mime_type = Gio.content_type_get_mime_type(extension) + log.debug('Guessed MIME type: %s', mime_type) - return mime_type + return mime_type or '' def guess_simple_file_type(file_path: str, |