StudiosManualImageProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Providers.ImagesByName
  14. {
  15. public class StudiosManualImageProvider : IImageProvider
  16. {
  17. private readonly IServerConfigurationManager _config;
  18. private readonly IHttpClient _httpClient;
  19. private readonly IFileSystem _fileSystem;
  20. private readonly SemaphoreSlim _listResourcePool = new SemaphoreSlim(1, 1);
  21. public StudiosManualImageProvider(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 "Media Browser"; }
  34. }
  35. public bool Supports(IHasImages item)
  36. {
  37. return item is Studio;
  38. }
  39. public Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
  40. {
  41. return GetImages(item, imageType == ImageType.Primary, imageType == ImageType.Backdrop, cancellationToken);
  42. }
  43. public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
  44. {
  45. return GetImages(item, true, true, cancellationToken);
  46. }
  47. private async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, bool posters, bool backdrops, CancellationToken cancellationToken)
  48. {
  49. var list = new List<RemoteImageInfo>();
  50. if (posters)
  51. {
  52. var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
  53. await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
  54. list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
  55. }
  56. cancellationToken.ThrowIfCancellationRequested();
  57. if (backdrops)
  58. {
  59. var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
  60. await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
  61. list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
  62. }
  63. return list.Where(i => i != null);
  64. }
  65. private RemoteImageInfo GetImage(IHasImages item, string filename, ImageType type, string remoteFilename)
  66. {
  67. var list = ImageUtils.GetAvailableImages(filename);
  68. var match = ImageUtils.FindMatch(item, list);
  69. if (!string.IsNullOrEmpty(match))
  70. {
  71. var url = GetUrl(match, remoteFilename);
  72. return new RemoteImageInfo
  73. {
  74. ProviderName = Name,
  75. Type = type,
  76. Url = url
  77. };
  78. }
  79. return null;
  80. }
  81. private string GetUrl(string image, string filename)
  82. {
  83. return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
  84. }
  85. private Task EnsureThumbsList(string file, CancellationToken cancellationToken)
  86. {
  87. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
  88. return ImageUtils.EnsureList(url, file, _httpClient, _fileSystem, _listResourcePool, cancellationToken);
  89. }
  90. private Task EnsurePosterList(string file, CancellationToken cancellationToken)
  91. {
  92. const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
  93. return ImageUtils.EnsureList(url, file, _httpClient, _fileSystem, _listResourcePool, cancellationToken);
  94. }
  95. public int Priority
  96. {
  97. get { return 0; }
  98. }
  99. }
  100. }