Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the mimetypes problem discovered in python3.12 #41

Open
wants to merge 2 commits into
base: py3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions fcp3/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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):
Expand Down