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

126 lines
3.6 KiB

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using System;
using System.IO;
13 years ago
namespace MediaBrowser.Controller.Resolvers
{
public abstract class BaseItemResolver<T> : IBaseItemResolver
where T : BaseItem, new()
13 years ago
{
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)
13 years ago
{
// 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)
13 years ago
{
item.Parent = args.Parent;
13 years ago
}
item.Id = Kernel.GetMD5(item.Path);
}
public BaseItem ResolvePath(ItemResolveEventArgs args)
13 years ago
{
T item = Resolve(args);
13 years ago
if (item != null)
{
// Set initial values on the newly resolved item
SetInitialItemValues(item, args);
13 years ago
// Make sure the item has a name
13 years ago
EnsureName(item);
// Make sure DateCreated and DateModified have values
EnsureDates(item, args);
13 years ago
}
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)
13 years ago
{
if (!Path.IsPathRooted(item.Path))
13 years ago
{
return;
13 years ago
}
// See if a different path came out of the resolver than what went in
if (!args.Path.Equals(item.Path, StringComparison.OrdinalIgnoreCase))
13 years ago
{
WIN32_FIND_DATA? childData = args.GetFileSystemEntry(item.Path);
if (childData != null)
{
item.DateCreated = childData.Value.CreationTimeUtc;
item.DateModified = childData.Value.LastWriteTimeUtc;
}
else
{
WIN32_FIND_DATA fileData = FileData.GetFileData(item.Path);
item.DateCreated = fileData.CreationTimeUtc;
item.DateModified = fileData.LastWriteTimeUtc;
}
13 years ago
}
else
{
item.DateCreated = args.FileInfo.CreationTimeUtc;
item.DateModified = args.FileInfo.LastWriteTimeUtc;
}
13 years ago
}
}
/// <summary>
/// Weed this to keep a list of resolvers, since Resolvers are built with generics
/// </summary>
13 years ago
public interface IBaseItemResolver
{
BaseItem ResolvePath(ItemResolveEventArgs args);
ResolverPriority Priority { get; }
}
public enum ResolverPriority
{
First = 1,
Second = 2,
Third = 3,
Last = 4
13 years ago
}
}