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