StudiosImageProvider.cs 4.6 KB

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