using System;
using System.IO;
using System.Linq;
using Emby.Naming.Common;
namespace Emby.Naming.AudioBook
{
///
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
///
public class AudioBookResolver
{
private readonly NamingOptions _options;
///
/// Initializes a new instance of the class.
///
/// containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.
public AudioBookResolver(NamingOptions options)
{
_options = options;
}
///
/// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
///
/// Path to audiobook file.
/// Returns object.
public AudioBookFileInfo? Resolve(string path)
{
if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
{
// Return null to indicate this path will not be used, instead of stopping whole process with exception
return null;
}
var extension = Path.GetExtension(path);
// Check supported extensions
if (!_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
return null;
}
var container = extension.TrimStart('.');
var parsingResult = new AudioBookFilePathParser(_options).Parse(path);
return new AudioBookFileInfo(
path,
container,
chapterNumber: parsingResult.ChapterNumber,
partNumber: parsingResult.PartNumber);
}
}
}