From 1937b8169911c60827cc65cb9f18938b80e0fb6c Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 10 May 2018 18:52:00 +0200
Subject: [PATCH] Fix #196: In-place imported tracks non-ascii characters don't
 break reverse-proxy serving

---
 api/funkwhale_api/music/views.py |  2 +-
 api/tests/music/test_views.py    | 18 ++++++++++++++++++
 changes/changelog.d/196.bug      |  1 -
 changes/changelog.d/196.bugfix   |  1 +
 4 files changed, 20 insertions(+), 2 deletions(-)
 delete mode 100644 changes/changelog.d/196.bug
 create mode 100644 changes/changelog.d/196.bugfix

diff --git a/api/funkwhale_api/music/views.py b/api/funkwhale_api/music/views.py
index 14900ca988..b104bf3899 100644
--- a/api/funkwhale_api/music/views.py
+++ b/api/funkwhale_api/music/views.py
@@ -241,7 +241,7 @@ def get_file_path(audio_file):
                     'You need to specify MUSIC_DIRECTORY_SERVE_PATH and '
                     'MUSIC_DIRECTORY_PATH to serve in-place imported files'
                 )
-            path = audio_file.replace(prefix, serve_path, 1)
+            path = audio_file.replace(prefix, serve_path, 1).encode('utf-8')
         return path
 
 
diff --git a/api/tests/music/test_views.py b/api/tests/music/test_views.py
index b22ab7fd50..e641b45d51 100644
--- a/api/tests/music/test_views.py
+++ b/api/tests/music/test_views.py
@@ -104,6 +104,24 @@ def test_serve_file_in_place(
     assert response[headers[proxy]] == expected
 
 
+@pytest.mark.parametrize('proxy,serve_path,expected', [
+    ('apache2', '/host/music', '/host/music/hello/worldéà.mp3'),
+    ('apache2', '/app/music', '/app/music/hello/worldéà.mp3'),
+    ('nginx', '/host/music', '/_protected/music/hello/worldéà.mp3'),
+    ('nginx', '/app/music', '/_protected/music/hello/worldéà.mp3'),
+])
+def test_serve_file_in_place_utf8(
+        proxy, serve_path, expected, factories, api_client, settings):
+    settings.PROTECT_AUDIO_FILES = False
+    settings.PROTECT_FILE_PATH = '/_protected/music'
+    settings.REVERSE_PROXY_TYPE = proxy
+    settings.MUSIC_DIRECTORY_PATH = '/app/music'
+    settings.MUSIC_DIRECTORY_SERVE_PATH = serve_path
+    path = views.get_file_path('/app/music/hello/worldéà.mp3')
+
+    assert path == expected.encode('utf-8')
+
+
 @pytest.mark.parametrize('proxy,serve_path,expected', [
     ('apache2', '/host/music', '/host/media/tracks/hello/world.mp3'),
     # apache with container not supported yet
diff --git a/changes/changelog.d/196.bug b/changes/changelog.d/196.bug
deleted file mode 100644
index 6f59a28d1a..0000000000
--- a/changes/changelog.d/196.bug
+++ /dev/null
@@ -1 +0,0 @@
-Tracks with special UTF-8 characters don't break anymore, fixing Bug #196
diff --git a/changes/changelog.d/196.bugfix b/changes/changelog.d/196.bugfix
new file mode 100644
index 0000000000..655df7d795
--- /dev/null
+++ b/changes/changelog.d/196.bugfix
@@ -0,0 +1 @@
+In-place imported tracks non-ascii characters don't break reverse-proxy serving (#196)
-- 
GitLab