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 obvious type error in guessMimetype #42

Open
wants to merge 4 commits into
base: py3
Choose a base branch
from
Open
Show file tree
Hide file tree
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
33 changes: 18 additions & 15 deletions fcp3/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion fcp3/sitemgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ def createsitemap():
lines.extend([
"<tr>",
"<td>%s</td>" % size,
"<td>%s</td>" % str(mimetype), # TODO: check: mimetype for tar.b2 is a list?
"<td>%s</td>" % str(mimetype),
"<td><a href=\"%s\">%s</a></td>" % (name, name),
"</tr>",
])
Expand Down