diff --git a/fcp3/node.py b/fcp3/node.py index 59d2329..7b30fdb 100644 --- a/fcp3/node.py +++ b/fcp3/node.py @@ -3185,32 +3185,35 @@ 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) -> str: """ Returns a guess of a mimetype based on a filename's extension """ - if isinstance(filename, bytes): - if filename.endswith(b".tar.bz2"): - return ('application/x-tar', 'bzip2') - else: - 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): diff --git a/fcp3/sitemgr.py b/fcp3/sitemgr.py index 4cf86f7..c964943 100644 --- a/fcp3/sitemgr.py +++ b/fcp3/sitemgr.py @@ -1347,7 +1347,7 @@ def createsitemap(): lines.extend([ "", "%s" % size, - "%s" % str(mimetype), # TODO: check: mimetype for tar.b2 is a list? + "%s" % str(mimetype), "%s" % (name, name), "", ])