From 3510e6311db479471102b5f0380d27fe2be8786b Mon Sep 17 00:00:00 2001 From: wisp3rwind <17089248+wisp3rwind@users.noreply.github.com> Date: Sat, 24 Sep 2022 12:08:04 +0200 Subject: [PATCH] web: slight refactor to make path handling more readable (at least, more readable to me) --- beetsplug/web/__init__.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index b7baa93c1..6a70a962d 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -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,