2013-03-04 05:43:06 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
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;
|
|
|
|
|
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
|
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
public ImageFromMediaLocationProvider(ILogManager logManager, IServerConfigurationManager configurationManager)
|
|
|
|
|
: base(logManager, configurationManager)
|
2013-03-02 17:59:15 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-05-02 02:44:52 +00:00
|
|
|
|
return item.LocationType == LocationType.FileSystem && item.ResolveArgs.IsDirectory;
|
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>
|
|
|
|
|
/// 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-03-08 05:08:27 +00:00
|
|
|
|
public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-04-28 05:29:27 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// Make sure current image paths still exist
|
|
|
|
|
ValidateImages(item);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
// Make sure current backdrop paths still exist
|
|
|
|
|
ValidateBackdrops(item);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-04-28 05:29:27 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (item.Images == 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
|
2013-05-18 18:16:07 +00:00
|
|
|
|
var deletedKeys = item.Images.ToList().Where(image =>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
var path = image.Value;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
return IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null;
|
2013-05-18 18:16:07 +00:00
|
|
|
|
|
|
|
|
|
}).Select(i => i.Key).ToList();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
// Now remove them from the dictionary
|
2013-04-28 05:29:27 +00:00
|
|
|
|
foreach (var key in deletedKeys)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
2013-04-28 05:29:27 +00:00
|
|
|
|
var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && item.ResolveArgs.GetMetaFileByPath(path) == null).ToList();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
// 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>
|
2013-03-14 19:52:53 +00:00
|
|
|
|
private bool IsInMetaLocation(BaseItem item, string path)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-14 02:13:32 +00:00
|
|
|
|
return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="filenameWithoutExtension">The filename without extension.</param>
|
2013-04-30 17:21:21 +00:00
|
|
|
|
/// <returns>FileSystemInfo.</returns>
|
2013-04-28 05:29:27 +00:00
|
|
|
|
protected virtual FileSystemInfo GetImage(BaseItem item, string filenameWithoutExtension)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-30 22:22:15 +00:00
|
|
|
|
return BaseItem.SupportedImageExtensions.Select(i => item.ResolveArgs.GetMetaFileByPath(Path.Combine(item.ResolveArgs.Path, filenameWithoutExtension + i))).FirstOrDefault(i => i != null);
|
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>
|
|
|
|
|
private void PopulateBaseItemImages(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
// Primary Image
|
2013-05-30 18:04:33 +00:00
|
|
|
|
var image = GetImage(item, "folder") ??
|
|
|
|
|
GetImage(item, "poster") ??
|
2013-05-30 22:22:15 +00:00
|
|
|
|
GetImage(item, "cover") ??
|
2013-05-30 18:04:33 +00:00
|
|
|
|
GetImage(item, "default");
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Primary, image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Logo Image
|
|
|
|
|
image = GetImage(item, "logo");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Logo, image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Banner Image
|
|
|
|
|
image = GetImage(item, "banner");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Banner, image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clearart
|
|
|
|
|
image = GetImage(item, "clearart");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Art, image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Thumbnail Image
|
|
|
|
|
image = GetImage(item, "thumb");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Thumb, image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 21:20:27 +00:00
|
|
|
|
// Box Image
|
2013-04-14 21:44:54 +00:00
|
|
|
|
image = GetImage(item, "box");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-04-14 21:44:54 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Box, image.FullName);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-04 21:20:27 +00:00
|
|
|
|
// BoxRear Image
|
|
|
|
|
image = GetImage(item, "boxrear");
|
|
|
|
|
|
|
|
|
|
if (image != null)
|
|
|
|
|
{
|
|
|
|
|
item.SetImage(ImageType.BoxRear, image.FullName);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 21:44:54 +00:00
|
|
|
|
// Thumbnail Image
|
|
|
|
|
image = GetImage(item, "menu");
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-04-14 21:44:54 +00:00
|
|
|
|
{
|
2013-04-28 05:29:27 +00:00
|
|
|
|
item.SetImage(ImageType.Menu, image.FullName);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
// Backdrop Image
|
2013-05-30 18:04:33 +00:00
|
|
|
|
PopulateBackdrops(item);
|
|
|
|
|
|
|
|
|
|
// Screenshot Image
|
|
|
|
|
image = GetImage(item, "screenshot");
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-30 19:58:07 +00:00
|
|
|
|
var screenshotFiles = new List<string>();
|
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
screenshotFiles.Add(image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var unfound = 0;
|
|
|
|
|
for (var i = 1; i <= 20; i++)
|
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
// Screenshot Image
|
|
|
|
|
image = GetImage(item, "screenshot" + i);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
screenshotFiles.Add(image.FullName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unfound++;
|
|
|
|
|
|
|
|
|
|
if (unfound >= 3)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 18:04:33 +00:00
|
|
|
|
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-");
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
if (backdropFiles.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
item.BackdropImagePaths = backdropFiles;
|
|
|
|
|
}
|
2013-05-30 18:04:33 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-30 18:04:33 +00:00
|
|
|
|
/// <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);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-04-14 21:44:54 +00:00
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
backdropFiles.Add(image.FullName);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 18:04:33 +00:00
|
|
|
|
var unfound = 0;
|
2013-04-14 21:44:54 +00:00
|
|
|
|
for (var i = 1; i <= 20; i++)
|
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
// Backdrop Image
|
|
|
|
|
image = GetImage(item, numberedSuffix + i);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
|
2013-04-28 05:29:27 +00:00
|
|
|
|
if (image != null)
|
2013-04-14 21:44:54 +00:00
|
|
|
|
{
|
2013-05-30 18:04:33 +00:00
|
|
|
|
backdropFiles.Add(image.FullName);
|
2013-04-14 21:44:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unfound++;
|
|
|
|
|
|
|
|
|
|
if (unfound >= 3)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|