jellyfin-server/MediaBrowser.Server.Implementations/Library/Resolvers/FolderResolver.cs

106 lines
3.1 KiB
C#
Raw Normal View History

2014-07-26 17:30:15 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Entities;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Resolvers;
2013-07-12 19:56:40 +00:00
using System;
using System.IO;
using System.Linq;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Server.Implementations.Library.Resolvers
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class FolderResolver
/// </summary>
public class FolderResolver : FolderResolver<Folder>
2013-02-21 01:33:05 +00:00
{
2014-07-26 17:30:15 +00:00
private readonly IFileSystem _fileSystem;
public FolderResolver(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override ResolverPriority Priority
{
get { return ResolverPriority.Last; }
}
/// <summary>
/// Resolves the specified args.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>Folder.</returns>
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)
{
2013-07-12 19:56:40 +00:00
return new CollectionFolder
{
CollectionType = GetCollectionType(args)
};
2013-02-21 01:33:05 +00:00
}
return new Folder();
}
return null;
}
2013-07-12 19:56:40 +00:00
private string GetCollectionType(ItemResolveArgs args)
{
return args.FileSystemChildren
2013-07-13 22:47:54 +00:00
.Where(i =>
{
try
{
return (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory &&
string.Equals(".collection", i.Extension, StringComparison.OrdinalIgnoreCase);
}
catch (IOException)
{
return false;
}
})
2014-07-26 17:30:15 +00:00
.Select(i => _fileSystem.GetFileNameWithoutExtension(i))
2013-07-12 19:56:40 +00:00
.FirstOrDefault();
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Class FolderResolver
2013-02-21 01:33:05 +00:00
/// </summary>
/// <typeparam name="TItemType">The type of the T item type.</typeparam>
public abstract class FolderResolver<TItemType> : ItemResolver<TItemType>
2013-02-21 01:33:05 +00:00
where TItemType : Folder, new()
{
/// <summary>
/// Sets the initial item values.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
protected override void SetInitialItemValues(TItemType item, ItemResolveArgs args)
{
base.SetInitialItemValues(item, args);
item.IsRoot = args.Parent == null;
item.IsPhysicalRoot = args.IsPhysicalRoot;
}
}
}