ItemHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Dynamic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using Jellyfin.Api.Models.UserDtos;
  8. using LrcParser.Model;
  9. using LrcParser.Parser;
  10. using MediaBrowser.Controller.Entities;
  11. namespace Jellyfin.Api.Helpers
  12. {
  13. /// <summary>
  14. /// Item helper.
  15. /// </summary>
  16. public static class ItemHelper
  17. {
  18. /// <summary>
  19. /// Opens lyrics file, converts to a List of Lyrics, and returns it.
  20. /// </summary>
  21. /// <param name="item">Requested Item.</param>
  22. /// <returns>Collection of Lyrics.</returns>
  23. internal static object? GetLyricData(BaseItem item)
  24. {
  25. // Find all classes that implement ILyricsProvider Interface
  26. var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
  27. .GetTypes()
  28. .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
  29. if (!foundLyricProviders.Any())
  30. {
  31. return null;
  32. }
  33. foreach (var provider in foundLyricProviders)
  34. {
  35. ILyricsProvider? newProvider = Activator.CreateInstance(provider) as ILyricsProvider;
  36. if (newProvider is not null)
  37. {
  38. newProvider.Process(item);
  39. if (newProvider.HasData)
  40. {
  41. return newProvider.Data;
  42. }
  43. }
  44. }
  45. return null;
  46. }
  47. /// <summary>
  48. /// Checks if requested item has a matching lyric file.
  49. /// </summary>
  50. /// <param name="itemPath">Path of requested item.</param>
  51. /// <returns>True if item has a matching lyrics file.</returns>
  52. public static string? GetLyricFilePath(string itemPath)
  53. {
  54. // Find all classes that implement ILyricsProvider Interface
  55. var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly()
  56. .GetTypes()
  57. .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface);
  58. if (!foundLyricProviders.Any())
  59. {
  60. return null;
  61. }
  62. // Iterate over all found lyric providers
  63. foreach (var provider in foundLyricProviders)
  64. {
  65. ILyricsProvider? foundProvider = Activator.CreateInstance(provider) as ILyricsProvider;
  66. if (foundProvider?.FileExtensions is null)
  67. {
  68. continue;
  69. }
  70. if (foundProvider.FileExtensions.Any())
  71. {
  72. foreach (string lyricFileExtension in foundProvider.FileExtensions)
  73. {
  74. string lyricFilePath = @Path.ChangeExtension(itemPath, lyricFileExtension);
  75. if (System.IO.File.Exists(lyricFilePath))
  76. {
  77. return lyricFilePath;
  78. }
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. /// <summary>
  85. /// Checks if requested item has a matching local lyric file.
  86. /// </summary>
  87. /// <param name="itemPath">Path of requested item.</param>
  88. /// <returns>True if item has a matching lyrics file; otherwise false.</returns>
  89. public static bool HasLyricFile(string itemPath)
  90. {
  91. string? lyricFilePath = GetLyricFilePath(itemPath);
  92. return !string.IsNullOrEmpty(lyricFilePath);
  93. }
  94. }
  95. }