jellyfin/Emby.Naming/AudioBook/AudioBookResolver.cs

57 lines
1.9 KiB
C#
Raw Normal View History

using System;
2018-09-12 19:26:21 +02:00
using System.IO;
using Emby.Naming.Common;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
2018-09-12 19:26:21 +02:00
namespace Emby.Naming.AudioBook
{
2020-11-10 17:11:48 +01:00
/// <summary>
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
/// </summary>
2018-09-12 19:26:21 +02:00
public class AudioBookResolver
{
private readonly NamingOptions _options;
2020-11-10 17:11:48 +01: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 19:26:21 +02:00
public AudioBookResolver(NamingOptions options)
{
_options = options;
}
2020-11-10 17:11:48 +01: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 19:26:21 +02:00
{
if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
2018-09-12 19:26:21 +02:00
{
// Return null to indicate this path will not be used, instead of stopping whole process with exception
return null;
2018-09-12 19:26:21 +02:00
}
2019-01-25 21:52:10 +01:00
var extension = Path.GetExtension(path);
2019-05-10 20:37:42 +02:00
2018-09-12 19:26:21 +02:00
// Check supported extensions
2021-12-20 13:31:07 +01:00
if (!_options.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
2018-09-12 19:26:21 +02:00
{
return null;
}
var container = extension.TrimStart('.');
2019-05-10 20:37:42 +02:00
var parsingResult = new AudioBookFilePathParser(_options).Parse(path);
2019-01-08 00:27:46 +01:00
2020-11-01 10:47:31 +01:00
return new AudioBookFileInfo(
path,
container,
chapterNumber: parsingResult.ChapterNumber,
partNumber: parsingResult.PartNumber);
2018-09-12 19:26:21 +02:00
}
}
}