AlbumParser.cs 2.3 KB

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