LyricInfo.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.IO;
  3. using Jellyfin.Extensions;
  4. namespace MediaBrowser.Controller.Lyrics;
  5. /// <summary>
  6. /// Lyric helper methods.
  7. /// </summary>
  8. public static class LyricInfo
  9. {
  10. /// <summary>
  11. /// Gets matching lyric file for a requested item.
  12. /// </summary>
  13. /// <param name="lyricProvider">The lyricProvider interface to use.</param>
  14. /// <param name="itemPath">Path of requested item.</param>
  15. /// <returns>Lyric file path if passed lyric provider's supported media type is found; otherwise, null.</returns>
  16. public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
  17. {
  18. // Ensure we have a provider
  19. if (lyricProvider is null)
  20. {
  21. return null;
  22. }
  23. // Ensure the path to the item is not null
  24. string? itemDirectoryPath = Path.GetDirectoryName(itemPath);
  25. if (itemDirectoryPath is null)
  26. {
  27. return null;
  28. }
  29. // Ensure the directory path exists
  30. if (!Directory.Exists(itemDirectoryPath))
  31. {
  32. return null;
  33. }
  34. foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
  35. {
  36. if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath.AsSpan())[1..], StringComparison.OrdinalIgnoreCase))
  37. {
  38. return lyricFilePath;
  39. }
  40. }
  41. return null;
  42. }
  43. }