2013-05-02 15:32:15 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2013-10-31 07:03:23 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-05-02 15:32:15 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-03-03 22:43:06 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-23 00:57:11 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2013-06-24 18:22:21 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 09:47:28 -07:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-04-27 17:44:38 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-04-27 17:44:38 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-06-09 09:47:28 -07:00
|
|
|
|
namespace MediaBrowser.Providers
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides images for generic types by looking for standard images in the IBN
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ImagesByNameProvider : ImageFromMediaLocationProvider
|
|
|
|
|
{
|
2013-10-30 07:40:14 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
|
|
|
|
public ImagesByNameProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
|
2013-04-27 17:44:38 -07:00
|
|
|
|
: base(logManager, configurationManager)
|
2013-03-02 10:59:15 -07:00
|
|
|
|
{
|
2013-10-30 07:40:14 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-03-02 10:59:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 18:22:21 -07:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07: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)
|
|
|
|
|
{
|
|
|
|
|
//only run for these generic types since we are expensive in file i/o
|
2013-04-23 12:22:37 -07:00
|
|
|
|
return item is IndexFolder || item is BasePluginFolder || item is CollectionFolder;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return MetadataProviderPriority.Last;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 12:17:21 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Needses the refresh internal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="providerInfo">The provider info.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
// Force a refresh if the IBN path changed
|
2013-06-21 11:50:44 -07:00
|
|
|
|
if (providerInfo.FileStamp != ConfigurationManager.ApplicationPaths.ItemsByNamePath.GetMD5())
|
2013-04-23 12:17:21 -07:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [refresh on file system stamp change].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
|
|
|
|
|
protected override bool RefreshOnFileSystemStampChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override this to return the date that should be compared to the last refresh date
|
|
|
|
|
/// to determine if this provider should be re-fetched.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>DateTime.</returns>
|
|
|
|
|
protected override DateTime CompareDate(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
// If the IBN location exists return the last modified date of any file in it
|
|
|
|
|
var location = GetLocation(item);
|
2013-04-13 16:43:41 -07:00
|
|
|
|
|
2013-05-26 18:43:28 -07:00
|
|
|
|
var directoryInfo = new DirectoryInfo(location);
|
|
|
|
|
|
|
|
|
|
if (!directoryInfo.Exists)
|
2013-04-13 16:43:41 -07:00
|
|
|
|
{
|
|
|
|
|
return DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-26 18:43:28 -07:00
|
|
|
|
var files = directoryInfo.EnumerateFiles().ToList();
|
2013-04-13 16:43:41 -07:00
|
|
|
|
|
|
|
|
|
if (files.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 17:44:38 -07:00
|
|
|
|
return files.Select(f =>
|
|
|
|
|
{
|
2013-10-31 07:03:23 -07:00
|
|
|
|
var lastWriteTime = _fileSystem.GetLastWriteTimeUtc(f);
|
2013-10-30 16:15:58 -07:00
|
|
|
|
var creationTime = _fileSystem.GetCreationTimeUtc(f);
|
2013-04-27 17:44:38 -07:00
|
|
|
|
|
|
|
|
|
return creationTime > lastWriteTime ? creationTime : lastWriteTime;
|
|
|
|
|
|
|
|
|
|
}).Max();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
2013-04-23 12:17:21 -07:00
|
|
|
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// </summary>
|
2013-04-23 12:17:21 -07:00
|
|
|
|
/// <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 async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var result = await base.FetchAsync(item, force, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
BaseProviderInfo data;
|
|
|
|
|
|
2013-05-06 06:21:34 -07:00
|
|
|
|
if (!item.ProviderData.TryGetValue(Id, out data))
|
2013-04-23 12:17:21 -07:00
|
|
|
|
{
|
2013-05-06 06:21:34 -07:00
|
|
|
|
data = new BaseProviderInfo();
|
|
|
|
|
item.ProviderData[Id] = data;
|
2013-04-23 12:17:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 11:50:44 -07:00
|
|
|
|
data.FileStamp = ConfigurationManager.ApplicationPaths.ItemsByNamePath.GetMD5();
|
2013-05-18 14:47:00 -07:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
|
2013-04-23 12:17:21 -07:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
protected string GetLocation(BaseItem item)
|
|
|
|
|
{
|
2013-10-30 07:40:14 -07:00
|
|
|
|
var name = _fileSystem.GetValidFilename(item.Name);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-03-03 22:43:06 -07:00
|
|
|
|
return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
2013-08-15 12:09:52 -07:00
|
|
|
|
/// <param name="args">The args.</param>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <param name="filenameWithoutExtension">The filename without extension.</param>
|
2013-04-30 10:21:21 -07:00
|
|
|
|
/// <returns>FileSystemInfo.</returns>
|
2013-08-15 12:09:52 -07:00
|
|
|
|
protected override FileSystemInfo GetImage(BaseItem item, ItemResolveArgs args, string filenameWithoutExtension)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
var location = GetLocation(item);
|
|
|
|
|
|
2013-05-27 09:53:10 -07:00
|
|
|
|
var directoryInfo = new DirectoryInfo(location);
|
|
|
|
|
|
|
|
|
|
if (!directoryInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var files = directoryInfo.EnumerateFiles("*", SearchOption.TopDirectoryOnly).ToList();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-05-25 08:16:50 -07:00
|
|
|
|
var file = files.FirstOrDefault(i => string.Equals(i.Name, filenameWithoutExtension + ".png", StringComparison.OrdinalIgnoreCase));
|
2013-04-27 22:29:27 -07:00
|
|
|
|
|
2013-05-25 08:16:50 -07:00
|
|
|
|
if (file != null)
|
2013-04-27 22:29:27 -07:00
|
|
|
|
{
|
2013-05-25 08:16:50 -07:00
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file = files.FirstOrDefault(i => string.Equals(i.Name, filenameWithoutExtension + ".jpg", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
return file;
|
2013-04-27 22:29:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|