You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Emby.Naming/Audio/AlbumParser.cs

71 lines
2.1 KiB

using System;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Emby.Naming.Common;
using Jellyfin.Extensions;
namespace Emby.Naming.Audio
{
4 years ago
/// <summary>
/// Helper class to determine if Album is multipart.
/// </summary>
public partial class AlbumParser
{
private readonly NamingOptions _options;
4 years ago
/// <summary>
/// Initializes a new instance of the <see cref="AlbumParser"/> class.
/// </summary>
/// <param name="options">Naming options containing AlbumStackingPrefixes.</param>
public AlbumParser(NamingOptions options)
{
_options = options;
}
[GeneratedRegex(@"[-\.\(\)\s]+")]
private static partial Regex CleanRegex();
4 years ago
/// <summary>
/// Function that determines if album is multipart.
/// </summary>
/// <param name="path">Path to file.</param>
/// <returns>True if album is multipart.</returns>
public bool IsMultiPart(string path)
{
var filename = Path.GetFileName(path);
5 years ago
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<char> trimmedFilename = filename.AsSpan().TrimStart();
foreach (var prefix in _options.AlbumStackingPrefixes)
{
5 years ago
if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}
5 years ago
var tmp = trimmedFilename.Slice(prefix.Length).Trim();
if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _))
{
return true;
}
}
return false;
}
}
}