GameGenreImageProvider.cs 4.8 KB

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