2012-08-22 04:50:59 +02:00
|
|
|
|
using System.ComponentModel.Composition;
|
2012-08-19 18:41:47 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.TV.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.TV.Providers
|
|
|
|
|
{
|
|
|
|
|
[Export(typeof(BaseMetadataProvider))]
|
|
|
|
|
public class EpisodeImageFromMediaLocationProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
public override bool Supports(BaseEntity item)
|
2012-08-19 18:41:47 +02:00
|
|
|
|
{
|
|
|
|
|
return item is Episode;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 17:55:05 +02:00
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.First; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
2012-08-19 18:41:47 +02:00
|
|
|
|
{
|
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
Episode episode = item as Episode;
|
|
|
|
|
|
2012-08-19 18:41:47 +02:00
|
|
|
|
string metadataFolder = Path.Combine(args.Parent.Path, "metadata");
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
string episodeFileName = Path.GetFileName(episode.Path);
|
2012-08-19 18:41:47 +02:00
|
|
|
|
|
|
|
|
|
Season season = args.Parent as Season;
|
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
SetPrimaryImagePath(episode, season, metadataFolder, episodeFileName);
|
2012-08-19 18:41:47 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetPrimaryImagePath(Episode item, Season season, string metadataFolder, string episodeFileName)
|
|
|
|
|
{
|
|
|
|
|
string[] imageFiles = new string[] {
|
|
|
|
|
Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".jpg")),
|
|
|
|
|
Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".png"))
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-21 18:33:38 +02:00
|
|
|
|
string image;
|
|
|
|
|
|
2012-08-19 18:41:47 +02:00
|
|
|
|
if (season == null)
|
|
|
|
|
{
|
|
|
|
|
// Gotta do this the slow way
|
2012-08-21 18:33:38 +02:00
|
|
|
|
image = imageFiles.FirstOrDefault(f => File.Exists(f));
|
2012-08-19 18:41:47 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-21 18:33:38 +02:00
|
|
|
|
image = imageFiles.FirstOrDefault(f => season.ContainsMetadataFile(f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we found something, set PrimaryImagePath
|
|
|
|
|
if (!string.IsNullOrEmpty(image))
|
|
|
|
|
{
|
|
|
|
|
item.PrimaryImagePath = image;
|
2012-08-19 18:41:47 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|