From d1f7ba022bcd3b82c35b3c09efa624a4681f3ece Mon Sep 17 00:00:00 2001 From: Alan Collins Date: Sat, 9 Mar 2024 22:53:02 -0600 Subject: [PATCH] New: 'Custom Format: Format Name' rename token (cherry picked from commit 48cb5d227187a06930aad5ee1b4e7b76422d8421) --- .../MediaManagement/Naming/NamingModal.js | 3 +- .../CustomFormatsFixture.cs | 122 ++++++++++++++++++ .../CustomFormats/CustomFormat.cs | 2 +- .../Organizer/FileNameBuilder.cs | 11 +- 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 src/NzbDrone.Core.Test/OrganizerTests/FileNameBuilderTests/CustomFormatsFixture.cs diff --git a/frontend/src/Settings/MediaManagement/Naming/NamingModal.js b/frontend/src/Settings/MediaManagement/Naming/NamingModal.js index dec15893f..7c54740de 100644 --- a/frontend/src/Settings/MediaManagement/Naming/NamingModal.js +++ b/frontend/src/Settings/MediaManagement/Naming/NamingModal.js @@ -142,7 +142,8 @@ const mediaInfoTokens = [ const otherTokens = [ { token: '{Release Group}', example: 'Rls Grp' }, - { token: '{Custom Formats}', example: 'iNTERNAL' } + { token: '{Custom Formats}', example: 'iNTERNAL' }, + { token: '{Custom Format:FormatName}', example: 'AMZN' } ]; const originalTokens = [ diff --git a/src/NzbDrone.Core.Test/OrganizerTests/FileNameBuilderTests/CustomFormatsFixture.cs b/src/NzbDrone.Core.Test/OrganizerTests/FileNameBuilderTests/CustomFormatsFixture.cs new file mode 100644 index 000000000..fb644e104 --- /dev/null +++ b/src/NzbDrone.Core.Test/OrganizerTests/FileNameBuilderTests/CustomFormatsFixture.cs @@ -0,0 +1,122 @@ +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.CustomFormats; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.Organizer; +using NzbDrone.Core.Qualities; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.OrganizerTests.FileNameBuilderTests +{ + [TestFixture] + + public class CustomFormatsFixture : CoreTest + { + private Series _series; + private Episode _episode1; + private EpisodeFile _episodeFile; + private NamingConfig _namingConfig; + + private List _customFormats; + + [SetUp] + public void Setup() + { + _series = Builder + .CreateNew() + .With(s => s.Title = "South Park") + .Build(); + + _namingConfig = NamingConfig.Default; + _namingConfig.RenameEpisodes = true; + + Mocker.GetMock() + .Setup(c => c.GetConfig()).Returns(_namingConfig); + + _episode1 = Builder.CreateNew() + .With(e => e.Title = "City Sushi") + .With(e => e.SeasonNumber = 15) + .With(e => e.EpisodeNumber = 6) + .With(e => e.AbsoluteEpisodeNumber = 100) + .Build(); + + _episodeFile = new EpisodeFile { Quality = new QualityModel(Quality.HDTV720p), ReleaseGroup = "SonarrTest" }; + + _customFormats = new List() + { + new CustomFormat() + { + Name = "INTERNAL", + IncludeCustomFormatWhenRenaming = true + }, + new CustomFormat() + { + Name = "AMZN", + IncludeCustomFormatWhenRenaming = true + }, + new CustomFormat() + { + Name = "NAME WITH SPACES", + IncludeCustomFormatWhenRenaming = true + }, + new CustomFormat() + { + Name = "NotIncludedFormat", + IncludeCustomFormatWhenRenaming = false + } + }; + + Mocker.GetMock() + .Setup(v => v.Get(Moq.It.IsAny())) + .Returns(v => Quality.DefaultQualityDefinitions.First(c => c.Quality == v)); + } + + [TestCase("{Custom Formats}", "INTERNAL AMZN NAME WITH SPACES")] + public void should_replace_custom_formats(string format, string expected) + { + _namingConfig.StandardEpisodeFormat = format; + + Subject.BuildFileName(new List { _episode1 }, _series, _episodeFile, customFormats: _customFormats) + .Should().Be(expected); + } + + [TestCase("{Custom Formats}", "")] + public void should_replace_custom_formats_with_no_custom_formats(string format, string expected) + { + _namingConfig.StandardEpisodeFormat = format; + + Subject.BuildFileName(new List { _episode1 }, _series, _episodeFile, customFormats: new List()) + .Should().Be(expected); + } + + [TestCase("{Custom Format}", "")] + [TestCase("{Custom Format:INTERNAL}", "INTERNAL")] + [TestCase("{Custom Format:AMZN}", "AMZN")] + [TestCase("{Custom Format:NAME WITH SPACES}", "NAME WITH SPACES")] + [TestCase("{Custom Format:DOESNOTEXIST}", "")] + [TestCase("{Custom Format:INTERNAL} - {Custom Format:AMZN}", "INTERNAL - AMZN")] + [TestCase("{Custom Format:AMZN} - {Custom Format:INTERNAL}", "AMZN - INTERNAL")] + public void should_replace_custom_format(string format, string expected) + { + _namingConfig.StandardEpisodeFormat = format; + + Subject.BuildFileName(new List { _episode1 }, _series, _episodeFile, customFormats: _customFormats) + .Should().Be(expected); + } + + [TestCase("{Custom Format}", "")] + [TestCase("{Custom Format:INTERNAL}", "")] + [TestCase("{Custom Format:AMZN}", "")] + public void should_replace_custom_format_with_no_custom_formats(string format, string expected) + { + _namingConfig.StandardEpisodeFormat = format; + + Subject.BuildFileName(new List { _episode1 }, _series, _episodeFile, customFormats: new List()) + .Should().Be(expected); + } + } +} diff --git a/src/NzbDrone.Core/CustomFormats/CustomFormat.cs b/src/NzbDrone.Core/CustomFormats/CustomFormat.cs index 51e61e287..8c58b1e07 100644 --- a/src/NzbDrone.Core/CustomFormats/CustomFormat.cs +++ b/src/NzbDrone.Core/CustomFormats/CustomFormat.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Datastore; diff --git a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs index abfa9399d..e50a77058 100644 --- a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs +++ b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs @@ -34,7 +34,7 @@ namespace NzbDrone.Core.Organizer private readonly ICached _absoluteTrackFormatCache; private readonly Logger _logger; - private static readonly Regex TitleRegex = new Regex(@"(?\{\{|\}\})|\{(?[- ._\[(]*)(?(?:[a-z0-9]+)(?:(?[- ._]+)(?:[a-z0-9]+))?)(?::(?[a-z0-9+-]+(?[- ._)\]]*)\}", + private static readonly Regex TitleRegex = new Regex(@"(?\{\{|\}\})|\{(?[- ._\[(]*)(?(?:[a-z0-9]+)(?:(?[- ._]+)(?:[a-z0-9]+))?)(?::(?[ a-z0-9+-]+(?[- ._)\]]*)\}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); public static readonly Regex TrackRegex = new Regex(@"(?\{track(?:\:0+)?})", @@ -427,6 +427,15 @@ namespace NzbDrone.Core.Organizer } tokenHandlers["{Custom Formats}"] = m => string.Join(" ", customFormats.Where(x => x.IncludeCustomFormatWhenRenaming)); + tokenHandlers["{Custom Format}"] = m => + { + if (m.CustomFormat.IsNullOrWhiteSpace()) + { + return string.Empty; + } + + return customFormats.Where(x => x.IncludeCustomFormatWhenRenaming && x.Name == m.CustomFormat).FirstOrDefault()?.ToString() ?? string.Empty; + }; } private string ReplaceTokens(string pattern, Dictionary> tokenHandlers, NamingConfig namingConfig, bool escape = false)