From 85bc8d93e87a73baee6d11e6028e73c45e96a83b Mon Sep 17 00:00:00 2001
From: Agate <me@agate.blue>
Date: Mon, 4 May 2020 13:52:25 +0200
Subject: [PATCH] Fix #1093: mimetype detection issue that broke transcoding on
 certain tracks

---
 .../music/management/commands/fix_uploads.py  | 36 +++++++++++++++----
 api/funkwhale_api/music/utils.py              |  2 ++
 api/tests/music/test_views.py                 |  1 +
 changes/changelog.d/1093.bugfix               |  1 +
 4 files changed, 34 insertions(+), 6 deletions(-)
 create mode 100644 changes/changelog.d/1093.bugfix

diff --git a/api/funkwhale_api/music/management/commands/fix_uploads.py b/api/funkwhale_api/music/management/commands/fix_uploads.py
index 94f8dd21c..582a837c4 100644
--- a/api/funkwhale_api/music/management/commands/fix_uploads.py
+++ b/api/funkwhale_api/music/management/commands/fix_uploads.py
@@ -16,20 +16,44 @@ class Command(BaseCommand):
             default=False,
             help="Do not execute anything",
         )
+        parser.add_argument(
+            "--mimetypes",
+            action="store_true",
+            dest="mimetypes",
+            default=True,
+            help="Check and fix mimetypes",
+        )
+        parser.add_argument(
+            "--audio-data",
+            action="store_true",
+            dest="data",
+            default=False,
+            help="Check and fix bitrate and duration, can be really slow because it needs to access files",
+        )
+        parser.add_argument(
+            "--size",
+            action="store_true",
+            dest="size",
+            default=False,
+            help="Check and fix file size, can be really slow because it needs to access files",
+        )
 
     def handle(self, *args, **options):
         if options["dry_run"]:
             self.stdout.write("Dry-run on, will not commit anything")
-        self.fix_mimetypes(**options)
-        self.fix_file_data(**options)
-        self.fix_file_size(**options)
+        if options["mimetypes"]:
+            self.fix_mimetypes(**options)
+        if options["data"]:
+            self.fix_file_data(**options)
+        if options["size"]:
+            self.fix_file_size(**options)
 
     @transaction.atomic
     def fix_mimetypes(self, dry_run, **kwargs):
         self.stdout.write("Fixing missing mimetypes...")
-        matching = models.Upload.objects.filter(source__startswith="file://").exclude(
-            mimetype__startswith="audio/"
-        )
+        matching = models.Upload.objects.filter(
+            Q(source__startswith="file://") | Q(source__startswith="upload://")
+        ).exclude(mimetype__startswith="audio/")
         self.stdout.write(
             "[mimetypes] {} entries found with bad or no mimetype".format(
                 matching.count()
diff --git a/api/funkwhale_api/music/utils.py b/api/funkwhale_api/music/utils.py
index 14f245aaa..64a7c24f8 100644
--- a/api/funkwhale_api/music/utils.py
+++ b/api/funkwhale_api/music/utils.py
@@ -22,6 +22,8 @@ def guess_mimetype(f):
         mt, _ = mimetypes.guess_type(f.name)
         if mt:
             t = mt
+        else:
+            t = EXTENSION_TO_MIMETYPE.get(f.name.split(".")[-1])
     return t
 
 
diff --git a/api/tests/music/test_views.py b/api/tests/music/test_views.py
index db39f571f..3d79c0eba 100644
--- a/api/tests/music/test_views.py
+++ b/api/tests/music/test_views.py
@@ -824,6 +824,7 @@ def test_user_can_create_draft_upload(
     assert upload.source == "upload://test"
     assert upload.import_reference == "test"
     assert upload.import_status == "draft"
+    assert upload.mimetype == "audio/ogg"
     assert upload.track is None
     m.assert_not_called()
 
diff --git a/changes/changelog.d/1093.bugfix b/changes/changelog.d/1093.bugfix
new file mode 100644
index 000000000..1d9a15e59
--- /dev/null
+++ b/changes/changelog.d/1093.bugfix
@@ -0,0 +1 @@
+Fixed mimetype detection issue that broke transcoding on some tracks (#1093). Run ``python manage.py fix_uploads --mimetypes`` to set proper mimetypes on existing uploads.
-- 
GitLab