From d0fd41b47484eca2fcf7c271ec35431f13cccd76 Mon Sep 17 00:00:00 2001 From: Waweic Date: Thu, 1 Mar 2018 19:45:44 +0100 Subject: [PATCH] Add unicode support for Python 2 and 3 Converts bytes to unicode using util.text_string, assuming that the string is a UTF-8 string. If that fails, it falls back to a hardcoded fallback filename. --- beetsplug/web/__init__.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/beetsplug/web/__init__.py b/beetsplug/web/__init__.py index c85e6a8c0..f9512ddc1 100644 --- a/beetsplug/web/__init__.py +++ b/beetsplug/web/__init__.py @@ -225,12 +225,19 @@ def item_file(item_id): item_path = util.syspath(item.path) else: item_path = util.py3_path(item.path) - try: - os.path.basename(util.py3_path(item.path)).encode("latin-1", "strict") #Imitate http.server behaviour - response = flask.send_file(item_path,as_attachment=True) - except UnicodeEncodeError: - response = flask.send_file(item_path,as_attachment=True, attachment_filename=unidecode(os.path.basename(util.py3_path(item.path)))) + try: + unicode_item_path = util.text_string(item.path) + except: + unicode_item_path = u"fallback" + os.path.splitext(item_path)[1] + + try: + os.path.basename(unicode_item_path).encode("latin-1", "strict") #Imitate http.server behaviour + safe_filename = os.path.basename(unicode_item_path) + except: + safe_filename = unidecode(os.path.basename(unicode_item_path)) + + response = flask.send_file(item_path,as_attachment=True, attachment_filename=safe_filename) response.headers['Content-Length'] = os.path.getsize(item_path) return response