From 6d8b38366fb83174e97718e9a709458310d023d5 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 1 May 2014 13:29:09 -0700 Subject: [PATCH] Fixed: Double periods in filenames will be replaces with a single period --- .../OrganizerTests/GetNewFilenameFixture.cs | 30 +++++++++++++++++++ .../Organizer/FileNameBuilder.cs | 10 +++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/OrganizerTests/GetNewFilenameFixture.cs b/src/NzbDrone.Core.Test/OrganizerTests/GetNewFilenameFixture.cs index 67afd0a44..6e1258888 100644 --- a/src/NzbDrone.Core.Test/OrganizerTests/GetNewFilenameFixture.cs +++ b/src/NzbDrone.Core.Test/OrganizerTests/GetNewFilenameFixture.cs @@ -405,5 +405,35 @@ namespace NzbDrone.Core.Test.OrganizerTests Subject.BuildFilename(new List { episode }, new Series { Title = "30 Rock" }, _episodeFile) .Should().Be("30 Rock - S06E06 - Part 1"); } + + [Test] + public void should_replace_double_period_with_single_period() + { + _namingConfig.StandardEpisodeFormat = "{Series.Title}.S{season:00}E{episode:00}.{Episode.Title}"; + + var episode = Builder.CreateNew() + .With(e => e.Title = "Part 1") + .With(e => e.SeasonNumber = 6) + .With(e => e.EpisodeNumber = 6) + .Build(); + + Subject.BuildFilename(new List { episode }, new Series { Title = "Chicago P.D." }, _episodeFile) + .Should().Be("Chicago.P.D.S06E06.Part.1"); + } + + [Test] + public void should_replace_triple_period_with_single_period() + { + _namingConfig.StandardEpisodeFormat = "{Series.Title}.S{season:00}E{episode:00}.{Episode.Title}"; + + var episode = Builder.CreateNew() + .With(e => e.Title = "Part 1") + .With(e => e.SeasonNumber = 6) + .With(e => e.EpisodeNumber = 6) + .Build(); + + Subject.BuildFilename(new List { episode }, new Series { Title = "Chicago P.D.." }, _episodeFile) + .Should().Be("Chicago.P.D.S06E06.Part.1"); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs index c09bc0c6d..142ec8bb4 100644 --- a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs +++ b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs @@ -46,6 +46,8 @@ namespace NzbDrone.Core.Organizer public static readonly Regex SeriesTitleRegex = new Regex(@"(?\{(?:Series)(?\s|\.|-|_)Title\})", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private static readonly Regex FilenameCleanupRegex = new Regex(@"\.{2,}", RegexOptions.Compiled); + private static readonly char[] EpisodeTitleTrimCharaters = new[] { ' ', '.', '?' }; public FileNameBuilder(INamingConfigService namingConfigService, @@ -90,6 +92,7 @@ namespace NzbDrone.Core.Organizer var sortedEpisodes = episodes.OrderBy(e => e.EpisodeNumber).ToList(); var pattern = namingConfig.StandardEpisodeFormat; + var episodeTitles = new List { sortedEpisodes.First().Title.TrimEnd(EpisodeTitleTrimCharaters) @@ -153,8 +156,11 @@ namespace NzbDrone.Core.Organizer tokenValues.Add("{Episode Title}", GetEpisodeTitle(episodeTitles)); tokenValues.Add("{Quality Title}", GetQualityTitle(episodeFile.Quality)); - - return CleanFilename(ReplaceTokens(pattern, tokenValues).Trim()); + + var filename = ReplaceTokens(pattern, tokenValues).Trim(); + filename = FilenameCleanupRegex.Replace(filename, match => match.Captures[0].Value[0].ToString() ); + + return CleanFilename(filename); } public string BuildFilePath(Series series, int seasonNumber, string fileName, string extension)