jellyfin-server/MediaBrowser.TV/Providers/EpisodeImageFromMediaLocationProvider.cs

69 lines
2.3 KiB
C#
Raw Normal View History

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