jellyfin/Emby.Naming/AudioBook/AudioBookResolver.cs

57 lines
1.9 KiB
C#
Raw Normal View History

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