2012-07-12 06:55:27 +00:00
|
|
|
|
using System;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
|
|
|
{
|
|
|
|
|
public class ItemController
|
|
|
|
|
{
|
|
|
|
|
#region PreBeginResolvePath Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires when a path is about to be resolved, but before child folders and files
|
|
|
|
|
/// have been collected from the file system.
|
|
|
|
|
/// This gives listeners a chance to cancel the operation and cause the path to be ignored.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<PreBeginResolveEventArgs> PreBeginResolvePath;
|
|
|
|
|
private bool OnPreBeginResolvePath(Folder parent, string path, FileAttributes attributes)
|
|
|
|
|
{
|
|
|
|
|
PreBeginResolveEventArgs args = new PreBeginResolveEventArgs()
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
Parent = parent,
|
|
|
|
|
FileAttributes = attributes,
|
|
|
|
|
Cancel = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (PreBeginResolvePath != null)
|
|
|
|
|
{
|
|
|
|
|
PreBeginResolvePath(this, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !args.Cancel;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region BeginResolvePath Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires when a path is about to be resolved, but after child folders and files
|
|
|
|
|
/// have been collected from the file system.
|
|
|
|
|
/// This gives listeners a chance to cancel the operation and cause the path to be ignored.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<ItemResolveEventArgs> BeginResolvePath;
|
|
|
|
|
private bool OnBeginResolvePath(ItemResolveEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (BeginResolvePath != null)
|
|
|
|
|
{
|
|
|
|
|
BeginResolvePath(this, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !args.Cancel;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
private async Task<BaseItem> ResolveItem(ItemResolveEventArgs args)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-20 15:55:05 +00:00
|
|
|
|
// Try first priority resolvers
|
|
|
|
|
foreach (IBaseItemResolver resolver in Kernel.Instance.EntityResolvers.Where(p => p.Priority == ResolverPriority.First))
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
var item = await resolver.ResolvePath(args);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 15:55:05 +00:00
|
|
|
|
// Try second priority resolvers
|
|
|
|
|
foreach (IBaseItemResolver resolver in Kernel.Instance.EntityResolvers.Where(p => p.Priority == ResolverPriority.Second))
|
|
|
|
|
{
|
|
|
|
|
var item = await resolver.ResolvePath(args);
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try third priority resolvers
|
|
|
|
|
foreach (IBaseItemResolver resolver in Kernel.Instance.EntityResolvers.Where(p => p.Priority == ResolverPriority.Third))
|
|
|
|
|
{
|
|
|
|
|
var item = await resolver.ResolvePath(args);
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try last priority resolvers
|
|
|
|
|
foreach (IBaseItemResolver resolver in Kernel.Instance.EntityResolvers.Where(p => p.Priority == ResolverPriority.Last))
|
|
|
|
|
{
|
|
|
|
|
var item = await resolver.ResolvePath(args);
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
{
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves a path into a BaseItem
|
|
|
|
|
/// </summary>
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async Task<BaseItem> GetItem(Folder parent, string path)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
return await GetItemInternal(parent, path, File.GetAttributes(path));
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves a path into a BaseItem
|
|
|
|
|
/// </summary>
|
2012-08-19 15:58:35 +00:00
|
|
|
|
private async Task<BaseItem> GetItemInternal(Folder parent, string path, FileAttributes attributes)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (!OnPreBeginResolvePath(parent, path, attributes))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren;
|
|
|
|
|
|
|
|
|
|
// Gather child folder and files
|
|
|
|
|
if (attributes.HasFlag(FileAttributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
fileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair<string, FileAttributes>(f, File.GetAttributes(f)));
|
|
|
|
|
|
|
|
|
|
bool isVirtualFolder = parent != null && parent.IsRoot;
|
|
|
|
|
fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fileSystemChildren = new KeyValuePair<string, FileAttributes>[] { };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ItemResolveEventArgs args = new ItemResolveEventArgs()
|
|
|
|
|
{
|
|
|
|
|
Path = path,
|
|
|
|
|
FileAttributes = attributes,
|
|
|
|
|
FileSystemChildren = fileSystemChildren,
|
|
|
|
|
Parent = parent,
|
|
|
|
|
Cancel = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Fire BeginResolvePath to see if anyone wants to cancel this operation
|
|
|
|
|
if (!OnBeginResolvePath(args))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
BaseItem item = await ResolveItem(args);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
var folder = item as Folder;
|
|
|
|
|
|
|
|
|
|
if (folder != null)
|
|
|
|
|
{
|
|
|
|
|
// If it's a folder look for child entities
|
2012-08-19 15:58:35 +00:00
|
|
|
|
await AttachChildren(folder, fileSystemChildren);
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds child BaseItems for a given Folder
|
|
|
|
|
/// </summary>
|
2012-08-19 15:58:35 +00:00
|
|
|
|
private async Task AttachChildren(Folder folder, IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
KeyValuePair<string, FileAttributes>[] fileSystemChildrenArray = fileSystemChildren.ToArray();
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
int count = fileSystemChildrenArray.Length;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
Task<BaseItem>[] tasks = new Task<BaseItem>[count];
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
var child = fileSystemChildrenArray[i];
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
tasks[i] = GetItemInternal(folder, child.Key, child.Value);
|
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
BaseItem[] baseItemChildren = await Task<BaseItem>.WhenAll(tasks);
|
2012-08-20 15:55:05 +00:00
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
// Sort them
|
2012-08-19 15:58:35 +00:00
|
|
|
|
folder.Children = baseItemChildren.Where(i => i != null).OrderBy(f =>
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
|
|
|
|
|
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Transforms shortcuts into their actual paths
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<KeyValuePair<string, FileAttributes>> FilterChildFileSystemEntries(IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren, bool flattenShortcuts)
|
|
|
|
|
{
|
|
|
|
|
List<KeyValuePair<string, FileAttributes>> returnFiles = new List<KeyValuePair<string, FileAttributes>>();
|
|
|
|
|
|
|
|
|
|
// Loop through each file
|
|
|
|
|
foreach (KeyValuePair<string, FileAttributes> file in fileSystemChildren)
|
|
|
|
|
{
|
|
|
|
|
// Folders
|
|
|
|
|
if (file.Value.HasFlag(FileAttributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
returnFiles.Add(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's a shortcut, resolve it
|
|
|
|
|
else if (Shortcut.IsShortcut(file.Key))
|
|
|
|
|
{
|
|
|
|
|
string newPath = Shortcut.ResolveShortcut(file.Key);
|
|
|
|
|
FileAttributes newPathAttributes = File.GetAttributes(newPath);
|
|
|
|
|
|
|
|
|
|
// Find out if the shortcut is pointing to a directory or file
|
|
|
|
|
|
|
|
|
|
if (newPathAttributes.HasFlag(FileAttributes.Directory))
|
|
|
|
|
{
|
|
|
|
|
// If we're flattening then get the shortcut's children
|
|
|
|
|
|
|
|
|
|
if (flattenShortcuts)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<KeyValuePair<string, FileAttributes>> newChildren = Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair<string, FileAttributes>(f, File.GetAttributes(f)));
|
|
|
|
|
|
|
|
|
|
returnFiles.AddRange(FilterChildFileSystemEntries(newChildren, false));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
returnFiles.Add(new KeyValuePair<string, FileAttributes>(newPath, newPathAttributes));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
returnFiles.Add(new KeyValuePair<string, FileAttributes>(newPath, newPathAttributes));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
returnFiles.Add(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnFiles;
|
|
|
|
|
}
|
2012-07-16 16:50:44 +00:00
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Person
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public async Task<Person> GetPerson(string name)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
string path = Path.Combine(Kernel.Instance.ApplicationPaths.PeoplePath, name);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return await GetImagesByNameItem<Person>(path, name);
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Studio
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public async Task<Studio> GetStudio(string name)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
string path = Path.Combine(Kernel.Instance.ApplicationPaths.StudioPath, name);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return await GetImagesByNameItem<Studio>(path, name);
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Genre
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public async Task<Genre> GetGenre(string name)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
string path = Path.Combine(Kernel.Instance.ApplicationPaths.GenrePath, name);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return await GetImagesByNameItem<Genre>(path, name);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Year
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
public async Task<Year> GetYear(int value)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
string path = Path.Combine(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
return await GetImagesByNameItem<Year>(path, value.ToString());
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>();
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generically retrieves an IBN item
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task<T> GetImagesByNameItem<T>(string path, string name)
|
2012-08-14 16:06:46 +00:00
|
|
|
|
where T : BaseEntity, new()
|
|
|
|
|
{
|
|
|
|
|
string key = path.ToLower();
|
|
|
|
|
|
|
|
|
|
// Look for it in the cache, if it's not there, create it
|
|
|
|
|
if (!ImagesByNameItemCache.ContainsKey(key))
|
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
T obj = await CreateImagesByNameItem<T>(path, name);
|
|
|
|
|
ImagesByNameItemCache[key] = obj;
|
|
|
|
|
return obj;
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ImagesByNameItemCache[key] as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an IBN item based on a given path
|
|
|
|
|
/// </summary>
|
2012-08-19 20:38:31 +00:00
|
|
|
|
private async Task<T> CreateImagesByNameItem<T>(string path, string name)
|
2012-08-19 15:58:35 +00:00
|
|
|
|
where T : BaseEntity, new()
|
2012-08-14 16:06:46 +00:00
|
|
|
|
{
|
|
|
|
|
T item = new T();
|
|
|
|
|
|
|
|
|
|
item.Name = name;
|
|
|
|
|
item.Id = Kernel.GetMD5(path);
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
if (!Directory.Exists(path))
|
2012-08-14 16:06:46 +00:00
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
Directory.CreateDirectory(path);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
|
|
|
|
item.DateCreated = Directory.GetCreationTime(path);
|
|
|
|
|
item.DateModified = Directory.GetLastAccessTime(path);
|
|
|
|
|
|
2012-08-20 12:59:21 +00:00
|
|
|
|
ItemResolveEventArgs args = new ItemResolveEventArgs();
|
|
|
|
|
args.Path = path;
|
|
|
|
|
args.FileAttributes = File.GetAttributes(path);
|
|
|
|
|
args.FileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair<string, FileAttributes>(f, File.GetAttributes(f)));
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 12:59:21 +00:00
|
|
|
|
await Kernel.Instance.ExecuteMetadataProviders(item, args);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
|
|
|
|
return item;
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|