GenreImageProvider.cs 4.7 KB

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