New: Accept ':##' on renaming tokens to allow specifying a maximum length for movie titles and release group

(cherry picked from commit 19db75b36beaa5e549d903b136dbda300f1f8562)

Closes #9713
pull/9731/head
Mark McDowall 10 months ago committed by Bogdan
parent adf647f3e1
commit 422db874f0

@ -0,0 +1,17 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Common.Test.ExtensionTests.StringExtensionTests
{
[TestFixture]
public class ReverseFixture
{
[TestCase("input", "tupni")]
[TestCase("racecar", "racecar")]
public void should_reverse_string(string input, string expected)
{
input.Reverse().Should().Be(expected);
}
}
}

@ -219,5 +219,14 @@ namespace NzbDrone.Common.Extensions
{
return input.Contains(':') ? $"[{input}]" : input;
}
public static string Reverse(this string text)
{
var array = text.ToCharArray();
Array.Reverse(array);
return new string(array);
}
}
}

@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.CustomFormats;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.OrganizerTests.FileNameBuilderTests
{
[TestFixture]
public class TruncatedMovieTitleFixture : CoreTest<FileNameBuilder>
{
private Movie _movie;
private NamingConfig _namingConfig;
[SetUp]
public void Setup()
{
_movie = Builder<Movie>
.CreateNew()
.With(s => s.Title = "Movie Title")
.Build();
_namingConfig = NamingConfig.Default;
_namingConfig.RenameMovies = true;
Mocker.GetMock<INamingConfigService>()
.Setup(c => c.GetConfig()).Returns(_namingConfig);
Mocker.GetMock<IQualityDefinitionService>()
.Setup(v => v.Get(Moq.It.IsAny<Quality>()))
.Returns<Quality>(v => Quality.DefaultQualityDefinitions.First(c => c.Quality == v));
Mocker.GetMock<ICustomFormatService>()
.Setup(v => v.All())
.Returns(new List<CustomFormat>());
}
[TestCase("{Movie Title:16}", "The Fantastic...")]
[TestCase("{Movie TitleThe:17}", "Fantastic Life...")]
[TestCase("{Movie CleanTitle:-13}", "...Mr. Sisko")]
public void should_truncate_series_title(string format, string expected)
{
_movie.Title = "The Fantastic Life of Mr. Sisko";
_namingConfig.MovieFolderFormat = format;
var result = Subject.GetMovieFolder(_movie, _namingConfig);
result.Should().Be(expected);
}
}
}

@ -0,0 +1,82 @@
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.Movies;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.OrganizerTests.FileNameBuilderTests
{
[TestFixture]
public class TruncatedReleaseGroupFixture : CoreTest<FileNameBuilder>
{
private Movie _movie;
private MovieFile _movieFile;
private NamingConfig _namingConfig;
[SetUp]
public void Setup()
{
_movie = Builder<Movie>
.CreateNew()
.With(s => s.Title = "Movie Title")
.With(s => s.Year = 2024)
.Build();
_namingConfig = NamingConfig.Default;
_namingConfig.RenameMovies = true;
Mocker.GetMock<INamingConfigService>()
.Setup(c => c.GetConfig()).Returns(_namingConfig);
_movieFile = new MovieFile { Quality = new QualityModel(Quality.HDTV720p), ReleaseGroup = "RadarrTest" };
Mocker.GetMock<IQualityDefinitionService>()
.Setup(v => v.Get(Moq.It.IsAny<Quality>()))
.Returns<Quality>(v => Quality.DefaultQualityDefinitions.First(c => c.Quality == v));
Mocker.GetMock<ICustomFormatService>()
.Setup(v => v.All())
.Returns(new List<CustomFormat>());
}
private void GivenProper()
{
_movieFile.Quality.Revision.Version = 2;
}
[Test]
public void should_truncate_from_beginning()
{
_movie.Title = "The Fantastic Life of Mr. Sisko";
_movieFile.Quality.Quality = Quality.Bluray1080p;
_movieFile.ReleaseGroup = "IWishIWasALittleBitTallerIWishIWasABallerIWishIHadAGirlWhoLookedGoodIWouldCallHerIWishIHadARabbitInAHatWithABatAndASixFourImpala";
_namingConfig.StandardMovieFormat = "{Movie Title} ({Release Year}) {Quality Full}-{ReleaseGroup:12}";
var result = Subject.BuildFileName(_movie, _movieFile);
result.Length.Should().BeLessOrEqualTo(255);
result.Should().Be("The Fantastic Life of Mr. Sisko (2024) Bluray-1080p-IWishIWas...");
}
[Test]
public void should_truncate_from_from_end()
{
_movie.Title = "The Fantastic Life of Mr. Sisko";
_movieFile.Quality.Quality = Quality.Bluray1080p;
_movieFile.ReleaseGroup = "IWishIWasALittleBitTallerIWishIWasABallerIWishIHadAGirlWhoLookedGoodIWouldCallHerIWishIHadARabbitInAHatWithABatAndASixFourImpala";
_namingConfig.StandardMovieFormat = "{Movie Title} ({Release Year}) {Quality Full}-{ReleaseGroup:-17}";
var result = Subject.BuildFileName(_movie, _movieFile);
result.Length.Should().BeLessOrEqualTo(255);
result.Should().Be("The Fantastic Life of Mr. Sisko (2024) Bluray-1080p-...ASixFourImpala");
}
}
}

