From 31ff902098dcca533884fa39f05071f4dccda874 Mon Sep 17 00:00:00 2001 From: Adrian Vollmer Date: Tue, 18 Oct 2022 17:30:40 +0200 Subject: [PATCH] Add fall back when guessing mime type This fixes a bug that appears if the document is built on a system without libmagic1. The python library python-magic uses it to guess the mime type. We treat files with unknown mime type as `octet-stream`, which causes them to be downloaded rather than displayed in the main iframe. This results in lots of popups when opening the resulting HTML file in a browser. In this commit we add a fallback to the built-in library `mimetypes` which gueses the mime type from the filename alone. This should work in most cases but could still fail. Close #2. --- zundler/embed.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/zundler/embed.py b/zundler/embed.py index 4e92b75..2f4e189 100644 --- a/zundler/embed.py +++ b/zundler/embed.py @@ -120,7 +120,7 @@ def prepare_file(filename, before, after): _, ext = os.path.splitext(filename) ext = ext.lower()[1:] data = open(filename, 'rb').read() - mime_type = mime_type_from_bytes(data) + mime_type = mime_type_from_bytes(filename, data) base64encoded = False if ext == 'css': @@ -260,7 +260,7 @@ def embed_css_resources(css, filename): mime_type = 'text/css' content = embed_css_resources(content, filename) else: - mime_type = mime_type_from_bytes(content) + mime_type = mime_type_from_bytes(filename, content) if not mime_type: logger.error('Unable to determine mime type: %s' % path) mime_type = 'application/octet-stream' @@ -278,14 +278,28 @@ def embed_css_resources(css, filename): return css -def mime_type_from_bytes(buffer): +def mime_type_from_bytes(filename, buffer): try: import magic mime_type = magic.Magic(mime=True).from_buffer(buffer) except Exception as e: - logger.error("Error while guessing mime type: " + str(buffer[:10]) + '...') + logger.error( + "Error while guessing mime type (%s): %s" % + (filename, str(buffer[:10]) + '...') + ) logger.error(str(e)) - return 'application/octet-stream' + + # Fall back to builtin; can happen if libmagic1 is missing + import mimetypes + mime_type = mimetypes.guess_type(filename)[0] + + if not mime_type: + logger.error( + "Unknown mime type (%s): %s" % + (filename, str(buffer[:10]) + '...') + ) + mime_type = 'application/octet-stream' + return mime_type