2019-01-13 14:54:44 -05:00
using System ;
2018-09-12 13:26:21 -04:00
using System.IO ;
using System.Linq ;
2019-01-13 14:17:29 -05:00
using Emby.Naming.Audio ;
using Emby.Naming.Common ;
2018-09-12 13:26:21 -04:00
namespace Emby.Naming.Video
{
2020-11-10 13:23:10 -05:00
/// <summary>
/// Resolve if file is extra for video.
/// </summary>
2018-09-12 13:26:21 -04:00
public class ExtraResolver
{
private readonly NamingOptions _options ;
2020-11-10 13:23:10 -05:00
/// <summary>
/// Initializes a new instance of the <see cref="ExtraResolver"/> class.
/// </summary>
/// <param name="options"><see cref="NamingOptions"/> object containing VideoExtraRules and passed to <see cref="AudioFileParser"/> and <see cref="VideoResolver"/>.</param>
2018-09-12 13:26:21 -04:00
public ExtraResolver ( NamingOptions options )
{
_options = options ;
}
2020-11-10 13:23:10 -05:00
/// <summary>
/// Attempts to resolve if file is extra.
/// </summary>
/// <param name="path">Path to file.</param>
/// <returns>Returns <see cref="ExtraResult"/> object.</returns>
2018-09-12 13:26:21 -04:00
public ExtraResult GetExtraInfo ( string path )
{
return _options . VideoExtraRules
. Select ( i = > GetExtraInfo ( path , i ) )
2019-12-06 14:40:06 -05:00
. FirstOrDefault ( i = > i . ExtraType ! = null ) ? ? new ExtraResult ( ) ;
2018-09-12 13:26:21 -04:00
}
private ExtraResult GetExtraInfo ( string path , ExtraRule rule )
{
var result = new ExtraResult ( ) ;
if ( rule . MediaType = = MediaType . Audio )
{
2020-02-19 15:56:35 -05:00
if ( ! AudioFileParser . IsAudioFile ( path , _options ) )
2018-09-12 13:26:21 -04:00
{
return result ;
}
}
else if ( rule . MediaType = = MediaType . Video )
{
if ( ! new VideoResolver ( _options ) . IsVideoFile ( path ) )
{
return result ;
}
}
else
{
2020-11-05 10:59:15 -05:00
// Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
throw new InvalidOperationException ( ) ;
2018-09-12 13:26:21 -04:00
}
if ( rule . RuleType = = ExtraRuleType . Filename )
{
var filename = Path . GetFileNameWithoutExtension ( path ) ;
if ( string . Equals ( filename , rule . Token , StringComparison . OrdinalIgnoreCase ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
}
else if ( rule . RuleType = = ExtraRuleType . Suffix )
{
var filename = Path . GetFileNameWithoutExtension ( path ) ;
if ( filename . IndexOf ( rule . Token , StringComparison . OrdinalIgnoreCase ) > 0 )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
}
else if ( rule . RuleType = = ExtraRuleType . Regex )
{
2020-11-05 10:59:15 -05:00
// Currently unreachable code if new rule.MediaType is desired add if clause with proper tests
throw new InvalidOperationException ( ) ;
/ *
2018-09-12 13:26:21 -04:00
var filename = Path . GetFileName ( path ) ;
var regex = new Regex ( rule . Token , RegexOptions . IgnoreCase ) ;
if ( regex . IsMatch ( filename ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
2020-11-05 10:59:15 -05:00
* /
2018-09-12 13:26:21 -04:00
}
2020-04-01 12:53:19 -04:00
else if ( rule . RuleType = = ExtraRuleType . DirectoryName )
{
var directoryName = Path . GetFileName ( Path . GetDirectoryName ( path ) ) ;
if ( string . Equals ( directoryName , rule . Token , StringComparison . OrdinalIgnoreCase ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
}
2018-09-12 13:26:21 -04:00
return result ;
}
}
}