using System;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Jellyfin.Extensions;
namespace Emby.Naming.Audio
{
///
/// Helper class to determine if Album is multipart.
///
public partial class AlbumParser
{
private readonly NamingOptions _options;
///
/// Initializes a new instance of the class.
///
/// Naming options containing AlbumStackingPrefixes.
public AlbumParser(NamingOptions options)
{
_options = options;
}
[GeneratedRegex(@"[-\.\(\)\s]+")]
private static partial Regex CleanRegex();
///
/// Function that determines if album is multipart.
///
/// Path to file.
/// True if album is multipart.
public bool IsMultiPart(string path)
{
var filename = Path.GetFileName(path);
if (filename.Length == 0)
{
return false;
}
// TODO: Move this logic into options object
// Even better, remove the prefixes and come up with regexes
// But Kodi documentation seems to be weak for audio
// Normalize
// Remove whitespace
filename = CleanRegex().Replace(filename, " ");
ReadOnlySpan trimmedFilename = filename.AsSpan().TrimStart();
foreach (var prefix in _options.AlbumStackingPrefixes)
{
if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}
var tmp = trimmedFilename.Slice(prefix.Length).Trim();
if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _))
{
return true;
}
}
return false;
}
}
}