jellyfin/Emby.Naming/Audio/AlbumParser.cs

71 lines
2.1 KiB
C#
Raw Normal View History

using System;
2018-09-12 13:26:21 -04:00
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
2019-01-13 14:17:29 -05:00
using Emby.Naming.Common;
using Jellyfin.Extensions;
2018-09-12 13:26:21 -04:00
namespace Emby.Naming.Audio
{
2020-11-10 11:11:48 -05:00
/// <summary>
/// Helper class to determine if Album is multipart.
/// </summary>
2023-05-22 16:48:09 -04:00
public partial class AlbumParser
2018-09-12 13:26:21 -04:00
{
private readonly NamingOptions _options;
2020-11-10 11:11:48 -05:00
/// <summary>
/// Initializes a new instance of the <see cref="AlbumParser"/> class.
/// </summary>
/// <param name="options">Naming options containing AlbumStackingPrefixes.</param>
2018-09-12 13:26:21 -04:00
public AlbumParser(NamingOptions options)
{
_options = options;
}
2023-05-23 09:44:47 -04:00
[GeneratedRegex(@"[-\.\(\)\s]+")]
2023-05-22 16:48:09 -04:00
private static partial Regex CleanRegex();
2020-11-10 11:11:48 -05:00
/// <summary>
/// Function that determines if album is multipart.
/// </summary>
/// <param name="path">Path to file.</param>
/// <returns>True if album is multipart.</returns>
2020-01-22 16:18:56 -05:00
public bool IsMultiPart(string path)
2018-09-12 13:26:21 -04:00
{
var filename = Path.GetFileName(path);
2020-04-19 12:27:07 -04:00
if (filename.Length == 0)
2018-09-12 13:26:21 -04:00
{
2020-01-22 16:18:56 -05:00
return false;
2018-09-12 13:26:21 -04:00
}
// 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
2023-05-22 16:48:09 -04:00
filename = CleanRegex().Replace(filename, " ");
2018-09-12 13:26:21 -04:00
2023-05-22 16:48:09 -04:00
ReadOnlySpan<char> trimmedFilename = filename.AsSpan().TrimStart();
2018-09-12 13:26:21 -04:00
foreach (var prefix in _options.AlbumStackingPrefixes)
{
2020-04-19 05:57:03 -04:00
if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
2018-09-12 13:26:21 -04:00
{
2019-05-10 14:37:42 -04:00
continue;
}
2020-04-19 05:57:03 -04:00
var tmp = trimmedFilename.Slice(prefix.Length).Trim();
2018-09-12 13:26:21 -04:00
if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _))
2019-05-10 14:37:42 -04:00
{
2020-01-22 16:18:56 -05:00
return true;
2018-09-12 13:26:21 -04:00
}
}
2020-01-22 16:18:56 -05:00
return false;
2018-09-12 13:26:21 -04:00
}
}
}