2012-07-12 06:55:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Events;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets initial values on the newly resolved item
|
|
|
|
|
/// </summary>
|
2012-07-12 06:55:27 +00:00
|
|
|
|
protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
// If the subclass didn't specify this
|
|
|
|
|
if (string.IsNullOrEmpty(item.Path))
|
|
|
|
|
{
|
|
|
|
|
item.Path = args.Path;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
// If the subclass didn't specify this
|
2012-07-13 03:50:50 +00:00
|
|
|
|
if (args.Parent != null)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-07-13 03:50:50 +00:00
|
|
|
|
item.Parent = args.Parent;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.Id = Kernel.GetMD5(item.Path);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async Task<BaseItem> ResolvePath(ItemResolveEventArgs args)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
T item = Resolve(args);
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
// Set initial values on the newly resolved item
|
2012-07-12 06:55:27 +00:00
|
|
|
|
SetItemValues(item, args);
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
// Make sure the item has a name
|
2012-07-12 06:55:27 +00:00
|
|
|
|
EnsureName(item);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
|
|
|
|
|
// Make sure DateCreated and DateModified have values
|
2012-07-12 06:55:27 +00:00
|
|
|
|
EnsureDates(item);
|
2012-08-19 15:58:35 +00:00
|
|
|
|
|
|
|
|
|
await FetchMetadataFromProviders(item, args);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
private async Task FetchMetadataFromProviders(T item, ItemResolveEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
foreach (BaseMetadataProvider provider in Kernel.Instance.MetadataProviders)
|
|
|
|
|
{
|
|
|
|
|
if (provider.Supports(item))
|
|
|
|
|
{
|
|
|
|
|
await provider.Fetch(item, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures DateCreated and DateModified have values
|
|
|
|
|
/// </summary>
|
2012-07-12 06:55:27 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Weed this to keep a list of resolvers, since Resolvers are built with generics
|
|
|
|
|
/// </summary>
|
2012-07-12 06:55:27 +00:00
|
|
|
|
public interface IBaseItemResolver
|
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
Task<BaseItem> ResolvePath(ItemResolveEventArgs args);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|