jellyfin-server/MediaBrowser.Controller/Library/ItemController.cs

327 lines
12 KiB
C#
Raw Normal View History

2012-07-12 06:55:27 +00:00
using System;
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.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, WIN32_FIND_DATA fileData)
2012-07-12 06:55:27 +00:00
{
PreBeginResolveEventArgs args = new PreBeginResolveEventArgs()
{
Path = path,
Parent = parent,
FileData = fileData,
2012-07-12 06:55:27 +00:00
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-20 21:48:11 +00:00
private BaseItem ResolveItem(ItemResolveEventArgs args)
2012-07-12 06:55:27 +00:00
{
// Try first priority resolvers
2012-08-21 01:21:03 +00:00
for (int i = 0; i < Kernel.Instance.EntityResolvers.Length; i++)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
var item = Kernel.Instance.EntityResolvers[i].ResolvePath(args);
if (item != null)
{
return item;
}
}
2012-08-20 21:48:11 +00:00
2012-07-12 06:55:27 +00:00
return null;
}
/// <summary>
/// Resolves a path into a BaseItem
/// </summary>
2012-08-21 01:21:03 +00:00
public async Task<BaseItem> GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
WIN32_FIND_DATA fileData = fileInfo ?? FileData.GetFileData(path);
if (!OnPreBeginResolvePath(parent, path, fileData))
2012-07-12 06:55:27 +00:00
{
return null;
}
2012-08-21 01:21:03 +00:00
KeyValuePair<string, WIN32_FIND_DATA>[] fileSystemChildren;
2012-07-12 06:55:27 +00:00
// Gather child folder and files
if (fileData.IsDirectory)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
fileSystemChildren = ConvertFileSystemEntries(Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly));
2012-07-12 06:55:27 +00:00
bool isVirtualFolder = parent != null && parent.IsRoot;
fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder);
}
else
{
fileSystemChildren = new KeyValuePair<string, WIN32_FIND_DATA>[] { };
2012-07-12 06:55:27 +00:00
}
ItemResolveEventArgs args = new ItemResolveEventArgs()
{
Path = path,
FileSystemChildren = fileSystemChildren,
FileData = fileData,
2012-07-12 06:55:27 +00:00
Parent = parent,
Cancel = false
};
// Fire BeginResolvePath to see if anyone wants to cancel this operation
if (!OnBeginResolvePath(args))
{
return null;
}
2012-08-20 21:48:11 +00:00
BaseItem item = ResolveItem(args);
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
if (item != null)
2012-07-12 06:55:27 +00:00
{
2012-08-20 21:48:11 +00:00
await Kernel.Instance.ExecuteMetadataProviders(item, args);
var folder = item as Folder;
if (folder != null)
{
// If it's a folder look for child entities
await AttachChildren(folder, fileSystemChildren).ConfigureAwait(false);
}
2012-07-12 06:55:27 +00:00
}
return item;
}
/// <summary>
/// Finds child BaseItems for a given Folder
/// </summary>
2012-08-21 01:21:03 +00:00
private async Task AttachChildren(Folder folder, KeyValuePair<string, WIN32_FIND_DATA>[] fileSystemChildren)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
int count = fileSystemChildren.Length;
2012-07-12 06:55:27 +00:00
Task<BaseItem>[] tasks = new Task<BaseItem>[count];
2012-07-12 06:55:27 +00:00
for (int i = 0; i < count; i++)
{
2012-08-21 01:21:03 +00:00
var child = fileSystemChildren[i];
2012-07-12 06:55:27 +00:00
2012-08-21 01:21:03 +00:00
tasks[i] = GetItem(child.Key, folder, child.Value);
}
2012-07-12 06:55:27 +00:00
2012-08-20 21:48:11 +00:00
BaseItem[] baseItemChildren = await Task<BaseItem>.WhenAll(tasks).ConfigureAwait(false);
2012-07-12 06:55:27 +00:00
// Sort them
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;
2012-08-21 01:21:03 +00:00
});
2012-07-12 06:55:27 +00:00
}
/// <summary>
/// Transforms shortcuts into their actual paths
/// </summary>
2012-08-21 01:21:03 +00:00
private KeyValuePair<string, WIN32_FIND_DATA>[] FilterChildFileSystemEntries(KeyValuePair<string, WIN32_FIND_DATA>[] fileSystemChildren, bool flattenShortcuts)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
KeyValuePair<string, WIN32_FIND_DATA>[] returnArray = new KeyValuePair<string, WIN32_FIND_DATA>[fileSystemChildren.Length];
List<KeyValuePair<string, WIN32_FIND_DATA>> resolvedShortcuts = new List<KeyValuePair<string, WIN32_FIND_DATA>>();
2012-07-12 06:55:27 +00:00
2012-08-21 01:21:03 +00:00
for (int i = 0; i < fileSystemChildren.Length; i++)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
KeyValuePair<string, WIN32_FIND_DATA> file = fileSystemChildren[i];
if (file.Value.IsDirectory)
2012-07-12 06:55:27 +00:00
{
2012-08-21 01:21:03 +00:00
returnArray[i] = file;
2012-07-12 06:55:27 +00:00
}
// If it's a shortcut, resolve it
else if (Shortcut.IsShortcut(file.Key))
{
string newPath = Shortcut.ResolveShortcut(file.Key);
WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
2012-07-12 06:55:27 +00:00
// Find out if the shortcut is pointing to a directory or file
if (newPathData.IsDirectory)
2012-07-12 06:55:27 +00:00
{
// If we're flattening then get the shortcut's children
if (flattenShortcuts)
{
2012-08-21 01:21:03 +00:00
returnArray[i] = file;
KeyValuePair<string, WIN32_FIND_DATA>[] newChildren = ConvertFileSystemEntries(Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly));
2012-07-12 06:55:27 +00:00
2012-08-21 01:21:03 +00:00
resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false));
2012-07-12 06:55:27 +00:00
}
else
{
2012-08-21 01:21:03 +00:00
returnArray[i] = new KeyValuePair<string, WIN32_FIND_DATA>(newPath, newPathData);
2012-07-12 06:55:27 +00:00
}
}
else
{
2012-08-21 01:21:03 +00:00
returnArray[i] = new KeyValuePair<string, WIN32_FIND_DATA>(newPath, newPathData);
2012-07-12 06:55:27 +00:00
}
}
else
{
2012-08-21 01:21:03 +00:00
returnArray[i] = file;
2012-07-12 06:55:27 +00:00
}
}
2012-08-21 01:21:03 +00:00
if (resolvedShortcuts.Count > 0)
{
resolvedShortcuts.InsertRange(0, returnArray);
return resolvedShortcuts.ToArray();
}
else
{
return returnArray;
}
2012-07-12 06:55:27 +00:00
}
/// <summary>
/// Gets a Person
/// </summary>
public async Task<Person> GetPerson(string name)
{
string path = Path.Combine(Kernel.Instance.ApplicationPaths.PeoplePath, name);
2012-08-20 21:48:11 +00:00
return await GetImagesByNameItem<Person>(path, name).ConfigureAwait(false);
}
/// <summary>
/// Gets a Studio
/// </summary>
public async Task<Studio> GetStudio(string name)
{
string path = Path.Combine(Kernel.Instance.ApplicationPaths.StudioPath, name);
2012-08-20 21:48:11 +00:00
return await GetImagesByNameItem<Studio>(path, name).ConfigureAwait(false);
}
/// <summary>
/// Gets a Genre
/// </summary>
public async Task<Genre> GetGenre(string name)
{
string path = Path.Combine(Kernel.Instance.ApplicationPaths.GenrePath, name);
2012-08-20 21:48:11 +00:00
return await GetImagesByNameItem<Genre>(path, name).ConfigureAwait(false);
}
/// <summary>
/// Gets a Year
/// </summary>
public async Task<Year> GetYear(int value)
{
string path = Path.Combine(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
2012-08-20 21:48:11 +00:00
return await GetImagesByNameItem<Year>(path, value.ToString()).ConfigureAwait(false);
}
private ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>();
/// <summary>
/// Generically retrieves an IBN item
/// </summary>
private async Task<T> GetImagesByNameItem<T>(string path, string name)
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-20 21:48:11 +00:00
T obj = await CreateImagesByNameItem<T>(path, name).ConfigureAwait(false);
ImagesByNameItemCache[key] = obj;
return obj;
}
return ImagesByNameItemCache[key] as T;
}
/// <summary>
/// Creates an IBN item based on a given path
/// </summary>
private async Task<T> CreateImagesByNameItem<T>(string path, string name)
where T : BaseEntity, new()
{
T item = new T();
item.Name = name;
item.Id = Kernel.GetMD5(path);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
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.FileData = FileData.GetFileData(path);
2012-08-21 01:21:03 +00:00
args.FileSystemChildren = ConvertFileSystemEntries(Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly));
2012-08-20 21:48:11 +00:00
await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
return item;
}
2012-08-21 01:21:03 +00:00
private KeyValuePair<string, WIN32_FIND_DATA>[] ConvertFileSystemEntries(string[] files)
{
KeyValuePair<string, WIN32_FIND_DATA>[] items = new KeyValuePair<string, WIN32_FIND_DATA>[files.Length];
for (int i = 0; i < files.Length; i++)
{
string file = files[i];
items[i] = new KeyValuePair<string, WIN32_FIND_DATA>(file, FileData.GetFileData(file));
}
return items;
}
2012-07-12 06:55:27 +00:00
}
}