From 47032cb4e921adf6827e76b311a99fe9ad6d12e2 Mon Sep 17 00:00:00 2001 From: Ciaran Gultnieks Date: Thu, 13 Aug 2015 19:16:28 +0200 Subject: [PATCH] Fix multiple errors in get_mime_type Closes #111 --- fdroidserver/common.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 7b2aa087..e775e846 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -1452,23 +1452,25 @@ def get_mime_type(path): libmagic. Hence this function with the following hacks: ''' + ms = None try: import magic - ms = None try: ms = magic.open(magic.MIME_TYPE) ms.load() - return magic.from_file(path, mime=True) + result = magic.from_file(path, mime=True) except AttributeError: - return ms.file(path) - if ms is not None: - ms.close() + result = ms.file(path) except UnicodeError: logging.warn('Found malformed magic number at %s' % path) + result = None except ImportError: import mimetypes mimetypes.init() - return mimetypes.guess_type(path, strict=False) + result = mimetypes.guess_type(path, strict=False) + if ms is not None: + ms.close() + return result # Scan the source code in the given directory (and all subdirectories) -- 2.30.2