2019-01-13 14:54:44 -05:00
using System ;
2018-09-12 13:26:21 -04:00
using System.IO ;
2020-11-12 07:16:33 -05:00
using System.Text.RegularExpressions ;
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
{
2021-10-05 15:47:59 -04:00
private static readonly char [ ] _digits = new [ ] { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' } ;
2018-09-12 13:26:21 -04:00
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 )
{
var result = new ExtraResult ( ) ;
2021-05-16 08:49:11 -04:00
for ( var i = 0 ; i < _options . VideoExtraRules . Length ; i + + )
2018-09-12 13:26:21 -04:00
{
2021-05-16 08:49:11 -04:00
var rule = _options . VideoExtraRules [ i ] ;
if ( rule . MediaType = = MediaType . Audio )
2018-09-12 13:26:21 -04:00
{
2021-05-16 08:49:11 -04:00
if ( ! AudioFileParser . IsAudioFile ( path , _options ) )
{
continue ;
}
2018-09-12 13:26:21 -04:00
}
2021-05-16 08:49:11 -04:00
else if ( rule . MediaType = = MediaType . Video )
2018-09-12 13:26:21 -04:00
{
2021-05-23 18:30:41 -04:00
if ( ! VideoResolver . IsVideoFile ( path , _options ) )
2021-05-16 08:49:11 -04:00
{
continue ;
}
2018-09-12 13:26:21 -04:00
}
2021-05-16 08:49:11 -04:00
var pathSpan = path . AsSpan ( ) ;
if ( rule . RuleType = = ExtraRuleType . Filename )
2018-09-12 13:26:21 -04:00
{
2021-05-16 08:49:11 -04:00
var filename = Path . GetFileNameWithoutExtension ( pathSpan ) ;
2018-09-12 13:26:21 -04:00
2021-05-16 08:49:11 -04:00
if ( filename . Equals ( rule . Token , StringComparison . OrdinalIgnoreCase ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
}
else if ( rule . RuleType = = ExtraRuleType . Suffix )
2018-09-12 13:26:21 -04:00
{
2021-10-05 15:47:59 -04:00
// Trim the digits from the end of the filename so we can recognize things like -trailer2
var filename = Path . GetFileNameWithoutExtension ( pathSpan ) . TrimEnd ( _digits ) ;
2021-05-16 08:49:11 -04:00
2021-10-05 15:47:59 -04:00
if ( filename . EndsWith ( rule . Token , StringComparison . OrdinalIgnoreCase ) )
2021-05-16 08:49:11 -04:00
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
2018-09-12 13:26:21 -04:00
}
2021-05-16 08:49:11 -04:00
else if ( rule . RuleType = = ExtraRuleType . Regex )
{
var filename = Path . GetFileName ( path ) ;
2018-09-12 13:26:21 -04:00
2021-05-16 08:49:11 -04:00
var regex = new Regex ( rule . Token , RegexOptions . IgnoreCase ) ;
2018-09-12 13:26:21 -04:00
2021-05-16 08:49:11 -04:00
if ( regex . IsMatch ( filename ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
}
else if ( rule . RuleType = = ExtraRuleType . DirectoryName )
2018-09-12 13:26:21 -04:00
{
2021-05-16 08:49:11 -04:00
var directoryName = Path . GetFileName ( Path . GetDirectoryName ( pathSpan ) ) ;
if ( directoryName . Equals ( rule . Token , StringComparison . OrdinalIgnoreCase ) )
{
result . ExtraType = rule . ExtraType ;
result . Rule = rule ;
}
2018-09-12 13:26:21 -04:00
}
2021-05-16 08:49:11 -04:00
if ( result . ExtraType ! = null )
2020-04-01 12:53:19 -04:00
{
2021-05-16 08:49:11 -04:00
return result ;
2020-04-01 12:53:19 -04:00
}
}
2018-09-12 13:26:21 -04:00
return result ;
}
}
}