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