diff --git a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs index 3089fb51e..db540b5c4 100644 --- a/src/NzbDrone.Core/Organizer/FileNameBuilder.cs +++ b/src/NzbDrone.Core/Organizer/FileNameBuilder.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text.RegularExpressions; using NLog; +using NzbDrone.Common.Cache; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Tv; @@ -21,6 +22,7 @@ public class FileNameBuilder : IBuildFileNames { private readonly IConfigService _configService; private readonly INamingConfigService _namingConfigService; + private readonly ICached _patternCache; private readonly Logger _logger; private static readonly Regex TitleRegex = new Regex(@"(?\{(?:\w+)(?\s|\W|_)\w+\})", @@ -37,10 +39,14 @@ public class FileNameBuilder : IBuildFileNames public static readonly Regex AirDateRegex = new Regex(@"\{Air(\s|\W|_)Date\}", RegexOptions.Compiled | RegexOptions.IgnoreCase); - public FileNameBuilder(INamingConfigService namingConfigService, IConfigService configService, Logger logger) + public FileNameBuilder(INamingConfigService namingConfigService, + IConfigService configService, + ICacheManger cacheManger, + Logger logger) { _namingConfigService = namingConfigService; _configService = configService; + _patternCache = cacheManger.GetCache(GetType()); _logger = logger; } @@ -99,17 +105,10 @@ public string BuildFilename(IList episodes, Series series, EpisodeFile } } - var seasonEpisode = SeasonEpisodePatternRegex.Match(pattern); - if (seasonEpisode.Success) - { - var episodeFormat = new EpisodeFormat - { - EpisodeSeparator = seasonEpisode.Groups["episodeSeparator"].Value, - Separator = seasonEpisode.Groups["separator"].Value, - EpisodePattern = seasonEpisode.Groups["episode"].Value, - SeasonEpisodePattern = seasonEpisode.Groups["seasonEpisode"].Value, - }; + var episodeFormat = GetSeasonEpisodePattern(pattern); + if (episodeFormat != null) + { pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, "{Season Episode}"); var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern; @@ -246,6 +245,28 @@ private string ReplaceNumberToken(string token, int value) return value.ToString(split[1]); } + + private EpisodeFormat GetSeasonEpisodePattern(string pattern) + { + return _patternCache.Get(pattern, () => + { + var match = SeasonEpisodePatternRegex.Match(pattern); + + if (match.Success) + { + return new EpisodeFormat + { + EpisodeSeparator = match.Groups["episodeSeparator"].Value, + Separator = match.Groups["separator"].Value, + EpisodePattern = match.Groups["episode"].Value, + SeasonEpisodePattern = match.Groups["seasonEpisode"].Value, + }; + + } + + return null; + }); + } } public enum MultiEpisodeStyle