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, item.MetaLocation); cancellationToken.ThrowIfCancellationRequested(); SetPrimaryImagePath(episode, parent, item.MetaLocation, episodeFileName); SetLastRefreshed(item, DateTime.UtcNow); return TrueTaskResult; } /// /// Validates the primary image path still exists /// /// The episode. /// The metadata folder path. /// true if XXXX, false otherwise private void ValidateImage(Episode episode, string metadataFolderPath) { 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) { // Look for the image file in the metadata folder, and if found, set PrimaryImagePath var imageFiles = new[] { Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".jpg")), Path.Combine(metadataFolder, Path.ChangeExtension(episodeFileName, ".png")) }; var file = parent.ResolveArgs.GetMetaFileByPath(imageFiles[0]) ?? parent.ResolveArgs.GetMetaFileByPath(imageFiles[1]); if (file != null) { item.PrimaryImagePath = file.FullName; } } } }