DefaultImageProvider.cs 4.4 KB

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