jellyfin/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs

126 lines
3.6 KiB
C#
Raw Normal View History

2012-07-12 06:55:27 +00:00
using System;
using System.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
2012-07-12 06:55:27 +00:00
namespace MediaBrowser.Controller.Resolvers
{
public abstract class BaseItemResolver<T> : IBaseItemResolver
where T : BaseItem, new()
2012-07-12 06:55:27 +00:00
{
protected virtual T Resolve(ItemResolveEventArgs args)
{
return null;
}
public virtual ResolverPriority Priority
{
get
{
return ResolverPriority.First;
}
}
/// <summary>
/// Sets initial values on the newly resolved item
/// </summary>
protected virtual void SetInitialItemValues(T item, ItemResolveEventArgs args)
2012-07-12 06:55:27 +00:00
{
// 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)
2012-07-12 06:55:27 +00:00
{
item.Parent = args.Parent;
2012-07-12 06:55:27 +00:00
}
item.Id = Kernel.GetMD5(item.Path);
}
2012-08-20 21:48:11 +00:00
public BaseItem ResolvePath(ItemResolveEventArgs args)
2012-07-12 06:55:27 +00:00
{
T item = Resolve(args);
2012-07-12 06:55:27 +00:00
if (item != null)
{
// Set initial values on the newly resolved item
SetInitialItemValues(item, args);
2012-07-12 06:55:27 +00:00
// Make sure the item has a name
2012-07-12 06:55:27 +00:00
EnsureName(item);
// Make sure DateCreated and DateModified have values
EnsureDates(item, args);
2012-07-12 06:55:27 +00:00
}
return item;
}
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>
private void EnsureDates(T item, ItemResolveEventArgs args)
2012-07-12 06:55:27 +00:00
{
if (!Path.IsPathRooted(item.Path))
2012-07-12 06:55:27 +00:00
{
return;
2012-07-12 06:55:27 +00:00
}
// See if a different path came out of the resolver than what went in
if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
2012-07-12 06:55:27 +00:00
{
WIN32_FIND_DATA? childData = args.GetFileSystemEntry(item.Path);
if (childData != null)
{
2012-09-04 19:23:15 +00:00
item.DateCreated = childData.Value.CreationTimeUtc;
item.DateModified = childData.Value.LastWriteTimeUtc;
}
else
{
WIN32_FIND_DATA fileData = FileData.GetFileData(item.Path);
2012-09-04 19:23:15 +00:00
item.DateCreated = fileData.CreationTimeUtc;
item.DateModified = fileData.LastWriteTimeUtc;
}
2012-07-12 06:55:27 +00:00
}
2012-08-21 14:42:40 +00:00
else
{
2012-09-04 19:23:15 +00:00
item.DateCreated = args.FileInfo.CreationTimeUtc;
item.DateModified = args.FileInfo.LastWriteTimeUtc;
2012-08-21 14:42:40 +00:00
}
2012-07-12 06:55:27 +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-20 21:48:11 +00:00
BaseItem ResolvePath(ItemResolveEventArgs args);
ResolverPriority Priority { get; }
}
public enum ResolverPriority
{
2012-08-21 01:21:03 +00:00
First = 1,
Second = 2,
Third = 3,
Last = 4
2012-07-12 06:55:27 +00:00
}
}