using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
using System;
using System.IO;
using System.Linq;
namespace MediaBrowser.Server.Implementations.Library.Resolvers
{
///
/// Class FolderResolver
///
public class FolderResolver : FolderResolver
{
///
/// Gets the priority.
///
/// The priority.
public override ResolverPriority Priority
{
get { return ResolverPriority.Last; }
}
///
/// Resolves the specified args.
///
/// The args.
/// Folder.
protected override Folder Resolve(ItemResolveArgs args)
{
if (args.IsDirectory)
{
if (args.IsPhysicalRoot)
{
return new AggregateFolder();
}
if (args.IsRoot)
{
return new UserRootFolder(); //if we got here and still a root - must be user root
}
if (args.IsVf)
{
return new CollectionFolder
{
CollectionType = GetCollectionType(args)
};
}
return new Folder();
}
return null;
}
private string GetCollectionType(ItemResolveArgs args)
{
return args.FileSystemChildren
.Where(i =>
{
try
{
return (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory &&
string.Equals(".collection", i.Extension, StringComparison.OrdinalIgnoreCase);
}
catch (IOException)
{
return false;
}
})
.Select(i => Path.GetFileNameWithoutExtension(i.FullName))
.FirstOrDefault();
}
}
///
/// Class FolderResolver
///
/// The type of the T item type.
public abstract class FolderResolver : ItemResolver
where TItemType : Folder, new()
{
///
/// Sets the initial item values.
///
/// The item.
/// The args.
protected override void SetInitialItemValues(TItemType item, ItemResolveArgs args)
{
base.SetInitialItemValues(item, args);
item.IsRoot = args.Parent == null;
item.IsPhysicalRoot = args.IsPhysicalRoot;
}
}
}