DefaultImageProvider.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Providers.FolderImages
  13. {
  14. public class DefaultImageProvider : IRemoteImageProvider, IHasItemChangeMonitor
  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 view = item as UserView;
  32. if (view != null)
  33. {
  34. return GetImages(view.ViewType, cancellationToken);
  35. }
  36. var folder = (ICollectionFolder)item;
  37. return GetImages(folder.CollectionType, cancellationToken);
  38. }
  39. private Task<IEnumerable<RemoteImageInfo>> GetImages(string viewType, CancellationToken cancellationToken)
  40. {
  41. var url = GetImageUrl(viewType);
  42. return Task.FromResult<IEnumerable<RemoteImageInfo>>(new List<RemoteImageInfo>
  43. {
  44. new RemoteImageInfo
  45. {
  46. ProviderName = Name,
  47. Url = url,
  48. Type = ImageType.Primary
  49. },
  50. new RemoteImageInfo
  51. {
  52. ProviderName = Name,
  53. Url = url,
  54. Type = ImageType.Thumb
  55. }
  56. });
  57. }
  58. private string GetImageUrl(string viewType)
  59. {
  60. const string urlPrefix = "https://raw.githubusercontent.com/MediaBrowser/MediaBrowser.Resources/master/images/folders/";
  61. if (string.Equals(viewType, CollectionType.Books, StringComparison.OrdinalIgnoreCase))
  62. {
  63. return urlPrefix + "books.png";
  64. }
  65. if (string.Equals(viewType, CollectionType.Games, StringComparison.OrdinalIgnoreCase))
  66. {
  67. return urlPrefix + "games.png";
  68. }
  69. if (string.Equals(viewType, CollectionType.Music, StringComparison.OrdinalIgnoreCase))
  70. {
  71. return urlPrefix + "music.png";
  72. }
  73. if (string.Equals(viewType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase))
  74. {
  75. return urlPrefix + "photos.png";
  76. }
  77. if (string.Equals(viewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase))
  78. {
  79. return urlPrefix + "tv.png";
  80. }
  81. if (string.Equals(viewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase))
  82. {
  83. return urlPrefix + "channels.png";
  84. }
  85. if (string.Equals(viewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase))
  86. {
  87. return urlPrefix + "livetv.png";
  88. }
  89. if (string.Equals(viewType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase))
  90. {
  91. return urlPrefix + "movies.png";
  92. }
  93. return urlPrefix + "generic.png";
  94. }
  95. public string Name
  96. {
  97. get { return "Default Image Provider"; }
  98. }
  99. public bool Supports(IHasImages item)
  100. {
  101. var view = item as UserView;
  102. if (view != null)
  103. {
  104. return !view.UserId.HasValue;
  105. }
  106. return item is ICollectionFolder;
  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. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  118. {
  119. return GetSupportedImages(item).Any(i => !item.HasImage(i));
  120. }
  121. }
  122. }