2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2021-03-08 19:28:21 -05:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-09-12 13:26:21 -04:00
|
|
|
using System.IO;
|
2019-01-13 14:17:29 -05:00
|
|
|
using Emby.Naming.Common;
|
2021-05-16 08:49:11 -04:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves <see cref="VideoFileInfo"/> from file path.
|
|
|
|
/// </summary>
|
2018-09-12 13:26:21 -04:00
|
|
|
public class VideoResolver
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _options;
|
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="VideoResolver"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="options"><see cref="NamingOptions"/> object containing VideoFileExtensions, StubFileExtensions, CleanStringRegexes and CleanDateTimeRegexes
|
|
|
|
/// and passes options in <see cref="StubResolver"/>, <see cref="FlagParser"/>, <see cref="Format3DParser"/> and <see cref="ExtraResolver"/>.</param>
|
2018-09-12 13:26:21 -04:00
|
|
|
public VideoResolver(NamingOptions options)
|
|
|
|
{
|
|
|
|
_options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the directory.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2020-11-01 04:47:31 -05:00
|
|
|
public VideoFileInfo? ResolveDirectory(string? path)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
return Resolve(path, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2020-11-01 04:47:31 -05:00
|
|
|
public VideoFileInfo? ResolveFile(string? path)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
return Resolve(path, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Resolves the specified path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
2019-05-10 14:37:42 -04:00
|
|
|
/// <param name="isDirectory">if set to <c>true</c> [is folder].</param>
|
2020-01-22 16:18:56 -05:00
|
|
|
/// <param name="parseName">Whether or not the name should be parsed for info.</param>
|
2018-09-12 13:26:21 -04:00
|
|
|
/// <returns>VideoFileInfo.</returns>
|
2019-10-25 06:47:20 -04:00
|
|
|
/// <exception cref="ArgumentNullException"><c>path</c> is <c>null</c>.</exception>
|
2020-11-01 04:47:31 -05:00
|
|
|
public VideoFileInfo? Resolve(string? path, bool isDirectory, bool parseName = true)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2020-11-05 10:59:15 -05:00
|
|
|
return null;
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:40:08 -05:00
|
|
|
bool isStub = false;
|
2021-05-19 02:51:46 -04:00
|
|
|
ReadOnlySpan<char> container = ReadOnlySpan<char>.Empty;
|
2020-01-22 16:18:56 -05:00
|
|
|
string? stubType = null;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2019-05-10 14:37:42 -04:00
|
|
|
if (!isDirectory)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
var extension = Path.GetExtension(path.AsSpan());
|
2019-05-10 14:37:42 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
// Check supported extensions
|
2021-05-16 08:49:11 -04:00
|
|
|
if (!_options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
// It's not supported. Check stub extensions
|
2020-01-22 16:18:56 -05:00
|
|
|
if (!StubResolver.TryResolveFile(path, _options, out stubType))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:18:56 -05:00
|
|
|
isStub = true;
|
2018-09-12 13:26:21 -04: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);
|
|
|
|
|
2021-05-16 08:49:11 -04:00
|
|
|
var name = Path.GetFileNameWithoutExtension(path);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
int? year = null;
|
|
|
|
|
|
|
|
if (parseName)
|
|
|
|
{
|
|
|
|
var cleanDateTimeResult = CleanDateTime(name);
|
2020-04-21 06:11:55 -04:00
|
|
|
name = cleanDateTimeResult.Name;
|
|
|
|
year = cleanDateTimeResult.Year;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2020-01-11 15:16:36 -05:00
|
|
|
if (extraResult.ExtraType == null
|
2020-04-25 09:29:59 -04:00
|
|
|
&& TryCleanString(name, out ReadOnlySpan<char> newName))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2020-01-11 15:16:36 -05:00
|
|
|
name = newName.ToString();
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:59:15 -05:00
|
|
|
return new VideoFileInfo(
|
|
|
|
path: path,
|
2021-05-19 02:51:46 -04:00
|
|
|
container: container.IsEmpty ? null : container.ToString(),
|
2020-11-05 10:59:15 -05:00
|
|
|
isStub: isStub,
|
|
|
|
name: name,
|
|
|
|
year: year,
|
|
|
|
stubType: stubType,
|
|
|
|
is3D: format3DResult.Is3D,
|
|
|
|
format3D: format3DResult.Format3D,
|
|
|
|
extraType: extraResult.ExtraType,
|
|
|
|
isDirectory: isDirectory,
|
|
|
|
extraRule: extraResult.Rule);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Determines if path is video file based on extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to file.</param>
|
|
|
|
/// <returns>True if is video file.</returns>
|
2018-09-12 13:26:21 -04:00
|
|
|
public bool IsVideoFile(string path)
|
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
var extension = Path.GetExtension(path.AsSpan());
|
|
|
|
return _options.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Determines if path is video file stub based on extension.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to file.</param>
|
|
|
|
/// <returns>True if is video file stub.</returns>
|
2018-09-12 13:26:21 -04:00
|
|
|
public bool IsStubFile(string path)
|
|
|
|
{
|
2021-05-16 08:49:11 -04:00
|
|
|
var extension = Path.GetExtension(path.AsSpan());
|
|
|
|
return _options.StubFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Tries to clean name of clutter.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">Raw name.</param>
|
|
|
|
/// <param name="newName">Clean name.</param>
|
|
|
|
/// <returns>True if cleaning of name was successful.</returns>
|
2021-03-08 19:28:21 -05:00
|
|
|
public bool TryCleanString([NotNullWhen(true)] string? name, out ReadOnlySpan<char> newName)
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2020-01-11 15:16:36 -05:00
|
|
|
return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
2020-11-10 13:23:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Tries to get name and year from raw name.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">Raw name.</param>
|
|
|
|
/// <returns>Returns <see cref="CleanDateTimeResult"/> with name and optional year.</returns>
|
2018-09-12 13:26:21 -04:00
|
|
|
public CleanDateTimeResult CleanDateTime(string name)
|
|
|
|
{
|
2020-01-11 15:31:35 -05:00
|
|
|
return CleanDateTimeParser.Clean(name, _options.CleanDateTimeRegexes);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|