This commit is contained in:
Eric Masseran 2025-12-05 10:52:09 +10:00 committed by GitHub
commit 1b19818dea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 5 deletions

View file

@ -364,15 +364,17 @@ class TestHelper(ConfigMixin):
items.append(item) items.append(item)
return self.lib.add_album(items) return self.lib.add_album(items)
def create_mediafile_fixture(self, ext="mp3", images=[]): def create_mediafile_fixture(self, ext="mp3", images=[], target_dir=None):
"""Copy a fixture mediafile with the extension to `temp_dir`. """Copy a fixture mediafile with the extension to `temp_dir`.
`images` is a subset of 'png', 'jpg', and 'tiff'. For each `images` is a subset of 'png', 'jpg', and 'tiff'. For each
specified extension a cover art image is added to the media specified extension a cover art image is added to the media
file. file.
""" """
if not target_dir:
target_dir = self.temp_dir
src = os.path.join(_common.RSRC, util.bytestring_path(f"full.{ext}")) src = os.path.join(_common.RSRC, util.bytestring_path(f"full.{ext}"))
handle, path = mkstemp(dir=self.temp_dir) handle, path = mkstemp(dir=target_dir)
path = bytestring_path(path) path = bytestring_path(path)
os.close(handle) os.close(handle)
shutil.copyfile(syspath(src), syspath(path)) shutil.copyfile(syspath(src), syspath(path))

View file

@ -206,9 +206,14 @@ def extract_first(log, outpath, items):
return real_path return real_path
def clear_item(item, log):
if mediafile.MediaFile(syspath(item.path)).images:
log.debug("Clearing art for {}", item)
item.try_write(tags={"images": None})
def clear(log, lib, query): def clear(log, lib, query):
items = lib.items(query) items = lib.items(query)
log.info("Clearing album art from {} items", len(items)) log.info("Clearing album art from {} items", len(items))
for item in items: for item in items:
log.debug("Clearing art for {}", item) clear_item(item, log)
item.try_write(tags={"images": None})

View file

@ -62,6 +62,7 @@ class EmbedCoverArtPlugin(BeetsPlugin):
"ifempty": False, "ifempty": False,
"remove_art_file": False, "remove_art_file": False,
"quality": 0, "quality": 0,
"clearart_on_import": False,
} }
) )
@ -82,6 +83,9 @@ class EmbedCoverArtPlugin(BeetsPlugin):
self.register_listener("art_set", self.process_album) self.register_listener("art_set", self.process_album)
if self.config["clearart_on_import"].get(bool):
self.register_listener("import_task_files", self.import_task_files)
def commands(self): def commands(self):
# Embed command. # Embed command.
embed_cmd = ui.Subcommand( embed_cmd = ui.Subcommand(
@ -278,3 +282,9 @@ class EmbedCoverArtPlugin(BeetsPlugin):
os.remove(syspath(album.artpath)) os.remove(syspath(album.artpath))
album.artpath = None album.artpath = None
album.store() album.store()
def import_task_files(self, session, task):
"""Automatically clearart of imported files."""
for item in task.imported_items():
self._log.debug("clearart-on-import {.filepath}", item)
art.clear_item(item, self._log)

View file

@ -28,6 +28,7 @@ from beets.test import _common
from beets.test.helper import ( from beets.test.helper import (
BeetsTestCase, BeetsTestCase,
FetchImageHelper, FetchImageHelper,
ImportHelper,
IOMixin, IOMixin,
PluginMixin, PluginMixin,
) )
@ -75,7 +76,9 @@ def require_artresizer_compare(test):
return wrapper return wrapper
class EmbedartCliTest(IOMixin, PluginMixin, FetchImageHelper, BeetsTestCase): class EmbedartCliTest(
ImportHelper, IOMixin, PluginMixin, FetchImageHelper, BeetsTestCase
):
plugin = "embedart" plugin = "embedart"
small_artpath = os.path.join(_common.RSRC, b"image-2x3.jpg") small_artpath = os.path.join(_common.RSRC, b"image-2x3.jpg")
abbey_artpath = os.path.join(_common.RSRC, b"abbey.jpg") abbey_artpath = os.path.join(_common.RSRC, b"abbey.jpg")
@ -225,10 +228,20 @@ class EmbedartCliTest(IOMixin, PluginMixin, FetchImageHelper, BeetsTestCase):
item = album.items()[0] item = album.items()[0]
self.io.addinput("y") self.io.addinput("y")
self.run_command("embedart", "-f", self.small_artpath) self.run_command("embedart", "-f", self.small_artpath)
embedded_time = os.path.getmtime(syspath(item.path))
self.io.addinput("y") self.io.addinput("y")
self.run_command("clearart") self.run_command("clearart")
mediafile = MediaFile(syspath(item.path)) mediafile = MediaFile(syspath(item.path))
assert not mediafile.images assert not mediafile.images
clear_time = os.path.getmtime(syspath(item.path))
assert clear_time > embedded_time
# A run on a file without an image should not be modified
self.io.addinput("y")
self.run_command("clearart")
no_clear_time = os.path.getmtime(syspath(item.path))
assert no_clear_time == clear_time
def test_clear_art_with_no_input(self): def test_clear_art_with_no_input(self):
self._setup_data() self._setup_data()
@ -273,6 +286,32 @@ class EmbedartCliTest(IOMixin, PluginMixin, FetchImageHelper, BeetsTestCase):
mediafile = MediaFile(syspath(item.path)) mediafile = MediaFile(syspath(item.path))
assert not mediafile.images assert not mediafile.images
def test_clearart_on_import_disabled(self):
file_path = self.create_mediafile_fixture(
images=["jpg"], target_dir=self.import_path
)
self.import_media.append(file_path)
with self.configure_plugin({"clearart_on_import": False}):
importer = self.setup_importer(autotag=False, write=True)
importer.run()
item = self.lib.items()[0]
assert MediaFile(os.path.join(item.path)).images
def test_clearart_on_import_enabled(self):
file_path = self.create_mediafile_fixture(
images=["jpg"], target_dir=self.import_path
)
self.import_media.append(file_path)
# Force re-init the plugin to register the listener
self.unload_plugins()
with self.configure_plugin({"clearart_on_import": True}):
importer = self.setup_importer(autotag=False, write=True)
importer.run()
item = self.lib.items()[0]
assert not MediaFile(os.path.join(item.path)).images
class DummyArtResizer(ArtResizer): class DummyArtResizer(ArtResizer):
"""An `ArtResizer` which pretends that ImageMagick is available, and has """An `ArtResizer` which pretends that ImageMagick is available, and has