diff --git a/api/funkwhale_api/music/metadata.py b/api/funkwhale_api/music/metadata.py index ad8a6b6a5d027160bd79464b88d5d7d6dce7bbbc..7a105e432ca441783f9778dbad6c5858aeb7f54b 100644 --- a/api/funkwhale_api/music/metadata.py +++ b/api/funkwhale_api/music/metadata.py @@ -409,8 +409,14 @@ class Metadata(object): return data - def get_picture(self, picture_type="cover_front"): - ptype = getattr(mutagen.id3.PictureType, picture_type.upper()) + def get_picture(self, *picture_types): + if not picture_types: + raise ValueError("You need to request at least one picture type") + ptypes = [ + getattr(mutagen.id3.PictureType, picture_type.upper()) + for picture_type in picture_types + ] + try: pictures = self.get("pictures") except (UnsupportedTag, TagNotFound): @@ -418,6 +424,9 @@ class Metadata(object): cleaner = self._conf.get("clean_pictures", lambda v: v) pictures = cleaner(pictures) - for p in pictures: - if p["type"] == ptype: - return p + if not pictures: + return + for ptype in ptypes: + for p in pictures: + if p["type"] == ptype: + return p diff --git a/api/funkwhale_api/music/tasks.py b/api/funkwhale_api/music/tasks.py index ea3f0f57b98cda61a78a6ec53aa9ef1e1bab2f24..76bc3897f437050297f12961bd9de551fc5460d1 100644 --- a/api/funkwhale_api/music/tasks.py +++ b/api/funkwhale_api/music/tasks.py @@ -191,7 +191,7 @@ def process_upload(upload): final_metadata = collections.ChainMap( additional_data, import_metadata, file_metadata ) - additional_data["cover_data"] = m.get_picture("cover_front") + additional_data["cover_data"] = m.get_picture("cover_front", "other") additional_data["upload_source"] = upload.source track = get_track_from_import_metadata(final_metadata) except UploadImportError as e: diff --git a/api/tests/music/test_metadata.py b/api/tests/music/test_metadata.py index 5e5590d7dba3d8696c35d8605ef3f2f818f9c8d6..f91f7545b89195a99b0072552cfa8ff164d6ca25 100644 --- a/api/tests/music/test_metadata.py +++ b/api/tests/music/test_metadata.py @@ -147,14 +147,16 @@ def test_can_get_metadata_from_id3_mp3_file(field, value): assert data.get(field) == value -@pytest.mark.parametrize("name", ["test.mp3", "sample.flac", "with_cover.ogg"]) +@pytest.mark.parametrize( + "name", ["test.mp3", "with_other_picture.mp3", "sample.flac", "with_cover.ogg"] +) def test_can_get_pictures(name): path = os.path.join(DATA_DIR, name) data = metadata.Metadata(path) pictures = data.get("pictures") assert len(pictures) == 1 - cover_data = data.get_picture("cover_front") + cover_data = data.get_picture("cover_front", "other") assert cover_data["mimetype"].startswith("image/") assert len(cover_data["content"]) > 0 assert type(cover_data["content"]) == bytes diff --git a/api/tests/music/with_other_picture.mp3 b/api/tests/music/with_other_picture.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..3118f067e37064f91f48f4265fac9745236169e0 Binary files /dev/null and b/api/tests/music/with_other_picture.mp3 differ diff --git a/changes/changelog.d/cover-other-mp3.enhancement b/changes/changelog.d/cover-other-mp3.enhancement new file mode 100644 index 0000000000000000000000000000000000000000..937a96c7b8706753cf56d9ec27ddab28cec4982d --- /dev/null +++ b/changes/changelog.d/cover-other-mp3.enhancement @@ -0,0 +1 @@ +Importer will now pick embedded images in files with OTHER type if no COVER_FRONT is present