jellyfin-server/MediaBrowser.Providers/ImageFromMediaLocationProvider.cs

649 lines
21 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
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-05-02 02:44:52 +00:00
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
2013-09-18 02:43:34 +00:00
using System.Globalization;
2013-02-21 01:33:05 +00:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Provides images for all types by looking for standard images - folder, backdrop, logo, etc.
/// </summary>
public class ImageFromMediaLocationProvider : BaseMetadataProvider
{
protected readonly IFileSystem FileSystem;
2013-12-08 01:42:15 +00:00
public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
: base(logManager, configurationManager)
2013-03-02 17:59:15 +00:00
{
FileSystem = fileSystem;
2013-03-02 17:59:15 +00:00
}
public override ItemUpdateType ItemUpdateType
{
get
{
return ItemUpdateType.ImageUpdate;
}
}
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)
{
if (item.LocationType == LocationType.FileSystem)
{
if (item.ResolveArgs.IsDirectory)
{
return true;
}
2013-08-16 20:13:45 +00:00
return item.IsInMixedFolder && item.Parent != null && !(item is Episode);
}
2014-01-05 05:15:38 +00:00
if (item.LocationType == LocationType.Virtual)
{
var season = item as Season;
if (season != null)
{
var series = season.Series;
if (series != null && series.LocationType == LocationType.FileSystem)
{
return true;
}
}
}
2013-12-08 01:42:15 +00:00
return false;
2013-02-21 01:33:05 +00:00
}
protected override IEnumerable<BaseItem> GetItemsForFileStampComparison(BaseItem item)
{
var season = item as Season;
if (season != null)
{
2014-01-05 05:15:38 +00:00
var list = new List<BaseItem>();
if (season.LocationType == LocationType.FileSystem)
{
list.Add(season);
}
var series = season.Series;
2014-01-05 05:15:38 +00:00
if (series != null && series.LocationType == LocationType.FileSystem)
{
2014-01-05 05:15:38 +00:00
list.Add(series);
}
2014-01-05 05:15:38 +00:00
return list;
}
return base.GetItemsForFileStampComparison(item);
}
2013-02-21 01:33:05 +00:00
/// <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-02-21 01:33:05 +00:00
/// <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>
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
// Make sure current image paths still exist
item.ValidateImages();
2013-02-21 01:33:05 +00:00
cancellationToken.ThrowIfCancellationRequested();
// Make sure current backdrop paths still exist
item.ValidateBackdrops();
2013-12-06 03:39:44 +00:00
var hasScreenshots = item as IHasScreenshots;
if (hasScreenshots != null)
{
hasScreenshots.ValidateScreenshots();
}
2013-02-21 01:33:05 +00:00
cancellationToken.ThrowIfCancellationRequested();
var args = GetResolveArgsContainingImages(item);
PopulateBaseItemImages(item, args);
2013-02-21 01:33:05 +00:00
2013-12-06 20:07:34 +00:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-02-21 01:33:05 +00:00
return TrueTaskResult;
}
private ItemResolveArgs GetResolveArgsContainingImages(BaseItem item)
{
2014-01-05 05:15:38 +00:00
if (item.LocationType != LocationType.FileSystem)
{
return null;
}
if (item.IsInMixedFolder)
{
2013-08-16 20:13:45 +00:00
if (item.Parent == null)
{
return item.ResolveArgs;
}
return item.Parent.ResolveArgs;
}
return item.ResolveArgs;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the image.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
2013-02-21 01:33:05 +00:00
/// <param name="filenameWithoutExtension">The filename without extension.</param>
2013-04-30 17:21:21 +00:00
/// <returns>FileSystemInfo.</returns>
protected virtual FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
{
2014-01-05 05:15:38 +00:00
if (string.IsNullOrEmpty(item.MetaLocation))
{
return null;
}
2013-12-08 01:42:15 +00:00
return BaseItem.SupportedImageExtensions
2014-01-05 05:15:38 +00:00
.Select(i => args.GetMetaFileByPath(GetFullImagePath(item, args, filenameWithoutExtension, i)))
2013-12-08 01:42:15 +00:00
.FirstOrDefault(i => i != null);
}
protected virtual string GetFullImagePath(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension, string extension)
2013-02-21 01:33:05 +00:00
{
var path = item.MetaLocation;
if (item.IsInMixedFolder)
{
var pathFilenameWithoutExtension = Path.GetFileNameWithoutExtension(item.Path);
// If the image filename and path file name match, just look for an image using the same full path as the item
if (string.Equals(pathFilenameWithoutExtension, filenameWithoutExtension))
{
return Path.ChangeExtension(item.Path, extension);
}
return Path.Combine(path, pathFilenameWithoutExtension + "-" + filenameWithoutExtension + extension);
}
return Path.Combine(path, filenameWithoutExtension + extension);
2013-02-21 01:33:05 +00:00
}
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2013-02-21 01:33:05 +00:00
/// <summary>
/// Fills in image paths based on files win the folder
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
private void PopulateBaseItemImages(BaseItem item, ItemResolveArgs args)
2013-02-21 01:33:05 +00:00
{
2013-10-16 01:44:23 +00:00
PopulatePrimaryImage(item, args);
2013-02-21 01:33:05 +00:00
// Logo Image
2013-10-16 01:44:23 +00:00
var image = GetImage(item, args, "logo");
2013-02-21 01:33:05 +00:00
if (image != null)
2013-02-21 01:33:05 +00:00
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Logo, image.FullName);
2013-02-21 01:33:05 +00:00
}
// Clearart
image = GetImage(item, args, "clearart");
2013-02-21 01:33:05 +00:00
if (image != null)
2013-02-21 01:33:05 +00:00
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Art, image.FullName);
2013-02-21 01:33:05 +00:00
}
2013-06-29 12:42:34 +00:00
// Disc
2013-08-22 13:59:53 +00:00
image = GetImage(item, args, "disc") ??
GetImage(item, args, "cdart");
2013-06-29 12:42:34 +00:00
if (image != null)
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Disc, image.FullName);
2013-06-29 12:42:34 +00:00
}
// Box Image
image = GetImage(item, args, "box");
2013-04-14 21:44:54 +00:00
if (image != null)
2013-04-14 21:44:54 +00:00
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Box, image.FullName);
2013-04-14 21:44:54 +00:00
}
// BoxRear Image
image = GetImage(item, args, "boxrear");
if (image != null)
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.BoxRear, image.FullName);
}
2013-04-14 21:44:54 +00:00
// Thumbnail Image
image = GetImage(item, args, "menu");
2013-04-14 21:44:54 +00:00
if (image != null)
2013-04-14 21:44:54 +00:00
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Menu, image.FullName);
2013-04-14 21:44:54 +00:00
}
2013-10-16 01:44:23 +00:00
PopulateBanner(item, args);
PopulateThumb(item, args);
2013-02-21 01:33:05 +00:00
// Backdrop Image
PopulateBackdrops(item, args);
2013-10-16 01:44:23 +00:00
PopulateScreenshots(item, args);
}
2013-10-16 01:44:23 +00:00
private void PopulatePrimaryImage(BaseItem item, ItemResolveArgs args)
{
// Primary Image
var image = GetImage(item, args, "folder") ??
GetImage(item, args, "poster") ??
GetImage(item, args, "cover") ??
GetImage(item, args, "default");
2013-02-21 01:33:05 +00:00
2013-10-16 01:44:23 +00:00
// Support plex/xbmc convention
if (image == null && item is Series)
{
2013-10-16 14:22:26 +00:00
image = GetImage(item, args, "show");
2013-10-16 01:44:23 +00:00
}
// Support plex/xbmc convention
2013-12-08 01:42:15 +00:00
if (image == null)
2013-10-16 01:44:23 +00:00
{
2013-12-08 01:42:15 +00:00
// Supprt xbmc conventions
var season = item as Season;
2014-01-05 05:15:38 +00:00
if (season != null && item.IndexNumber.HasValue && season.Series.LocationType == LocationType.FileSystem)
2013-10-16 01:44:23 +00:00
{
2013-12-08 01:42:15 +00:00
image = GetSeasonImageFromSeriesFolder(season, "-poster");
2013-10-16 01:44:23 +00:00
}
}
// Support plex/xbmc convention
if (image == null && (item is Movie || item is MusicVideo || item is AdultVideo))
{
image = GetImage(item, args, "movie");
}
// Look for a file with the same name as the item
2014-01-05 05:15:38 +00:00
if (image == null && !string.IsNullOrEmpty(item.Path))
2013-10-16 01:44:23 +00:00
{
var name = Path.GetFileNameWithoutExtension(item.Path);
if (!string.IsNullOrEmpty(name))
{
image = GetImage(item, args, name) ??
GetImage(item, args, name + "-poster");
}
}
2013-05-30 19:58:07 +00:00
if (image != null)
2013-02-21 01:33:05 +00:00
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Primary, image.FullName);
2013-02-21 01:33:05 +00:00
}
2013-10-16 01:44:23 +00:00
}
2013-02-21 01:33:05 +00:00
2013-10-16 01:44:23 +00:00
/// <summary>
/// Populates the banner.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
private void PopulateBanner(BaseItem item, ItemResolveArgs args)
{
// Banner Image
var image = GetImage(item, args, "banner");
if (image == null)
2013-02-21 01:33:05 +00:00
{
2013-10-16 01:44:23 +00:00
// Supprt xbmc conventions
2013-12-08 01:42:15 +00:00
var season = item as Season;
2014-01-05 05:15:38 +00:00
if (season != null && item.IndexNumber.HasValue && season.Series.LocationType == LocationType.FileSystem)
2013-10-16 01:44:23 +00:00
{
2013-12-08 01:42:15 +00:00
image = GetSeasonImageFromSeriesFolder(season, "-banner");
2013-10-16 01:44:23 +00:00
}
}
if (image != null)
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Banner, image.FullName);
2013-10-16 01:44:23 +00:00
}
}
/// <summary>
/// Populates the thumb.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
private void PopulateThumb(BaseItem item, ItemResolveArgs args)
{
// Thumbnail Image
2013-12-21 20:29:28 +00:00
var image = GetImage(item, args, "thumb") ??
GetImage(item, args, "landscape");
2013-10-16 01:44:23 +00:00
if (image == null)
{
// Supprt xbmc conventions
2013-12-08 01:42:15 +00:00
var season = item as Season;
2014-01-05 05:15:38 +00:00
if (season != null && item.IndexNumber.HasValue && season.Series.LocationType == LocationType.FileSystem)
2013-02-21 01:33:05 +00:00
{
2013-12-08 01:42:15 +00:00
image = GetSeasonImageFromSeriesFolder(season, "-landscape");
2013-02-21 01:33:05 +00:00
}
}
2013-10-16 01:44:23 +00:00
if (image != null)
{
2013-12-19 21:51:32 +00:00
item.SetImagePath(ImageType.Thumb, image.FullName);
}
2013-10-16 01:44:23 +00:00
}
/// <summary>
/// Populates the backdrops.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
private void PopulateBackdrops(BaseItem item, ItemResolveArgs args)
{
var backdropFiles = new List<string>();
PopulateBackdrops(item, args, backdropFiles, "backdrop", "backdrop");
// Support {name}-fanart.ext
2014-01-05 05:15:38 +00:00
if (!string.IsNullOrEmpty(item.Path))
{
var name = Path.GetFileNameWithoutExtension(item.Path);
if (!string.IsNullOrEmpty(name))
{
var image = GetImage(item, args, name + "-fanart");
if (image != null)
{
backdropFiles.Add(image.FullName);
}
}
}
// Support plex/xbmc conventions
PopulateBackdrops(item, args, backdropFiles, "fanart", "fanart-");
PopulateBackdrops(item, args, backdropFiles, "background", "background-");
PopulateBackdrops(item, args, backdropFiles, "art", "art-");
2013-12-08 01:42:15 +00:00
var season = item as Season;
2014-01-05 05:15:38 +00:00
if (season != null && item.IndexNumber.HasValue && season.Series.LocationType == LocationType.FileSystem)
2013-10-16 01:44:23 +00:00
{
2013-12-08 01:42:15 +00:00
var image = GetSeasonImageFromSeriesFolder(season, "-fanart");
2013-10-16 01:44:23 +00:00
2013-12-08 01:42:15 +00:00
if (image != null)
2013-10-16 01:44:23 +00:00
{
backdropFiles.Add(image.FullName);
}
}
2014-01-05 05:15:38 +00:00
if (item.LocationType == LocationType.FileSystem)
2013-10-21 01:03:33 +00:00
{
PopulateBackdropsFromExtraFanart(args, backdropFiles);
}
2013-02-21 01:33:05 +00:00
if (backdropFiles.Count > 0)
{
item.BackdropImagePaths = backdropFiles;
}
}
2013-02-21 01:33:05 +00:00
2013-12-08 01:42:15 +00:00
private FileSystemInfo GetSeasonImageFromSeriesFolder(Season season, string imageSuffix)
{
var series = season.Series;
var seriesFolderArgs = series.ResolveArgs;
var seasonNumber = season.IndexNumber;
string filename = null;
FileSystemInfo image;
if (seasonNumber.HasValue)
{
var seasonMarker = seasonNumber.Value == 0
? "-specials"
: seasonNumber.Value.ToString("00", _usCulture);
// Get this one directly from the file system since we have to go up a level
filename = "season" + seasonMarker + imageSuffix;
image = GetImage(series, seriesFolderArgs, filename);
if (image != null && image.Exists)
{
return image;
}
}
var previousFilename = filename;
// Try using the season name
filename = season.Name.ToLower().Replace(" ", string.Empty) + imageSuffix;
if (!string.Equals(previousFilename, filename))
{
image = GetImage(series, seriesFolderArgs, filename);
if (image != null && image.Exists)
{
return image;
}
}
return null;
}
/// <summary>
/// Populates the backdrops from extra fanart.
/// </summary>
/// <param name="args">The args.</param>
/// <param name="backdrops">The backdrops.</param>
private void PopulateBackdropsFromExtraFanart(ItemResolveArgs args, List<string> backdrops)
{
if (!args.IsDirectory)
{
return;
}
if (args.ContainsFileSystemEntryByName("extrafanart"))
{
var path = Path.Combine(args.Path, "extrafanart");
var imageFiles = Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly)
.Where(i =>
{
var extension = Path.GetExtension(i);
if (string.IsNullOrEmpty(extension))
{
return false;
}
return BaseItem.SupportedImageExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase);
})
.ToList();
backdrops.AddRange(imageFiles);
}
}
/// <summary>
/// Populates the backdrops.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</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, ItemResolveArgs args, List<string> backdropFiles, string filename, string numberedSuffix)
{
var image = GetImage(item, args, filename);
2013-04-14 21:44:54 +00:00
if (image != null)
2013-04-14 21:44:54 +00:00
{
backdropFiles.Add(image.FullName);
2013-04-14 21:44:54 +00:00
}
var unfound = 0;
2013-04-14 21:44:54 +00:00
for (var i = 1; i <= 20; i++)
{
// Backdrop Image
image = GetImage(item, args, numberedSuffix + i);
2013-04-14 21:44:54 +00:00
if (image != null)
2013-04-14 21:44:54 +00:00
{
backdropFiles.Add(image.FullName);
2013-04-14 21:44:54 +00:00
}
else
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
}
2013-10-16 01:44:23 +00:00
/// <summary>
/// Populates the screenshots.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
private void PopulateScreenshots(BaseItem item, ItemResolveArgs args)
{
// Screenshot Image
var image = GetImage(item, args, "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, args, "screenshot" + i);
if (image != null)
{
screenshotFiles.Add(image.FullName);
}
else
{
unfound++;
if (unfound >= 3)
{
break;
}
}
}
if (screenshotFiles.Count > 0)
{
var hasScreenshots = item as IHasScreenshots;
if (hasScreenshots != null)
{
hasScreenshots.ScreenshotImagePaths = screenshotFiles;
}
2013-10-16 01:44:23 +00:00
}
}
protected FileSystemInfo GetImageFromLocation(string path, string filenameWithoutExtension)
{
try
{
var files = new DirectoryInfo(path)
.EnumerateFiles()
.Where(i =>
{
var fileName = Path.GetFileNameWithoutExtension(i.FullName);
if (!string.Equals(fileName, filenameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return false;
}
var ext = i.Extension;
return !string.IsNullOrEmpty(ext) &&
BaseItem.SupportedImageExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase);
})
.ToList();
return BaseItem.SupportedImageExtensions
.Select(ext => files.FirstOrDefault(i => string.Equals(ext, i.Extension, StringComparison.OrdinalIgnoreCase)))
.FirstOrDefault(file => file != null);
}
catch (DirectoryNotFoundException)
{
return null;
}
}
2013-02-21 01:33:05 +00:00
}
}