jellyfin-server/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs

199 lines
6.8 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Configuration;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Entities;
2013-02-23 07:57:11 +00:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.Logging;
2013-02-21 01:33:05 +00:00
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Controller.Providers
{
/// <summary>
/// Provides images for generic types by looking for standard images in the IBN
/// </summary>
public class ImagesByNameProvider : ImageFromMediaLocationProvider
{
public ImagesByNameProvider(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)
{
//only run for these generic types since we are expensive in file i/o
2013-04-23 19:22:37 +00:00
return item is IndexFolder || item is BasePluginFolder || item is CollectionFolder;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get
{
return MetadataProviderPriority.Last;
}
}
2013-04-23 19:17:21 +00: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
if (!string.Equals(providerInfo.CustomData, ConfigurationManager.ApplicationPaths.ItemsByNamePath, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
2013-02-21 01:33:05 +00: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 23:43:41 +00:00
if (!Directory.Exists(location))
{
return DateTime.MinValue;
}
var files = new DirectoryInfo(location).EnumerateFiles("*", SearchOption.TopDirectoryOnly).ToList();
2013-04-13 23:43:41 +00:00
if (files.Count == 0)
{
return DateTime.MinValue;
}
return files.Select(f =>
{
var lastWriteTime = GetLastWriteTimeUtc(f);
var creationTime = GetCreationTimeUtc(f);
return creationTime > lastWriteTime ? creationTime : lastWriteTime;
}).Max();
}
/// <summary>
/// Gets the creation time UTC.
/// </summary>
/// <param name="info">The info.</param>
/// <returns>DateTime.</returns>
private DateTime GetLastWriteTimeUtc(FileSystemInfo info)
{
// This could throw an error on some file systems that have dates out of range
try
{
return info.LastAccessTimeUtc;
}
catch (Exception ex)
{
Logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
return DateTime.MinValue;
}
}
/// <summary>
/// Gets the creation time UTC.
/// </summary>
/// <param name="info">The info.</param>
/// <returns>DateTime.</returns>
private DateTime GetCreationTimeUtc(FileSystemInfo info)
{
// This could throw an error on some file systems that have dates out of range
try
{
return info.CreationTimeUtc;
}
catch (Exception ex)
{
Logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
return DateTime.MinValue;
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
2013-04-23 19:17:21 +00:00
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
2013-02-21 01:33:05 +00:00
/// </summary>
2013-04-23 19:17:21 +00: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;
if (item.ProviderData.TryGetValue(Id, out data))
{
data.CustomData = ConfigurationManager.ApplicationPaths.ItemsByNamePath;
}
return result;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the location.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>System.String.</returns>
protected string GetLocation(BaseItem item)
{
2013-04-23 19:17:21 +00:00
var name = FileSystem.GetValidFilename(item.Name);
2013-02-21 01:33:05 +00:00
2013-03-04 05:43:06 +00:00
return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name);
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>
/// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
protected override WIN32_FIND_DATA? GetImage(BaseItem item, string filenameWithoutExtension)
{
var location = GetLocation(item);
var result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".png"));
if (!result.HasValue)
result = FileSystem.GetFileData(Path.Combine(location, filenameWithoutExtension + ".jpg"));
return result;
}
}
}