@ -134,6 +134,7 @@ namespace NzbDrone.Core.Organizer
component = FileNameCleanupRegex.Replace(component, match => match.Captures[0].Value[0].ToString());
component = TrimSeparatorsRegex.Replace(component, string.Empty);
component = component.Replace("{ellipsis}", "...");
component = ReplaceReservedDeviceNames(component);
if (component.IsNotNullOrWhiteSpace())
@ -197,6 +198,7 @@ namespace NzbDrone.Core.Organizer
var component = ReplaceTokens(splitPattern, tokenHandlers, namingConfig);
component = CleanFolderName(component);
component = component.Replace("{ellipsis}", "...");
component = ReplaceReservedDeviceNames(component);
if (component.IsNotNullOrWhiteSpace())
@ -264,15 +266,15 @@ namespace NzbDrone.Core.Organizer
private void AddMovieTokens(Dictionary<string, Func<TokenMatch, string>> tokenHandlers, Movie movie)
{
tokenHandlers["{Movie Title}"] = m => GetLanguageTitle(movie, m.CustomFormat);
tokenHandlers["{Movie CleanTitle}"] = m => CleanTitle(GetLanguageTitle(movie, m.CustomFormat));
tokenHandlers["{Movie TitleThe}"] = m => TitleThe(movie.Title);
tokenHandlers["{Movie Title}"] = m => Truncate(GetLanguageTitle(movie, m.CustomFormat), m.CustomFormat);
tokenHandlers["{Movie CleanTitle}"] = m => Truncate(CleanTitle(GetLanguageTitle(movie, m.CustomFormat)), m.CustomFormat);
tokenHandlers["{Movie TitleThe}"] = m => Truncate(TitleThe(movie.Title), m.CustomFormat);
tokenHandlers["{Movie TitleFirstCharacter}"] = m => TitleFirstCharacter(TitleThe(GetLanguageTitle(movie, m.CustomFormat)));
tokenHandlers["{Movie OriginalTitle}"] = m => movie.MovieMetadata.Value.OriginalTitle ?? string.Empty;
tokenHandlers["{Movie CleanOriginalTitle}"] = m => CleanTitle(movie.MovieMetadata.Value.OriginalTitle ?? string.Empty);
tokenHandlers["{Movie OriginalTitle}"] = m => Truncate(movie.MovieMetadata.Value.OriginalTitle, m.CustomFormat) ?? string.Empty;
tokenHandlers["{Movie CleanOriginalTitle}"] = m => Truncate(CleanTitle(movie.MovieMetadata.Value.OriginalTitle ?? string.Empty), m.CustomFormat);
tokenHandlers["{Movie Certification}"] = m => movie.MovieMetadata.Value.Certification ?? string.Empty;
tokenHandlers["{Movie Collection}"] = m => movie.MovieMetadata.Value.CollectionTitle ?? string.Empty;
tokenHandlers["{Movie Collection}"] = m => Truncate(movie.MovieMetadata.Value.CollectionTitle, m.CustomFormat) ?? string.Empty;
}
private string GetLanguageTitle(Movie movie, string isoCodes)
@ -306,7 +308,7 @@ namespace NzbDrone.Core.Organizer
{
if (movieFile.Edition.IsNotNullOrWhiteSpace())
{
tokenHandlers["{Edition Tags}"] = m => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(movieFile.Edition.ToLower());
tokenHandlers["{Edition Tags}"] = m => Truncate(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(movieFile.Edition.ToLower()), m.CustomFormat);
}
}
@ -331,8 +333,7 @@ namespace NzbDrone.Core.Organizer
{
tokenHandlers["{Original Title}"] = m => GetOriginalTitle(movieFile, multipleTokens);
tokenHandlers["{Original Filename}"] = m => GetOriginalFileName(movieFile, multipleTokens);
tokenHandlers["{Release Group}"] = m => movieFile.ReleaseGroup ?? m.DefaultValue("Radarr");
tokenHandlers["{Release Group}"] = m => Truncate(movieFile.ReleaseGroup, m.CustomFormat) ?? m.DefaultValue("Radarr");
}
private void AddQualityTokens(Dictionary<string, Func<TokenMatch, string>> tokenHandlers, Movie movie, MovieFile movieFile)
@ -623,6 +624,30 @@ namespace NzbDrone.Core.Organizer
// Replace reserved windows device names with an alternative
return ReservedDeviceNamesRegex.Replace(input, match => match.Value.Replace(".", "_"));
}
private string Truncate(string input, string formatter)
{
var maxLength = GetMaxLengthFromFormatter(formatter);
if (maxLength == 0 || input.Length <= Math.Abs(maxLength))
{
return input;
}
if (maxLength < 0)
{
return $"{{ellipsis}}{input.Reverse().Truncate(Math.Abs(maxLength) - 3).TrimEnd(' ', '.').Reverse()}";
}
return $"{input.Truncate(maxLength - 3).TrimEnd(' ', '.')}{{ellipsis}}";
}
private int GetMaxLengthFromFormatter(string formatter)
{
int.TryParse(formatter, out var maxCustomLength);
return maxCustomLength;
}
}
internal sealed class TokenMatch

Loading…
Cancel
Save