StudiosImageProvider.cs 4.8 KB

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