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