AlbumParser.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 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. /// <summary>
  24. /// Function that determines if album is multipart.
  25. /// </summary>
  26. /// <param name="path">Path to file.</param>
  27. /// <returns>True if album is multipart.</returns>
  28. public bool IsMultiPart(string path)
  29. {
  30. var filename = Path.GetFileName(path);
  31. if (filename.Length == 0)
  32. {
  33. return false;
  34. }
  35. // TODO: Move this logic into options object
  36. // Even better, remove the prefixes and come up with regexes
  37. // But Kodi documentation seems to be weak for audio
  38. // Normalize
  39. // Remove whitespace
  40. filename = filename.Replace('-', ' ');
  41. filename = filename.Replace('.', ' ');
  42. filename = filename.Replace('(', ' ');
  43. filename = filename.Replace(')', ' ');
  44. filename = Regex.Replace(filename, @"\s+", " ");
  45. ReadOnlySpan<char> trimmedFilename = filename.TrimStart();
  46. foreach (var prefix in _options.AlbumStackingPrefixes)
  47. {
  48. if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  49. {
  50. continue;
  51. }
  52. var tmp = trimmedFilename.Slice(prefix.Length).Trim();
  53. if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _))
  54. {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. }
  61. }