2019-12-13 20:11:37 +01:00
|
|
|
#pragma warning disable CS1591
|
2020-01-22 22:18:56 +01:00
|
|
|
#nullable enable
|
2019-12-13 20:11:37 +01:00
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2018-09-12 19:26:21 +02:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-01-13 20:17:29 +01:00
|
|
|
using Emby.Naming.Common;
|
2018-09-12 19:26:21 +02:00
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
|
|
|
public class VideoResolver
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
|
|
|
public VideoResolver(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the directory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2020-01-22 22:18:56 +01:00
|
|
|
public VideoFileInfo? ResolveDirectory(string path)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
|
|
|
return Resolve(path, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2020-01-22 22:18:56 +01:00
|
|
|
public VideoFileInfo? ResolveFile(string path)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
|
|
|
return Resolve(path, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the specified path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
2019-05-10 20:37:42 +02:00
|
|
|
/// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
|
2020-01-22 22:18:56 +01:00
|
|
|
/// <param name="parseName">Whether or not the name should be parsed for info.</param>
|
2018-09-12 19:26:21 +02:00
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2019-10-25 12:47:20 +02:00
|
|
|
/// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
|
2020-01-22 22:18:56 +01:00
|
|
|
public VideoFileInfo? Resolve(string path, bool isDirectory, bool parseName = true)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 20:40:08 +01:00
|
|
|
bool isStub = false;
|
2020-01-22 22:18:56 +01:00
|
|
|
string? container = null;
|
|
|
|
string? stubType = null;
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2019-05-10 20:37:42 +02:00
|
|
|
if (!isDirectory)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2019-01-18 20:40:08 +01:00
|
|
|
var extension = Path.GetExtension(path);
|
2019-05-10 20:37:42 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
// Check supported extensions
|
|
|
|
if (!_options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
// It's not supported. Check stub extensions
|
2020-01-22 22:18:56 +01:00
|
|
|
if (!StubResolver.TryResolveFile(path, _options, out stubType))
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-22 22:18:56 +01:00
|
|
|
isStub = true;
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
container = extension.TrimStart('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
var flags = new FlagParser(_options).GetFlags(path);
|
|
|
|
var format3DResult = new Format3DParser(_options).Parse(flags);
|
|
|
|
|
|
|
|
var extraResult = new ExtraResolver(_options).GetExtraInfo(path);
|
|
|
|
|
2019-05-10 20:37:42 +02:00
|
|
|
var name = isDirectory
|
2019-01-18 20:40:08 +01:00
|
|
|
? Path.GetFileName(path)
|
|
|
|
: Path.GetFileNameWithoutExtension(path);
|
2018-09-12 19:26:21 +02:00
|
|
|
|
|
|
|
int? year = null;
|
|
|
|
|
|
|
|
if (parseName)
|
|
|
|
{
|
|
|
|
var cleanDateTimeResult = CleanDateTime(name);
|
2020-04-21 12:11:55 +02:00
|
|
|
name = cleanDateTimeResult.Name;
|
|
|
|
year = cleanDateTimeResult.Year;
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2020-01-11 21:16:36 +01:00
|
|
|
if (extraResult.ExtraType == null
|
|
|
|
&& TryCleanString(cleanDateTimeResult.Name, out ReadOnlySpan<char> newName))
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2020-01-11 21:16:36 +01:00
|
|
|
name = newName.ToString();
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new VideoFileInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Container = container,
|
|
|
|
IsStub = isStub,
|
|
|
|
Name = name,
|
|
|
|
Year = year,
|
|
|
|
StubType = stubType,
|
|
|
|
Is3D = format3DResult.Is3D,
|
|
|
|
Format3D = format3DResult.Format3D,
|
|
|
|
ExtraType = extraResult.ExtraType,
|
2019-05-10 20:37:42 +02:00
|
|
|
IsDirectory = isDirectory,
|
2018-09-12 19:26:21 +02:00
|
|
|
ExtraRule = extraResult.Rule
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsVideoFile(string path)
|
|
|
|
{
|
|
|
|
var extension = Path.GetExtension(path) ?? string.Empty;
|
|
|
|
return _options.VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsStubFile(string path)
|
|
|
|
{
|
|
|
|
var extension = Path.GetExtension(path) ?? string.Empty;
|
|
|
|
return _options.StubFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
2020-01-11 21:16:36 +01:00
|
|
|
public bool TryCleanString(string name, out ReadOnlySpan<char> newName)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2020-01-11 21:16:36 +01:00
|
|
|
return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public CleanDateTimeResult CleanDateTime(string name)
|
|
|
|
{
|
2020-01-11 21:31:35 +01:00
|
|
|
return CleanDateTimeParser.Clean(name, _options.CleanDateTimeRegexes);
|
2018-09-12 19:26:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|