jellyfin-server/Emby.Naming/Video/VideoResolver.cs

158 lines
6.0 KiB
C#
Raw Normal View History

using System;
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>
2021-05-23 22:30:41 +00:00
public static class VideoResolver
2018-09-12 17:26:21 +00:00
{
/// <summary>
/// Resolves the directory.
/// </summary>
/// <param name="path">The path.</param>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2018-09-12 17:26:21 +00:00
/// <returns>VideoFileInfo.</returns>
2021-05-23 22:30:41 +00:00
public static VideoFileInfo? ResolveDirectory(string? path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
return Resolve(path, true, namingOptions);
2018-09-12 17:26:21 +00:00
}
/// <summary>
/// Resolves the file.
/// </summary>
/// <param name="path">The path.</param>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2018-09-12 17:26:21 +00:00
/// <returns>VideoFileInfo.</returns>
2021-05-23 22:30:41 +00:00
public static VideoFileInfo? ResolveFile(string? path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
return Resolve(path, false, namingOptions);
2018-09-12 17:26:21 +00:00
}
/// <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>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</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>
2021-05-23 22:30:41 +00:00
public static VideoFileInfo? Resolve(string? path, bool isDirectory, NamingOptions namingOptions, 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
}
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-23 22:30:41 +00:00
if (!namingOptions.VideoFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
2018-09-12 17:26:21 +00:00
{
// It's not supported. Check stub extensions
2021-05-23 22:30:41 +00:00
if (!StubResolver.TryResolveFile(path, namingOptions, 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('.');
}
2021-05-23 22:30:41 +00:00
var format3DResult = Format3DParser.Parse(path, namingOptions);
2018-09-12 17:26:21 +00:00
2021-05-23 22:30:41 +00:00
var extraResult = new ExtraResolver(namingOptions).GetExtraInfo(path);
2018-09-12 17:26:21 +00:00
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)
{
2021-05-23 22:30:41 +00:00
var cleanDateTimeResult = CleanDateTime(name, namingOptions);
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
2021-05-23 22:30:41 +00:00
&& TryCleanString(name, namingOptions, 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>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <returns>True if is video file.</returns>
2021-05-23 22:30:41 +00:00
public static bool IsVideoFile(string path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
var extension = Path.GetExtension(path.AsSpan());
2021-05-23 22:30:41 +00:00
return namingOptions.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>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <returns>True if is video file stub.</returns>
2021-05-23 22:30:41 +00:00
public static bool IsStubFile(string path, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-16 12:49:11 +00:00
var extension = Path.GetExtension(path.AsSpan());
2021-05-23 22:30:41 +00:00
return namingOptions.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>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <param name="newName">Clean name.</param>
/// <returns>True if cleaning of name was successful.</returns>
2021-05-23 22:30:41 +00:00
public static bool TryCleanString([NotNullWhen(true)] string? name, NamingOptions namingOptions, out ReadOnlySpan<char> newName)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
return CleanStringParser.TryClean(name, namingOptions.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>
2021-05-23 22:30:41 +00:00
/// <param name="namingOptions">The naming options.</param>
2020-11-10 18:23:10 +00:00
/// <returns>Returns <see cref="CleanDateTimeResult"/> with name and optional year.</returns>
2021-05-23 22:30:41 +00:00
public static CleanDateTimeResult CleanDateTime(string name, NamingOptions namingOptions)
2018-09-12 17:26:21 +00:00
{
2021-05-23 22:30:41 +00:00
return CleanDateTimeParser.Clean(name, namingOptions.CleanDateTimeRegexes);
2018-09-12 17:26:21 +00:00
}
}
}