using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.TV
{
///
/// Class EpisodeImageFromMediaLocationProvider
///
public class EpisodeImageFromMediaLocationProvider : BaseMetadataProvider
{
public EpisodeImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
{
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is Episode && item.LocationType == LocationType.FileSystem;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
///
/// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
///
/// true if [refresh on file system stamp change]; otherwise, false.
protected override bool RefreshOnFileSystemStampChange
{
get
{
return true;
}
}
///
/// Gets the filestamp extensions.
///
/// The filestamp extensions.
protected override string[] FilestampExtensions
{
get
{
return BaseItem.SupportedImageExtensions;
}
}
///
/// 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 Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var episode = (Episode)item;
var episodeFileName = Path.GetFileName(episode.Path);
var parent = item.ResolveArgs.Parent;
ValidateImage(episode);
cancellationToken.ThrowIfCancellationRequested();
SetPrimaryImagePath(episode, parent, item.MetaLocation, episodeFileName);
SetLastRefreshed(item, DateTime.UtcNow);
return TrueTaskResult;
}
///
/// Validates the primary image path still exists
///
/// The episode.
/// true if XXXX, false otherwise
private void ValidateImage(Episode episode)
{
var path = episode.PrimaryImagePath;
if (string.IsNullOrEmpty(path))
{
return;
}
if (!File.Exists(path))
{
episode.PrimaryImagePath = null;
}
}
///
/// Sets the primary image path.
///
/// The item.
/// The parent.
/// The metadata folder.
/// Name of the episode file.
private void SetPrimaryImagePath(Episode item, Folder parent, string metadataFolder, string episodeFileName)
{
foreach (var extension in BaseItem.SupportedImageExtensions)
{
var path = Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, extension));
var file = parent.ResolveArgs.GetMetaFileByPath(path);
if (file != null)
{
item.PrimaryImagePath = file.FullName;
return;
}
}
var seasonFolder = Path.GetDirectoryName(item.Path);
foreach (var extension in BaseItem.SupportedImageExtensions)
{
var imageFilename = Path.GetFileNameWithoutExtension(episodeFileName) + "-thumb" + extension;
var path = Path.Combine(seasonFolder, imageFilename);
var file = parent.ResolveArgs.GetMetaFileByPath(path);
if (file != null)
{
item.PrimaryImagePath = file.FullName;
return;
}
}
}
}
}