diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py index a49b55236..d2bf7dd88 100644 --- a/api/funkwhale_api/music/models.py +++ b/api/funkwhale_api/music/models.py @@ -688,6 +688,10 @@ class Upload(models.Model): @property def extension(self): + try: + return utils.MIMETYPE_TO_EXTENSION[self.mimetype] + except KeyError: + pass if not self.audio_file: return return os.path.splitext(self.audio_file.name)[-1].replace(".", "", 1) diff --git a/api/funkwhale_api/music/utils.py b/api/funkwhale_api/music/utils.py index 2c1210cf7..ae5cda750 100644 --- a/api/funkwhale_api/music/utils.py +++ b/api/funkwhale_api/music/utils.py @@ -33,6 +33,7 @@ AUDIO_EXTENSIONS_AND_MIMETYPE = [ ("ogg", "audio/ogg"), ("mp3", "audio/mpeg"), ("flac", "audio/x-flac"), + ("flac", "audio/flac"), ] EXTENSION_TO_MIMETYPE = {ext: mt for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE} diff --git a/api/tests/music/test_models.py b/api/tests/music/test_models.py index 874c35afa..d89afd9c7 100644 --- a/api/tests/music/test_models.py +++ b/api/tests/music/test_models.py @@ -167,8 +167,7 @@ def test_audio_track_mime_type(extention, mimetype, factories): def test_upload_file_name(factories): name = "test.mp3" path = os.path.join(DATA_DIR, name) - upload = factories["music.Upload"](audio_file__from_path=path) - + upload = factories["music.Upload"](audio_file__from_path=path, mimetype=None) assert upload.filename == upload.track.full_name + ".mp3" @@ -484,3 +483,18 @@ def test_fid_is_populated(factories, model, factory_args, namespace): assert instance.fid == federation_utils.full_url( reverse(namespace, kwargs={"uuid": instance.uuid}) ) + + +@pytest.mark.parametrize( + "factory_args,expected", + [ + ({"audio_file__filename": "test.mp3", "mimetype": None}, "mp3"), + ({"mimetype": "audio/mpeg"}, "mp3"), + ({"audio_file__filename": "test.None", "mimetype": "audio/mpeg"}, "mp3"), + ({"audio_file__filename": "test.None", "mimetype": "audio/flac"}, "flac"), + ({"audio_file__filename": "test.None", "mimetype": "audio/x-flac"}, "flac"), + ], +) +def test_upload_extension(factory_args, factories, expected): + upload = factories["music.Upload"].build(**factory_args) + assert upload.extension == expected diff --git a/changes/changelog.d/473.bugfix b/changes/changelog.d/473.bugfix new file mode 100644 index 000000000..2346eb459 --- /dev/null +++ b/changes/changelog.d/473.bugfix @@ -0,0 +1 @@ +Fix ".None" extension when downloading Flac file (#473)