33using NzbDrone . Core . Parser . Model ;
44using System . Text . RegularExpressions ;
55
6- public class ReleaseFormatter
6+ namespace Tubifarry . Core
77{
8- private readonly ReleaseInfo _releaseInfo ;
9- private readonly Artist _artist ;
10- private readonly NamingConfig ? _namingConfig ;
11-
12- public ReleaseFormatter ( ReleaseInfo releaseInfo , Artist artist , NamingConfig ? namingConfig )
8+ public class ReleaseFormatter
139 {
14- _releaseInfo = releaseInfo ;
15- _artist = artist ;
16- _namingConfig = namingConfig ;
17- }
10+ private readonly ReleaseInfo _releaseInfo ;
11+ private readonly Artist _artist ;
12+ private readonly NamingConfig ? _namingConfig ;
1813
19- public string BuildTrackFilename ( string ? pattern , Track track , Album album )
20- {
21- pattern ??= _namingConfig ? . StandardTrackFormat ?? "{track:0} {Track Title}" ;
22- Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( track , album ) ;
23- string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
24- return CleanFileName ( formattedString ) ;
25- }
14+ public ReleaseFormatter ( ReleaseInfo releaseInfo , Artist artist , NamingConfig ? namingConfig )
15+ {
16+ _releaseInfo = releaseInfo ;
17+ _artist = artist ;
18+ _namingConfig = namingConfig ;
19+ }
2620
27- public string BuildAlbumFilename ( string ? pattern , Album album )
28- {
29- pattern ??= "{Album Title}";
30- Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( null , album ) ;
31- string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
32- return CleanFileName ( formattedString ) ;
33- }
21+ public string BuildTrackFilename ( string ? pattern , Track track , Album album )
22+ {
23+ pattern ??= _namingConfig ? . StandardTrackFormat ?? "{track:0} {Track Title}";
24+ Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( track , album ) ;
25+ string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
26+ return CleanFileName ( formattedString ) ;
27+ }
3428
35- public string BuildArtistFolderName ( string ? pattern )
36- {
37- pattern ??= _namingConfig ? . ArtistFolderFormat ?? "{Artist Name }";
38- Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( null , null ) ;
39- string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
40- return CleanFileName ( formattedString ) ;
41- }
29+ public string BuildAlbumFilename ( string ? pattern , Album album )
30+ {
31+ pattern ??= "{Album Title }";
32+ Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( null , album ) ;
33+ string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
34+ return CleanFileName ( formattedString ) ;
35+ }
4236
43- private Dictionary < string , Func < string > > GetTokenHandlers ( Track ? track , Album ? album )
44- {
45- return new Dictionary < string , Func < string > > ( StringComparer . OrdinalIgnoreCase )
37+ public string BuildArtistFolderName ( string ? pattern )
38+ {
39+ pattern ??= _namingConfig ? . ArtistFolderFormat ?? "{Artist Name}" ;
40+ Dictionary < string , Func < string > > tokenHandlers = GetTokenHandlers ( null , null ) ;
41+ string formattedString = ReplaceTokens ( pattern , tokenHandlers ) ;
42+ return CleanFileName ( formattedString ) ;
43+ }
44+
45+ private Dictionary < string , Func < string > > GetTokenHandlers ( Track ? track , Album ? album )
46+ {
47+ return new Dictionary < string , Func < string > > ( StringComparer . OrdinalIgnoreCase )
4648 {
4749 // Album Tokens (only added if album is provided)
4850 { "{Album Title}" , ( ) => album ? . Title ?? string . Empty } ,
@@ -82,71 +84,72 @@ private Dictionary<string, Func<string>> GetTokenHandlers(Track? track, Album? a
8284 // Release Info Tokens
8385 { "{Original Title}" , ( ) => _releaseInfo ? . Title ?? string . Empty }
8486 } ;
85- }
86-
87- private static string ReplaceTokens ( string pattern , Dictionary < string , Func < string > > tokenHandlers ) => Regex . Replace ( pattern , @"\{([^}]+)\}" , match =>
88- {
89- string token = match . Groups [ 1 ] . Value ;
90- if ( tokenHandlers . TryGetValue ( $ "{{{token}}}", out Func < string > ? handler ) )
91- return handler ( ) ;
87+ }
9288
93- return string . Empty ;
94- } ) ;
89+ private static string ReplaceTokens ( string pattern , Dictionary < string , Func < string > > tokenHandlers ) => Regex . Replace ( pattern , @"\{([^}]+)\}" , match =>
90+ {
91+ string token = match . Groups [ 1 ] . Value ;
92+ if ( tokenHandlers . TryGetValue ( $ "{{{token}}}", out Func < string > ? handler ) )
93+ return handler ( ) ;
9594
96- private string CleanFileName ( string fileName )
97- {
98- char [ ] invalidFileNameChars = Path . GetInvalidFileNameChars ( ) ;
99- char [ ] invalidPathChars = Path . GetInvalidPathChars ( ) ;
100- char [ ] invalidChars = invalidFileNameChars . Union ( invalidPathChars ) . ToArray ( ) ;
101- fileName = invalidChars . Aggregate ( fileName , ( current , invalidChar ) => current . Replace ( invalidChar . ToString ( ) , string . Empty ) ) ;
95+ return string . Empty ;
96+ } ) ;
10297
103- switch ( _namingConfig ? . ColonReplacementFormat )
98+ private string CleanFileName ( string fileName )
10499 {
105- case ColonReplacementFormat . Delete :
106- fileName = fileName . Replace ( ":" , string . Empty ) ;
107- break ;
108- case ColonReplacementFormat . Dash :
109- fileName = fileName . Replace ( ":" , "-" ) ;
110- break ;
111- case ColonReplacementFormat . SpaceDash :
112- fileName = fileName . Replace ( ":" , " -" ) ;
113- break ;
114- case ColonReplacementFormat . SpaceDashSpace :
115- fileName = fileName . Replace ( ":" , " - " ) ;
116- break ;
117- case ColonReplacementFormat . Smart :
118- fileName = Regex . Replace ( fileName , @":\s*" , " - " ) ;
119- break ;
100+ char [ ] invalidFileNameChars = Path . GetInvalidFileNameChars ( ) ;
101+ char [ ] invalidPathChars = Path . GetInvalidPathChars ( ) ;
102+ char [ ] invalidChars = invalidFileNameChars . Union ( invalidPathChars ) . ToArray ( ) ;
103+ fileName = invalidChars . Aggregate ( fileName , ( current , invalidChar ) => current . Replace ( invalidChar . ToString ( ) , string . Empty ) ) ;
104+
105+ switch ( _namingConfig ? . ColonReplacementFormat )
106+ {
107+ case ColonReplacementFormat . Delete :
108+ fileName = fileName . Replace ( ":" , string . Empty ) ;
109+ break ;
110+ case ColonReplacementFormat . Dash :
111+ fileName = fileName . Replace ( ":" , "-" ) ;
112+ break ;
113+ case ColonReplacementFormat . SpaceDash :
114+ fileName = fileName . Replace ( ":" , " -" ) ;
115+ break ;
116+ case ColonReplacementFormat . SpaceDashSpace :
117+ fileName = fileName . Replace ( ":" , " - " ) ;
118+ break ;
119+ case ColonReplacementFormat . Smart :
120+ fileName = Regex . Replace ( fileName , @":\s*" , " - " ) ;
121+ break ;
122+ }
123+
124+ return fileName . Trim ( ) ;
120125 }
121126
122- return fileName . Trim ( ) ;
123- }
124-
125- private static string CleanTitle ( string ? title )
126- {
127- if ( string . IsNullOrEmpty ( title ) ) return string . Empty ;
128- return title . Replace ( "&" , "and" ) . Replace ( "/" , " " ) . Replace ( "\\ " , " " ) . Trim ( ) ;
129- }
127+ private static string CleanTitle ( string ? title )
128+ {
129+ if ( string . IsNullOrEmpty ( title ) ) return string . Empty ;
130+ return title . Replace ( "&" , "and" ) . Replace ( "/" , " " ) . Replace ( "\\ " , " " ) . Trim ( ) ;
131+ }
130132
131- private static string TitleThe ( string ? title )
132- {
133- if ( string . IsNullOrEmpty ( title ) ) return string . Empty ;
134- return Regex . Replace ( title , @"^(The|A|An)\s+(.+)$" , "$2, $1" , RegexOptions . IgnoreCase ) ;
135- }
133+ private static string TitleThe ( string ? title )
134+ {
135+ if ( string . IsNullOrEmpty ( title ) ) return string . Empty ;
136+ return Regex . Replace ( title , @"^(The|A|An)\s+(.+)$" , "$2, $1" , RegexOptions . IgnoreCase ) ;
137+ }
136138
137- private static string CleanTitleThe ( string ? title ) => CleanTitle ( TitleThe ( title ) ) ;
139+ private static string CleanTitleThe ( string ? title ) => CleanTitle ( TitleThe ( title ) ) ;
138140
139- private static string TitleFirstCharacter ( string ? title )
140- {
141- if ( string . IsNullOrEmpty ( title ) ) return "_" ;
142- return char . IsLetterOrDigit ( title [ 0 ] ) ? title [ ..1 ] . ToUpper ( ) : "_" ;
143- }
141+ private static string TitleFirstCharacter ( string ? title )
142+ {
143+ if ( string . IsNullOrEmpty ( title ) ) return "_" ;
144+ return char . IsLetterOrDigit ( title [ 0 ] ) ? title [ ..1 ] . ToUpper ( ) : "_" ;
145+ }
144146
145- private static string FormatTrackNumber ( string ? trackNumber , string ? format )
146- {
147- if ( string . IsNullOrEmpty ( trackNumber ) ) return string . Empty ;
148- if ( int . TryParse ( trackNumber , out int trackNumberInt ) )
149- return trackNumberInt . ToString ( format ) ;
150- return trackNumber ;
147+ private static string FormatTrackNumber ( string ? trackNumber , string ? format )
148+ {
149+ if ( string . IsNullOrEmpty ( trackNumber ) ) return string . Empty ;
150+ if ( int . TryParse ( trackNumber , out int trackNumberInt ) )
151+ return trackNumberInt . ToString ( format ) ;
152+ return trackNumber ;
153+ }
151154 }
152155}
0 commit comments