DefaultLyricProvider.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Extensions;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Resolvers;
  7. namespace MediaBrowser.Providers.Lyric;
  8. /// <inheritdoc />
  9. public class DefaultLyricProvider : ILyricProvider
  10. {
  11. private static readonly string[] _lyricExtensions = { ".lrc", ".elrc", ".txt" };
  12. /// <inheritdoc />
  13. public string Name => "DefaultLyricProvider";
  14. /// <inheritdoc />
  15. public ResolverPriority Priority => ResolverPriority.First;
  16. /// <inheritdoc />
  17. public bool HasLyrics(BaseItem item)
  18. {
  19. var path = GetLyricsPath(item);
  20. return path is not null;
  21. }
  22. /// <inheritdoc />
  23. public async Task<LyricFile?> GetLyrics(BaseItem item)
  24. {
  25. var path = GetLyricsPath(item);
  26. if (path is not null)
  27. {
  28. var content = await File.ReadAllTextAsync(path).ConfigureAwait(false);
  29. if (!string.IsNullOrEmpty(content))
  30. {
  31. return new LyricFile(path, content);
  32. }
  33. }
  34. return null;
  35. }
  36. private string? GetLyricsPath(BaseItem item)
  37. {
  38. // Ensure the path to the item is not null
  39. string? itemDirectoryPath = Path.GetDirectoryName(item.Path);
  40. if (itemDirectoryPath is null)
  41. {
  42. return null;
  43. }
  44. // Ensure the directory path exists
  45. if (!Directory.Exists(itemDirectoryPath))
  46. {
  47. return null;
  48. }
  49. foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(item.Path)}.*"))
  50. {
  51. if (_lyricExtensions.Contains(Path.GetExtension(lyricFilePath.AsSpan()), StringComparison.OrdinalIgnoreCase))
  52. {
  53. return lyricFilePath;
  54. }
  55. }
  56. return null;
  57. }
  58. }