jellyfin-server/Emby.Naming/AudioBook/AudioBookResolver.cs

57 lines
1.9 KiB
C#
Raw Normal View History

using System;
2018-09-12 17:26:21 +00:00
using System.IO;
using Emby.Naming.Common;
2021-12-20 12:31:07 +00:00
using Jellyfin.Extensions;
2018-09-12 17:26:21 +00:00
namespace Emby.Naming.AudioBook
{
2020-11-10 16:11:48 +00:00
/// <summary>
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
/// </summary>
2018-09-12 17:26:21 +00:00
public class AudioBookResolver
{
private readonly NamingOptions _options;
2020-11-10 16:11:48 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="AudioBookResolver"/> class.
/// </summary>
/// <param name="options"><see cref="NamingOptions"/> containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.</param>
2018-09-12 17:26:21 +00:00
public AudioBookResolver(NamingOptions options)
{
_options = options;
}
2020-11-10 16:11:48 +00:00
/// <summary>
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
/// </summary>
/// <param name="path">Path to audiobook file.</param>
/// <returns>Returns <see cref="AudioBookResolver"/> object.</returns>
public AudioBookFileInfo? Resolve(string path)
2018-09-12 17:26:21 +00:00
{
if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
2018-09-12 17:26:21 +00:00
{
// Return null to indicate this path will not be used, instead of stopping whole process with exception
return null;
2018-09-12 17:26:21 +00:00
}
2019-01-25 20:52:10 +00:00
var extension = Path.GetExtension(path);
2019-05-10 18:37:42 +00:00
2018-09-12 17:26:21 +00:00
// Check supported extensions
2021-12-20 12:31:07 +00:00
if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
2018-09-12 17:26:21 +00:00
{
return null;
}
var container = extension.TrimStart('.');
2019-05-10 18:37:42 +00:00
var parsingResult = new AudioBookFilePathParser(_options).Parse(path);
2019-01-07 23:27:46 +00:00
2020-11-01 09:47:31 +00:00
return new AudioBookFileInfo(
path,
container,
chapterNumber: parsingResult.ChapterNumber,
partNumber: parsingResult.PartNumber);
2018-09-12 17:26:21 +00:00
}
}
}