From 18ad6cfa7871ee5e0365f9f586ac5e1de63c50b0 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Sun, 3 Jun 2018 18:57:35 +0200
Subject: [PATCH] Fix #273: fix_track_files will now update files with bad
 mimetype

---
 .../management/commands/fix_track_files.py    |  4 +--
 api/tests/music/test_commands.py              | 28 +++++++++++++++++++
 changes/changelog.d/273.enhancement           |  2 ++
 3 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 changes/changelog.d/273.enhancement

diff --git a/api/funkwhale_api/music/management/commands/fix_track_files.py b/api/funkwhale_api/music/management/commands/fix_track_files.py
index 9adc1b9b..c18e2b25 100644
--- a/api/funkwhale_api/music/management/commands/fix_track_files.py
+++ b/api/funkwhale_api/music/management/commands/fix_track_files.py
@@ -33,9 +33,9 @@ class Command(BaseCommand):
     def fix_mimetypes(self, dry_run, **kwargs):
         self.stdout.write('Fixing missing mimetypes...')
         matching = models.TrackFile.objects.filter(
-            source__startswith='file://', mimetype=None)
+            source__startswith='file://').exclude(mimetype__startswith='audio/')
         self.stdout.write(
-            '[mimetypes] {} entries found with no mimetype'.format(
+            '[mimetypes] {} entries found with bad or no mimetype'.format(
                 matching.count()))
         for extension, mimetype in utils.EXTENSION_TO_MIMETYPE.items():
             qs = matching.filter(source__endswith='.{}'.format(extension))
diff --git a/api/tests/music/test_commands.py b/api/tests/music/test_commands.py
index ff3343aa..6f03f6b8 100644
--- a/api/tests/music/test_commands.py
+++ b/api/tests/music/test_commands.py
@@ -1,5 +1,9 @@
+import os
+
 from funkwhale_api.music.management.commands import fix_track_files
 
+DATA_DIR = os.path.dirname(os.path.abspath(__file__))
+
 
 def test_fix_track_files_bitrate_length(factories, mocker):
     tf1 = factories['music.TrackFile'](bitrate=1, duration=2)
@@ -43,3 +47,27 @@ def test_fix_track_files_size(factories, mocker):
 
     # updated
     assert tf2.size == 2
+
+
+def test_fix_track_files_mimetype(factories, mocker):
+    name = 'test.mp3'
+    mp3_path = os.path.join(DATA_DIR, 'test.mp3')
+    ogg_path = os.path.join(DATA_DIR, 'test.ogg')
+    tf1 = factories['music.TrackFile'](
+        audio_file__from_path=mp3_path,
+        source='file://{}'.format(mp3_path),
+        mimetype='application/x-empty')
+
+    # this one already has a mimetype set, to it should not be updated
+    tf2 = factories['music.TrackFile'](
+        audio_file__from_path=ogg_path,
+        source='file://{}'.format(ogg_path),
+        mimetype='audio/something')
+    c = fix_track_files.Command()
+    c.fix_mimetypes(dry_run=False)
+
+    tf1.refresh_from_db()
+    tf2.refresh_from_db()
+
+    assert tf1.mimetype == 'audio/mpeg'
+    assert tf2.mimetype == 'audio/something'
diff --git a/changes/changelog.d/273.enhancement b/changes/changelog.d/273.enhancement
new file mode 100644
index 00000000..c1d634e0
--- /dev/null
+++ b/changes/changelog.d/273.enhancement
@@ -0,0 +1,2 @@
+fix_track_files will now update files with bad mimetype (and not only
+the one with no mimetype) (#273)
-- 
GitLab