123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Logging;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MediaBrowser.Providers
- {
- /// <summary>
- /// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
- /// </summary>
- public class ImageFromMediaLocationProvider : BaseMetadataProvider
- {
- public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
- : base(logManager, configurationManager)
- {
- }
- /// <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.LocationType == LocationType.FileSystem && item.ResolveArgs.IsDirectory;
- }
- /// <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;
- }
- }
- /// <summary>
- /// 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>
- public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- // Make sure current image paths still exist
- ValidateImages(item);
- cancellationToken.ThrowIfCancellationRequested();
- // Make sure current backdrop paths still exist
- ValidateBackdrops(item);
- cancellationToken.ThrowIfCancellationRequested();
- PopulateBaseItemImages(item);
- SetLastRefreshed(item, DateTime.UtcNow);
- return TrueTaskResult;
- }
- /// <summary>
- /// Validates that images within the item are still on the file system
- /// </summary>
- /// <param name="item">The item.</param>
- private void ValidateImages(BaseItem item)
- {
- // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
- var deletedKeys = item.Images.ToList().Where(image =>
- {
- var path = image.Value;
- return IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null;
- }).Select(i => i.Key).ToList();
- // Now remove them from the dictionary
- foreach (var key in deletedKeys)
- {
- item.Images.Remove(key);
- }
- }
- /// <summary>
- /// Validates that backdrops within the item are still on the file system
- /// </summary>
- /// <param name="item">The item.</param>
- private void ValidateBackdrops(BaseItem item)
- {
- if (item.BackdropImagePaths == null)
- {
- return;
- }
- // Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
- var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null).ToList();
- // Now remove them from the dictionary
- foreach (var path in deletedImages)
- {
- item.BackdropImagePaths.Remove(path);
- }
- }
- /// <summary>
- /// Determines whether [is in same directory] [the specified item].
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="path">The path.</param>
- /// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
- private bool IsInMetaLocation(BaseItem item, string path)
- {
- return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
- }
- /// <summary>
- /// Gets the image.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="filenameWithoutExtension">The filename without extension.</param>
- /// <returns>FileSystemInfo.</returns>
- protected virtual FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
- {
- return BaseItem.SupportedImageExtensions.Select(i => item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + i))).FirstOrDefault(i => i != null);
- }
- /// <summary>
- /// Fills in image paths based on files win the folder
- /// </summary>
- /// <param name="item">The item.</param>
- private void PopulateBaseItemImages(BaseItem item)
- {
- // Primary Image
- var image = GetImage(item, "folder") ??
- GetImage(item, "poster") ??
- GetImage(item, "cover") ??
- GetImage(item, "default");
- // Look for a file with the same name as the item
- if (image == null)
- {
- var name = Path.GetFileNameWithoutExtension(item.Path);
- if (!string.IsNullOrEmpty(name))
- {
- image = GetImage(item, name);
- }
- }
- if (image != null)
- {
- item.SetImage(ImageType.Primary, image.FullName);
- }
- // Logo Image
- image = GetImage(item, "logo");
- if (image != null)
- {
- item.SetImage(ImageType.Logo, image.FullName);
- }
- // Banner Image
- image = GetImage(item, "banner");
- if (image != null)
- {
- item.SetImage(ImageType.Banner, image.FullName);
- }
- // Clearart
- image = GetImage(item, "clearart");
- if (image != null)
- {
- item.SetImage(ImageType.Art, image.FullName);
- }
- // Thumbnail Image
- image = GetImage(item, "thumb");
- if (image != null)
- {
- item.SetImage(ImageType.Thumb, image.FullName);
- }
- // Box Image
- image = GetImage(item, "box");
- if (image != null)
- {
- item.SetImage(ImageType.Box, image.FullName);
- }
- // BoxRear Image
- image = GetImage(item, "boxrear");
- if (image != null)
- {
- item.SetImage(ImageType.BoxRear, image.FullName);
- }
- // Thumbnail Image
- image = GetImage(item, "menu");
- if (image != null)
- {
- item.SetImage(ImageType.Menu, image.FullName);
- }
- // Backdrop Image
- PopulateBackdrops(item);
- // Screenshot Image
- image = GetImage(item, "screenshot");
- var screenshotFiles = new List<string>();
- if (image != null)
- {
- screenshotFiles.Add(image.FullName);
- }
- var unfound = 0;
- for (var i = 1; i <= 20; i++)
- {
- // Screenshot Image
- image = GetImage(item, "screenshot" + i);
- if (image != null)
- {
- screenshotFiles.Add(image.FullName);
- }
- else
- {
- unfound++;
- if (unfound >= 3)
- {
- break;
- }
- }
- }
- if (screenshotFiles.Count > 0)
- {
- item.ScreenshotImagePaths = screenshotFiles;
- }
- }
- /// <summary>
- /// Populates the backdrops.
- /// </summary>
- /// <param name="item">The item.</param>
- private void PopulateBackdrops(BaseItem item)
- {
- var backdropFiles = new List<string>();
- PopulateBackdrops(item, backdropFiles, "backdrop", "backdrop");
- // Support plex/xbmc conventions
- PopulateBackdrops(item, backdropFiles, "fanart", "fanart-");
- PopulateBackdrops(item, backdropFiles, "background", "background-");
- if (backdropFiles.Count > 0)
- {
- item.BackdropImagePaths = backdropFiles;
- }
- }
- /// <summary>
- /// Populates the backdrops.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="backdropFiles">The backdrop files.</param>
- /// <param name="filename">The filename.</param>
- /// <param name="numberedSuffix">The numbered suffix.</param>
- private void PopulateBackdrops(BaseItem item, List<string> backdropFiles, string filename, string numberedSuffix)
- {
- var image = GetImage(item, filename);
- if (image != null)
- {
- backdropFiles.Add(image.FullName);
- }
- var unfound = 0;
- for (var i = 1; i <= 20; i++)
- {
- // Backdrop Image
- image = GetImage(item, numberedSuffix + i);
- if (image != null)
- {
- backdropFiles.Add(image.FullName);
- }
- else
- {
- unfound++;
- if (unfound >= 3)
- {
- break;
- }
- }
- }
- }
- }
- }
|