DefaultImageProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Providers;
  6. using MediaBrowser.Providers.Genres;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Providers.FolderImages
  12. {
  13. public class DefaultImageProvider : IRemoteImageProvider
  14. {
  15. private readonly IHttpClient _httpClient;
  16. public DefaultImageProvider(IHttpClient httpClient)
  17. {
  18. _httpClient = httpClient;
  19. }
  20. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  21. {
  22. return new List<ImageType>
  23. {
  24. ImageType.Primary,
  25. ImageType.Thumb
  26. };
  27. }
  28. public Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
  29. {
  30. var view = item as UserView;
  31. if (view != null)
  32. {
  33. return GetImages(view.ViewType, cancellationToken);
  34. }
  35. var folder = (ICollectionFolder)item;
  36. return GetImages(folder.CollectionType, cancellationToken);
  37. }
  38. private Task<IEnumerable<RemoteImageInfo>> GetImages(string viewType, CancellationToken cancellationToken)
  39. {
  40. var url = GetImageUrl(viewType);
  41. return Task.FromResult<IEnumerable<RemoteImageInfo>>(new List<RemoteImageInfo>
  42. {
  43. new RemoteImageInfo
  44. {
  45. ProviderName = Name,
  46. Url = url,
  47. Type = ImageType.Primary
  48. },
  49. new RemoteImageInfo
  50. {
  51. ProviderName = Name,
  52. Url = url,
  53. Type = ImageType.Thumb
  54. }
  55. });
  56. }
  57. private string GetImageUrl(string viewType)
  58. {
  59. const string urlPrefix = "https://raw.githubusercontent.com/MediaBrowser/MediaBrowser.Resources/master/images/folders/";
  60. if (string.Equals(viewType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  61. {
  62. return urlPrefix + "books.jpg";
  63. }
  64. if (string.Equals(viewType, CollectionType.Games, StringComparison.OrdinalIgnoreCase))
  65. {
  66. return urlPrefix + "games.jpg";
  67. }
  68. if (string.Equals(viewType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
  69. {
  70. return urlPrefix + "music.jpg";
  71. }
  72. if (string.Equals(viewType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  73. {
  74. return urlPrefix + "photos.jpg";
  75. }
  76. if (string.Equals(viewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  77. {
  78. return urlPrefix + "tv.jpg";
  79. }
  80. if (string.Equals(viewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
  81. {
  82. return urlPrefix + "channels.jpg";
  83. }
  84. if (string.Equals(viewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  85. {
  86. return urlPrefix + "livetv.jpg";
  87. }
  88. if (string.Equals(viewType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  89. {
  90. return urlPrefix + "movies.jpg";
  91. }
  92. return urlPrefix + "generic.jpg";
  93. }
  94. public string Name
  95. {
  96. get { return "Default Image Provider"; }
  97. }
  98. public bool Supports(IHasImages item)
  99. {
  100. return item is UserView || item is ICollectionFolder;
  101. }
  102. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  103. {
  104. return _httpClient.GetResponse(new HttpRequestOptions
  105. {
  106. CancellationToken = cancellationToken,
  107. Url = url,
  108. ResourcePool = GenreImageProvider.ImageDownloadResourcePool
  109. });
  110. }
  111. }
  112. }