From e1ac30ba17a1d8ad4e21f4fd80f837a7329c1604 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sat, 31 Aug 2024 13:58:53 +0200 Subject: [PATCH] Fix alt version name generation Instead of replacing all occurrences of the containing folder name, just check the start of the string. This matches what happens in VideoListResolver.IsEligibleForMultiVersion Fixes #12555 --- MediaBrowser.Controller/Entities/BaseItem.cs | 17 ++++++----- .../Entities/BaseItemTests.cs | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 8bd4fb4f3..f16558d1e 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -1180,28 +1180,29 @@ namespace MediaBrowser.Controller.Entities return info; } - private string GetMediaSourceName(BaseItem item) + internal string GetMediaSourceName(BaseItem item) { var terms = new List(); var path = item.Path; if (item.IsFileProtocol && !string.IsNullOrEmpty(path)) { + var displayName = System.IO.Path.GetFileNameWithoutExtension(path); if (HasLocalAlternateVersions) { - var displayName = System.IO.Path.GetFileNameWithoutExtension(path) - .Replace(System.IO.Path.GetFileName(ContainingFolderPath), string.Empty, StringComparison.OrdinalIgnoreCase) - .TrimStart(new char[] { ' ', '-' }); - - if (!string.IsNullOrEmpty(displayName)) + var containingFolderName = System.IO.Path.GetFileName(ContainingFolderPath); + if (displayName.Length > containingFolderName.Length && displayName.StartsWith(containingFolderName, StringComparison.OrdinalIgnoreCase)) { - terms.Add(displayName); + var name = displayName.AsSpan(containingFolderName.Length).TrimStart([' ', '-']); + if (!name.IsWhiteSpace()) + { + terms.Add(name.ToString()); + } } } if (terms.Count == 0) { - var displayName = System.IO.Path.GetFileNameWithoutExtension(path); terms.Add(displayName); } } diff --git a/tests/Jellyfin.Controller.Tests/Entities/BaseItemTests.cs b/tests/Jellyfin.Controller.Tests/Entities/BaseItemTests.cs index f3ada59db..6171f12e4 100644 --- a/tests/Jellyfin.Controller.Tests/Entities/BaseItemTests.cs +++ b/tests/Jellyfin.Controller.Tests/Entities/BaseItemTests.cs @@ -1,4 +1,7 @@ using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.MediaInfo; +using Moq; using Xunit; namespace Jellyfin.Controller.Tests.Entities; @@ -14,4 +17,30 @@ public class BaseItemTests [InlineData("1test 2", "0000000001test 0000000002")] public void BaseItem_ModifySortChunks_Valid(string input, string expected) => Assert.Equal(expected, BaseItem.ModifySortChunks(input)); + + [Theory] + [InlineData("/Movies/Ted/Ted.mp4", "/Movies/Ted/Ted - Unrated Edition.mp4", "Ted", "Unrated Edition")] + [InlineData("/Movies/Deadpool 2 (2018)/Deadpool 2 (2018).mkv", "/Movies/Deadpool 2 (2018)/Deadpool 2 (2018) - Super Duper Cut.mkv", "Deadpool 2 (2018)", "Super Duper Cut")] + public void GetMediaSourceName_Valid(string primaryPath, string altPath, string name, string altName) + { + var mediaSourceManager = new Mock(); + mediaSourceManager.Setup(x => x.GetPathProtocol(It.IsAny())) + .Returns((string x) => MediaProtocol.File); + BaseItem.MediaSourceManager = mediaSourceManager.Object; + + var video = new Video() + { + Path = primaryPath + }; + + var videoAlt = new Video() + { + Path = altPath, + }; + + video.LocalAlternateVersions = [videoAlt.Path]; + + Assert.Equal(name, video.GetMediaSourceName(video)); + Assert.Equal(altName, video.GetMediaSourceName(videoAlt)); + } }