Skip to content

Commit

Permalink
Merge pull request beetbox#2827 from waweic/web-unicode
Browse files Browse the repository at this point in the history
Fix unicode bug in web plugin
  • Loading branch information
sampsyo authored Mar 7, 2018
2 parents 7fae3da + 06d4fe2 commit 9f945a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 16 additions & 1 deletion beetsplug/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from flask import g
from werkzeug.routing import BaseConverter, PathConverter
import os
from unidecode import unidecode
import json
import base64

Expand Down Expand Up @@ -225,10 +226,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(unicode_item_path)
try:
# Imitate http.server behaviour
base_filename.encode("latin-1", "strict")
except UnicodeEncodeError:
safe_filename = unidecode(base_filename)
else:
safe_filename = base_filename

response = flask.send_file(
item_path,
as_attachment=True,
attachment_filename=os.path.basename(util.py3_path(item.path)),
attachment_filename=safe_filename
)
response.headers['Content-Length'] = os.path.getsize(item_path)
return response
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ Fixes:
album art would not work and throw an exception. It now works as expected.
Additionally, the server will now return a 404 response when the album id
is unknown, instead of a 500 response and a thrown exception. :bug:`2823`
* :doc:`/plugins/web`: In a python 3 enviroment, the server would throw an
exception if non latin-1 characters where in the File name.
It now checks if non latin-1 characters are in the filename and changes
them to ascii-characters in that case :bug:`2815`

For developers:

Expand Down

0 comments on commit 9f945a7

Please sign in to comment.