MusicGenreImageProvider.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Providers;
  8. using MediaBrowser.Providers.Genres;
  9. using MediaBrowser.Providers.ImagesByName;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using CommonIO;
  16. namespace MediaBrowser.Providers.MusicGenres
  17. {
  18. public class MusicGenreImageProvider : IRemoteImageProvider
  19. {
  20. private readonly IServerConfigurationManager _config;
  21. private readonly IHttpClient _httpClient;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly SemaphoreSlim _listResourcePool = new SemaphoreSlim(1, 1);
  24. public MusicGenreImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
  25. {
  26. _config = config;
  27. _httpClient = httpClient;
  28. _fileSystem = fileSystem;
  29. }
  30. public string Name
  31. {
  32. get { return ProviderName; }
  33. }
  34. public static string ProviderName
  35. {
  36. get { return "Media Browser Designs"; }
  37. }
  38. public bool Supports(IHasImages item)
  39. {
  40. return item is MusicGenre;
  41. }
  42. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  43. {
  44. return new List<ImageType>
  45. {
  46. ImageType.Primary,
  47. ImageType.Thumb
  48. };
  49. }
  50. public Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
  51. {
  52. return GetImages(item, true, true, cancellationToken);
  53. }
  54. private async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, bool posters, bool thumbs, CancellationToken cancellationToken)
  55. {
  56. var list = new List<RemoteImageInfo>();
  57. if (posters)
  58. {
  59. var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotemusicgenreposters.txt");
  60. await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
  61. list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
  62. }
  63. cancellationToken.ThrowIfCancellationRequested();
  64. if (thumbs)
  65. {
  66. var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotemusicgenrethumbs.txt");
  67. await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
  68. list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
  69. }
  70. return list.Where(i => i != null);
  71. }
  72. private RemoteImageInfo GetImage(IHasImages item, string filename, ImageType type, string remoteFilename)
  73. {
  74. var list = ImageUtils.GetAvailableImages(filename);
  75. var match = ImageUtils.FindMatch(item, list);
  76. if (!string.IsNullOrEmpty(match))
  77. {
  78. var url = GetUrl(match, remoteFilename);
  79. return new RemoteImageInfo
  80. {
  81. ProviderName = Name,
  82. Type = type,
  83. Url = url
  84. };
  85. }
  86. return null;
  87. }
  88. private string GetUrl(string image, string filename)
  89. {
  90. return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/musicgenres/{0}/{1}.jpg", image, filename);
  91. }
  92. private Task EnsureThumbsList(string file, CancellationToken cancellationToken)
  93. {
  94. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/musicgenrethumbs.txt";
  95. return ImageUtils.EnsureList(url, file, _httpClient, _fileSystem, _listResourcePool, cancellationToken);
  96. }
  97. private Task EnsurePosterList(string file, CancellationToken cancellationToken)
  98. {
  99. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/musicgenreposters.txt";
  100. return ImageUtils.EnsureList(url, file, _httpClient, _fileSystem, _listResourcePool, cancellationToken);
  101. }
  102. public int Order
  103. {
  104. get { return 0; }
  105. }
  106. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  107. {
  108. return _httpClient.GetResponse(new HttpRequestOptions
  109. {
  110. CancellationToken = cancellationToken,
  111. Url = url,
  112. ResourcePool = GenreImageProvider.ImageDownloadResourcePool
  113. });
  114. }
  115. }
  116. }