AlbumParser.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using Emby.Naming.Common;
  6. using Jellyfin.Extensions;
  7. namespace Emby.Naming.Audio
  8. {
  9. /// <summary>
  10. /// Helper class to determine if Album is multipart.
  11. /// </summary>
  12. public partial class AlbumParser
  13. {
  14. private readonly NamingOptions _options;
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="AlbumParser"/> class.
  17. /// </summary>
  18. /// <param name="options">Naming options containing AlbumStackingPrefixes.</param>
  19. public AlbumParser(NamingOptions options)
  20. {
  21. _options = options;
  22. }
  23. [GeneratedRegex(@"[-\.\(\)\s]+")]
  24. private static partial Regex CleanRegex();
  25. /// <summary>
  26. /// Function that determines if album is multipart.
  27. /// </summary>
  28. /// <param name="path">Path to file.</param>
  29. /// <returns>True if album is multipart.</returns>
  30. public bool IsMultiPart(string path)
  31. {
  32. var filename = Path.GetFileName(path);
  33. if (filename.Length == 0)
  34. {
  35. return false;
  36. }
  37. // TODO: Move this logic into options object
  38. // Even better, remove the prefixes and come up with regexes
  39. // But Kodi documentation seems to be weak for audio
  40. // Normalize
  41. // Remove whitespace
  42. filename = CleanRegex().Replace(filename, " ");
  43. ReadOnlySpan<char> trimmedFilename = filename.AsSpan().TrimStart();
  44. foreach (var prefix in _options.AlbumStackingPrefixes)
  45. {
  46. if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  47. {
  48. continue;
  49. }
  50. var tmp = trimmedFilename.Slice(prefix.Length).Trim();
  51. if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _))
  52. {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. }
  59. }