feat: 优先使用当前文件标签作为曲目标题,避免重名问题

This commit is contained in:
jxxghp
2026-08-09 11:56:37 +08:00
parent 0c1b9c0416
commit 372b9bc60f
2 changed files with 34 additions and 8 deletions

View File

@@ -1084,12 +1084,10 @@ class TransferChain(ChainBase, ConfigReloadMixin, metaclass=Singleton):
file_meta = deepcopy(saved_meta)
file_meta.org_string = file_path.name
if file_tags:
has_tag_identity = bool(
file_tags.artists
or file_tags.album
or file_tags.track_number
or file_tags.isrc
)
# 曲目标题始终优先使用当前文件自身的标签(缺失时回退为文件名),
# 防止整包目录继续沿用订阅/下载标题(单曲名、专辑名等)导致所有文件重名。
if file_tags.title:
file_meta.title = file_tags.title
for field_name in (
"artists",
"album",
@@ -1104,8 +1102,6 @@ class TransferChain(ChainBase, ConfigReloadMixin, metaclass=Singleton):
):
if getattr(file_tags, field_name, None):
setattr(file_meta, field_name, deepcopy(getattr(file_tags, field_name)))
if has_tag_identity and file_tags.title:
file_meta.title = file_tags.title
for field_name in (
"audio_format",
"bit_depth",

View File

@@ -94,6 +94,36 @@ def test_restore_music_context_from_download_history():
assert restored_info.album == "Random Access Memories"
def test_restore_music_context_uses_file_title_over_subscription_title(tmp_path, monkeypatch):
"""曲目标题应优先取当前文件自身的标签/文件名,而非沿用订阅时的单曲标题。"""
meta, info = _music_context()
history = SimpleNamespace(
note={
"music": {
"version": 1,
"meta": meta.to_dict(),
"media": info.to_dict(),
}
}
)
# 订阅标题被识别为单曲"Get Lucky",而实际音轨标签曲目名 不同且无身份字段
audio_file = tmp_path / "07.幸福.flac"
audio_file.write_bytes(b"fake-flac")
from app.helper.audio import AudioMetadataHelper
monkeypatch.setattr(
AudioMetadataHelper,
"read",
lambda path: MetaMusic(org_string=path.name, title="流浪地图"),
)
restored_meta, _ = TransferChain._restore_music_download_context(history, audio_file)
assert restored_meta is not None
assert restored_meta.title == "流浪地图"
def test_job_manager_serializes_music_queue_models():
"""整理队列应使用音乐专属 Schema 序列化任务。"""
meta, info = _music_context()