You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs

106 lines
3.2 KiB

12 years ago
using System;
using System.IO;
using System.Threading.Tasks;
12 years ago
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.Providers;
12 years ago
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Resolvers
{
public abstract class BaseItemResolver<T> : IBaseItemResolver
where T : BaseItem, new ()
{
protected virtual T Resolve(ItemResolveEventArgs args)
{
return null;
}
/// <summary>
/// Sets initial values on the newly resolved item
/// </summary>
12 years ago
protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
{
// If the subclass didn't specify this
if (string.IsNullOrEmpty(item.Path))
{
item.Path = args.Path;
}
// If the subclass didn't specify this
if (args.Parent != null)
12 years ago
{
item.Parent = args.Parent;
12 years ago
}
item.Id = Kernel.GetMD5(item.Path);
}
public async Task<BaseItem> ResolvePath(ItemResolveEventArgs args)
12 years ago
{
T item = Resolve(args);
if (item != null)
{
// Set initial values on the newly resolved item
12 years ago
SetItemValues(item, args);
// Make sure the item has a name
12 years ago
EnsureName(item);
// Make sure DateCreated and DateModified have values
12 years ago
EnsureDates(item);
await FetchMetadataFromProviders(item, args);
12 years ago
}
return item;
}
private async Task FetchMetadataFromProviders(T item, ItemResolveEventArgs args)
{
foreach (BaseMetadataProvider provider in Kernel.Instance.MetadataProviders)
{
if (provider.Supports(item))
{
await provider.Fetch(item, args);
}
}
}
12 years ago
private void EnsureName(T item)
{
// If the subclass didn't supply a name, add it here
if (string.IsNullOrEmpty(item.Name))
{
item.Name = Path.GetFileNameWithoutExtension(item.Path);
}
}
/// <summary>
/// Ensures DateCreated and DateModified have values
/// </summary>
12 years ago
private void EnsureDates(T item)
{
// If the subclass didn't supply dates, add them here
if (item.DateCreated == DateTime.MinValue)
{
item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
}
if (item.DateModified == DateTime.MinValue)
{
item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
}
}
}
/// <summary>
/// Weed this to keep a list of resolvers, since Resolvers are built with generics
/// </summary>
12 years ago
public interface IBaseItemResolver
{
Task<BaseItem> ResolvePath(ItemResolveEventArgs args);
12 years ago
}
}