Skip to content

fix(art): truncate overlong album art filenames - #7204

Closed
Taiwudao wants to merge 1 commit into
beetbox:masterfrom
Taiwudao:fix/art-filename-truncation
Closed

Taiwudao wants to merge 1 commit into
beetbox:masterfrom
Taiwudao:fix/art-filename-truncation

Conversation

@Taiwudao

Copy link
Copy Markdown

Album.art_destination() sanitizes the rendered art_filename but never truncates it. Item.destination() runs the same kind of template through util.legalize_path(), which sanitizes, appends the extension and truncates each component - so tracks and cover art disagree once the name gets long.

Reproduction (Windows 10, CPython 3.12)

import beets, tempfile, os
from beets import library
from beets.util import syspath

base = tempfile.mkdtemp()
lib = library.Library(os.path.join(base, "test.db"))
beets.config["art_filename"] = ""

album = library.Album(album="A" * 264, albumartist="Artist")
lib.add(album)

dest = album.art_destination(b"cover.jpg", item_dir=base.encode())
print(len(os.path.basename(dest)))   # 268  -> component limit is 255
open(syspath(dest), "wb").write(b"x")

Before:

文件名长度: 268
open(): OSError: [Errno 22] Invalid argument

The track files for the same album are shortened correctly (Item.destination() truncates), so the import succeeds for the tracks and then fails on the cover.

Change

art_destination() now uses util.legalize_path() instead of calling util.sanitize_path() by hand and appending the extension itself. That is the same helper Item.destination() already uses, so both paths get identical sanitizing and truncation.

-        subpath = util.sanitize_path(subpath, replacements=self.db.replacements)
-        subpath_bytes = bytestring_path(subpath)
-
         _, ext = os.path.splitext(image)
-        dest = os.path.join(item_dir, subpath_bytes + ext)
+        subpath, _ = util.legalize_path(subpath, self.db.replacements, ext)
+        subpath_bytes = bytestring_path(subpath)
+        dest = os.path.join(item_dir, subpath_bytes)

Tests

Added TestArtDestination::test_art_filename_is_truncated: sets art_filename: $album, uses a 300-char album title, asserts the resulting component fits util.get_max_filename_length()` and keeps its extension.

pytest test/test_library.py::TestArtDestination 4 passed
pytest test/test_library.py 216 passed
pytest test/test_files.py 37 passed, 10 skipped
reverse check removing the change fails test_art_filename_is_truncated at the length assertion

A changelog entry is included under Unreleased -> Bug fixes.

@Taiwudao
Taiwudao requested a review from a team as a code owner November 21, 2026 05:56
Album.art_destination() sanitized the rendered art_filename but never
truncated it, while Item.destination() puts the same template through
util.legalize_path(), which sanitizes, appends the extension and then
truncates each component.

With a template like `art_filename: `` and a long album title, the
cover file name could exceed the filesystem limit. On Windows a 264-char
album name produced a 268-char component and copying the image failed:

    OSError: [Errno 22] Invalid argument

while the tracks in the same directory were shortened correctly.

Route the album art name through util.legalize_path() as well, so both stay
consistent.
@snejus

snejus commented Nov 21, 2026

Copy link
Copy Markdown
Member

See our AI Policy.

@snejus snejus closed this Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants