using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Music
{
///
/// Class FanArtAlbumProvider
///
public class FanArtAlbumProvider : FanartBaseProvider
{
///
/// The _provider manager
///
private readonly IProviderManager _providerManager;
///
/// Gets the HTTP client.
///
/// The HTTP client.
protected IHttpClient HttpClient { get; private set; }
private readonly IFileSystem _fileSystem;
///
/// Initializes a new instance of the class.
///
/// The HTTP client.
/// The log manager.
/// The configuration manager.
/// The provider manager.
public FanArtAlbumProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
: base(logManager, configurationManager)
{
_providerManager = providerManager;
_fileSystem = fileSystem;
HttpClient = httpClient;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Fourth; }
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is MusicAlbum;
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
///
/// Gets a value indicating whether [refresh on version change].
///
/// true if [refresh on version change]; otherwise, false.
protected override bool RefreshOnVersionChange
{
get
{
return true;
}
}
///
/// Gets the provider version.
///
/// The provider version.
protected override string ProviderVersion
{
get
{
return "17";
}
}
///
/// Needses the refresh internal.
///
/// The item.
/// The provider info.
/// true if XXXX, false otherwise
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (!ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc &&
!ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary)
{
return false;
}
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Disc))
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
protected override DateTime CompareDate(BaseItem item)
{
var artistMusicBrainzId = item.Parent.GetProviderId(MetadataProviders.Musicbrainz);
if (!string.IsNullOrEmpty(artistMusicBrainzId))
{
var artistXmlPath = FanArtArtistProvider.GetArtistDataPath(ConfigurationManager.CommonApplicationPaths, artistMusicBrainzId);
artistXmlPath = Path.Combine(artistXmlPath, "fanart.xml");
var file = new FileInfo(artistXmlPath);
if (file.Exists)
{
return _fileSystem.GetLastWriteTimeUtc(file);
}
}
return base.CompareDate(item);
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token.
/// Task{System.Boolean}.
public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualFanartAlbumProvider.ProviderName).ConfigureAwait(false);
await FetchFromXml(item, images.ToList(), cancellationToken).ConfigureAwait(false);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
///
/// Fetches from XML.
///
/// The item.
/// The images.
/// The cancellation token.
/// Task.
private async Task FetchFromXml(BaseItem item, List images, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary && !item.HasImage(ImageType.Primary))
{
var image = images.FirstOrDefault(i => i.Type == ImageType.Primary);
if (image != null)
{
await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
}
}
cancellationToken.ThrowIfCancellationRequested();
if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !item.HasImage(ImageType.Disc))
{
var image = images.FirstOrDefault(i => i.Type == ImageType.Disc);
if (image != null)
{
await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Disc, null, cancellationToken).ConfigureAwait(false);
}
}
}
}
}