diff --git a/fcp3/node.py b/fcp3/node.py index 59d2329..2deed2c 100644 --- a/fcp3/node.py +++ b/fcp3/node.py @@ -3185,7 +3185,8 @@ def sha256dda(nodehelloid, identifier, path=None): tohash = b"-".join([nodehelloid.encode('utf-8'), identifier.encode('utf-8'), open(path, "rb").read()]) return hashlib.sha256(tohash).digest() -def guessMimetype(filename): + +def guessMimetype(filename: str | bytes) -> tuple[str, str] | str: """ Returns a guess of a mimetype based on a filename's extension """ @@ -3196,21 +3197,29 @@ def guessMimetype(filename): if filename.endswith(".tar.bz2"): return ('application/x-tar', 'bzip2') try: - m = mimetypes.guess_type(filename, False)[0] - except TypeError: # bytes compared to string string … + m = mimetypes.guess_type(filename + if isinstance(filename, str) + else filename.decode(), + False)[0] + except TypeError: # bytes compared to string string … try: - m = mimetypes.guess_type(filename.decode(), False)[0] - except: + m = mimetypes.guess_type(filename.decode() + if isinstance(filename, bytes) + else filename, + False)[0] + except Exception: m = None - except: + except Exception: m = None - if m == "audio/mpegurl": # disallowed mime type by FF + if m == "audio/mpegurl": # disallowed mime type by FF m = "audio/x-mpegurl" - if m is None: # either an exception or a genuine None - # FIXME: log(INFO, "Could not find mimetype for filename %s" % filename) + if m is None: # either an exception or a genuine None + # FIXME: log(INFO, + # "Could not find mimetype for filename %s" % filename) m = "application/octet-stream" return m + _re_slugify = re.compile('[^\w\s\.-]', re.UNICODE) _re_slugify_multidashes = re.compile('[-\s]+', re.UNICODE) def toUrlsafe(filename):