#pragma warning disable CS1591
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Json;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
namespace MediaBrowser.Providers.Plugins.AudioDb
{
public class AudioDbAlbumImageProvider : IRemoteImageProvider, IHasOrder
{
private readonly IServerConfigurationManager _config;
private readonly IHttpClientFactory _httpClientFactory;
private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
public AudioDbAlbumImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory)
{
_config = config;
_httpClientFactory = httpClientFactory;
}
///
public string Name => "TheAudioDB";
///
// After embedded and fanart
public int Order => 2;
///
public IEnumerable GetSupportedImages(BaseItem item)
{
return new List
{
ImageType.Primary,
ImageType.Disc
};
}
///
public async Task> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var id = item.GetProviderId(MetadataProvider.MusicBrainzReleaseGroup);
if (!string.IsNullOrWhiteSpace(id))
{
await AudioDbAlbumProvider.Current.EnsureInfo(id, cancellationToken).ConfigureAwait(false);
var path = AudioDbAlbumProvider.GetAlbumInfoPath(_config.ApplicationPaths, id);
await using FileStream jsonStream = File.OpenRead(path);
var obj = await JsonSerializer.DeserializeAsync(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
if (obj != null && obj.album != null && obj.album.Count > 0)
{
return GetImages(obj.album[0]);
}
}
return new List();
}
private IEnumerable GetImages(AudioDbAlbumProvider.Album item)
{
var list = new List();
if (!string.IsNullOrWhiteSpace(item.strAlbumThumb))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strAlbumThumb,
Type = ImageType.Primary
});
}
if (!string.IsNullOrWhiteSpace(item.strAlbumCDart))
{
list.Add(new RemoteImageInfo
{
ProviderName = Name,
Url = item.strAlbumCDart,
Type = ImageType.Disc
});
}
return list;
}
///
public Task GetImageResponse(string url, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
return httpClient.GetAsync(url, cancellationToken);
}
///
public bool Supports(BaseItem item)
=> item is MusicAlbum;
}
}