jellyfin/Emby.Naming/Video/VideoResolver.cs

158 lines
6.0 KiB
C#
Raw Normal View History

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