jellyfin/MediaBrowser.Providers/TV/EpisodeImageFromMediaLocationProvider.cs

164 lines
5.3 KiB
C#
Raw Normal View History

2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
2013-09-06 17:26:56 +00:00
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.TV
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class EpisodeImageFromMediaLocationProvider
/// </summary>
public class EpisodeImageFromMediaLocationProvider : BaseMetadataProvider
{
public EpisodeImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
2013-03-02 17:59:15 +00:00
{
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
2013-10-15 22:16:50 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Supportses the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
public override bool Supports(BaseItem item)
{
return item is Episode && item.LocationType == LocationType.FileSystem;
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
/// <summary>
/// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
/// </summary>
/// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
protected override bool RefreshOnFileSystemStampChange
{
get
{
return true;
}
}
/// <summary>
/// Gets the filestamp extensions.
/// </summary>
/// <value>The filestamp extensions.</value>
protected override string[] FilestampExtensions
{
get
{
return BaseItem.SupportedImageExtensions;
}
}
2013-10-15 22:16:50 +00:00
/// <summary>
2013-02-21 01:33:05 +00:00
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
2013-12-06 20:07:34 +00:00
public override Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
2013-02-21 01:33:05 +00:00
{
cancellationToken.ThrowIfCancellationRequested();
2013-02-21 01:33:05 +00:00
var episode = (Episode)item;
var episodeFileName = Path.GetFileName(episode.Path);
var parent = item.ResolveArgs.Parent;
ValidateImage(episode);
2013-02-21 01:33:05 +00:00
cancellationToken.ThrowIfCancellationRequested();
2013-02-21 01:33:05 +00:00
SetPrimaryImagePath(episode, parent, item.MetaLocation, episodeFileName);
2013-12-06 20:07:34 +00:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-02-21 01:33:05 +00:00
return TrueTaskResult;
}
/// <summary>
/// Validates the primary image path still exists
/// </summary>
/// <param name="episode">The episode.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
private void ValidateImage(Episode episode)
2013-02-21 01:33:05 +00:00
{
var path = episode.PrimaryImagePath;
if (string.IsNullOrEmpty(path))
{
return;
}
2013-09-06 17:26:56 +00:00
if (!File.Exists(path))
2013-02-21 01:33:05 +00:00
{
episode.PrimaryImagePath = null;
}
}
/// <summary>
/// Sets the primary image path.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="parent">The parent.</param>
/// <param name="metadataFolder">The metadata folder.</param>
/// <param name="episodeFileName">Name of the episode file.</param>
private void SetPrimaryImagePath(Episode item, Folder parent, string metadataFolder, string episodeFileName)
{
2013-10-15 22:16:50 +00:00
foreach (var extension in BaseItem.SupportedImageExtensions)
{
var path = Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, extension));
2013-02-21 01:33:05 +00:00
2013-10-15 22:16:50 +00:00
var file = parent.ResolveArgs.GetMetaFileByPath(path);
if (file != null)
{
item.PrimaryImagePath = file.FullName;
return;
}
}
2013-02-21 01:33:05 +00:00
2013-10-15 22:16:50 +00:00
var seasonFolder = Path.GetDirectoryName(item.Path);
foreach (var extension in BaseItem.SupportedImageExtensions)
2013-02-21 01:33:05 +00:00
{
2013-10-15 22:16:50 +00:00
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;
}
2013-02-21 01:33:05 +00:00
}
}
}
}