using MediaBrowser.Common.Net; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Providers; using MediaBrowser.Providers.Genres; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Providers.FolderImages { public class DefaultImageProvider : IRemoteImageProvider, IHasItemChangeMonitor, IHasOrder { private readonly IHttpClient _httpClient; public DefaultImageProvider(IHttpClient httpClient) { _httpClient = httpClient; } public IEnumerable GetSupportedImages(IHasImages item) { return new List { ImageType.Primary, ImageType.Thumb }; } public Task> GetImages(IHasImages item, CancellationToken cancellationToken) { var view = item as UserView; if (view != null) { return GetImages(view.ViewType, view.ParentId != Guid.Empty, cancellationToken); } var folder = (ICollectionFolder)item; return GetImages(folder.CollectionType, false, cancellationToken); } private Task> GetImages(string viewType, bool isSubView, CancellationToken cancellationToken) { var url = GetImageUrl(viewType, isSubView); var list = new List(); if (!string.IsNullOrWhiteSpace(url)) { list.AddRange(new List { new RemoteImageInfo { ProviderName = Name, Url = url, Type = ImageType.Primary }, new RemoteImageInfo { ProviderName = Name, Url = url, Type = ImageType.Thumb } }); } return Task.FromResult>(list); } private string GetImageUrl(string viewType, bool isSubView) { const string urlPrefix = "https://raw.githubusercontent.com/MediaBrowser/MediaBrowser.Resources/master/images/folders/"; if (string.Equals(viewType, CollectionType.Books, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.Photos, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.Channels, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.LiveTv, StringComparison.OrdinalIgnoreCase)) { return null; } if (string.Equals(viewType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) { return null; } if (isSubView) { return null; } return urlPrefix + "generic.png"; } public string Name { get { return "Default Image Provider"; } } public bool Supports(IHasImages item) { return item is ICollectionFolder || item is UserView; } public Task GetImageResponse(string url, CancellationToken cancellationToken) { return _httpClient.GetResponse(new HttpRequestOptions { CancellationToken = cancellationToken, Url = url, ResourcePool = GenreImageProvider.ImageDownloadResourcePool }); } public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService) { return GetSupportedImages(item).Any(i => !item.HasImage(i)); } public int Order { get { // Run after the dynamic image provider return 1; } } } }