From 0a25243e259d26957fb7099dcf19b0a9b8b02e16 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 13 Jun 2019 11:05:28 +0200
Subject: [PATCH] Fix #857: Fix broken upload for specific files when using S3
 storage

---
 api/funkwhale_api/music/utils.py | 2 +-
 api/tests/music/test_utils.py    | 9 +++++++++
 changes/changelog.d/857.bugfix   | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 changes/changelog.d/857.bugfix

diff --git a/api/funkwhale_api/music/utils.py b/api/funkwhale_api/music/utils.py
index 5031e69ba8..571bc4ddd4 100644
--- a/api/funkwhale_api/music/utils.py
+++ b/api/funkwhale_api/music/utils.py
@@ -12,7 +12,7 @@ def guess_mimetype(f):
     t = magic.from_buffer(f.read(b), mime=True)
     if not t.startswith("audio/"):
         # failure, we try guessing by extension
-        mt, _ = mimetypes.guess_type(f.path)
+        mt, _ = mimetypes.guess_type(f.name)
         if mt:
             t = mt
     return t
diff --git a/api/tests/music/test_utils.py b/api/tests/music/test_utils.py
index 87eaddc435..982422c34a 100644
--- a/api/tests/music/test_utils.py
+++ b/api/tests/music/test_utils.py
@@ -36,3 +36,12 @@ def test_get_audio_file_data(name, expected):
         result = utils.get_audio_file_data(f)
 
     assert result == expected
+
+
+def test_guess_mimetype_dont_crash_with_s3(factories, mocker, settings):
+    """See #857"""
+    settings.DEFAULT_FILE_STORAGE = "funkwhale_api.common.storage.ASCIIS3Boto3Storage"
+    mocker.patch("magic.from_buffer", return_value="none")
+    f = factories["music.Upload"].build(audio_file__filename="test.mp3")
+
+    assert utils.guess_mimetype(f.audio_file) == "audio/mpeg"
diff --git a/changes/changelog.d/857.bugfix b/changes/changelog.d/857.bugfix
new file mode 100644
index 0000000000..5d525e3d9f
--- /dev/null
+++ b/changes/changelog.d/857.bugfix
@@ -0,0 +1 @@
+Fix broken upload for specific files when using S3 storage (#857)
-- 
GitLab