web: slight refactor to make path handling more readable

(at least, more readable to me)
This commit is contained in:
wisp3rwind 2022-09-24 12:08:04 +02:00
parent 35e167f75e
commit 3510e6311d

View file

@ -307,19 +307,24 @@ def item_file(item_id):
else:
item_path = util.py3_path(item.path)
try:
unicode_item_path = util.text_string(item.path)
except (UnicodeDecodeError, UnicodeEncodeError):
unicode_item_path = util.displayable_path(item.path)
base_filename = os.path.basename(item_path)
# FIXME: Arguably, this should just use `displayable_path`: The latter
# tries `_fsencoding()` first, but then falls back to `utf-8`, too.
if isinstance(base_filename, bytes):
try:
unicode_base_filename = base_filename.decode("utf-8")
except UnicodeError:
unicode_base_filename = util.displayable_path(base_filename)
else:
unicode_base_filename = base_filename
base_filename = os.path.basename(unicode_item_path)
try:
# Imitate http.server behaviour
base_filename.encode("latin-1", "strict")
except UnicodeEncodeError:
safe_filename = unidecode(base_filename)
unicode_base_filename.encode("latin-1", "strict")
except UnicodeError:
safe_filename = unidecode(unicode_base_filename)
else:
safe_filename = base_filename
safe_filename = unicode_base_filename
response = flask.send_file(
item_path,