2012-09-11 01:34:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
2012-09-07 19:22:37 +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;
|
|
|
|
|
|
|
|
|
|
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;
|
2012-08-23 05:45:26 +00:00
|
|
|
|
private bool OnPreBeginResolvePath(PreBeginResolveEventArgs args)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2012-08-20 15:55:05 +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);
|
2012-08-20 15:55:05 +00:00
|
|
|
|
|
|
|
|
|
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-23 12:26:08 +00:00
|
|
|
|
public async Task<BaseItem> GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null, bool allowInternetProviders = true)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-09-11 18:20:12 +00:00
|
|
|
|
var args = new ItemResolveEventArgs
|
2012-08-23 05:45:26 +00:00
|
|
|
|
{
|
|
|
|
|
FileInfo = fileInfo ?? FileData.GetFileData(path),
|
|
|
|
|
Parent = parent,
|
|
|
|
|
Cancel = false,
|
|
|
|
|
Path = path
|
|
|
|
|
};
|
2012-08-20 23:53:32 +00:00
|
|
|
|
|
2012-08-23 05:45:26 +00:00
|
|
|
|
if (!OnPreBeginResolvePath(args))
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 05:45:26 +00:00
|
|
|
|
WIN32_FIND_DATA[] fileSystemChildren;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
// Gather child folder and files
|
2012-08-23 05:45:26 +00:00
|
|
|
|
if (args.IsDirectory)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-23 05:45:26 +00:00
|
|
|
|
fileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
bool isVirtualFolder = parent != null && parent.IsRoot;
|
|
|
|
|
fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-23 05:45:26 +00:00
|
|
|
|
fileSystemChildren = new WIN32_FIND_DATA[] { };
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-23 05:45:26 +00:00
|
|
|
|
args.FileSystemChildren = fileSystemChildren;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
|
|
|
|
// 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-23 12:26:08 +00:00
|
|
|
|
await Kernel.Instance.ExecuteMetadataProviders(item, args, allowInternetProviders: allowInternetProviders).ConfigureAwait(false);
|
2012-08-20 21:48:11 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
if (item.IsFolder)
|
2012-08-20 21:48:11 +00:00
|
|
|
|
{
|
|
|
|
|
// If it's a folder look for child entities
|
2012-09-11 18:20:12 +00:00
|
|
|
|
(item as Folder).Children = (await Task.WhenAll(GetChildren(item as Folder, fileSystemChildren, allowInternetProviders)).ConfigureAwait(false))
|
2012-08-22 02:50:59 +00:00
|
|
|
|
.Where(i => i != null).OrderBy(f =>
|
|
|
|
|
{
|
|
|
|
|
return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
|
|
|
|
|
|
|
|
|
|
});
|
2012-08-20 21:48:11 +00:00
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds child BaseItems for a given Folder
|
|
|
|
|
/// </summary>
|
2012-08-23 12:26:08 +00:00
|
|
|
|
private Task<BaseItem>[] GetChildren(Folder folder, WIN32_FIND_DATA[] fileSystemChildren, bool allowInternetProviders)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
for (int i = 0; i < fileSystemChildren.Length; i++)
|
2012-08-19 15:58:35 +00:00
|
|
|
|
{
|
2012-08-21 01:21:03 +00:00
|
|
|
|
var child = fileSystemChildren[i];
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-23 12:26:08 +00:00
|
|
|
|
tasks[i] = GetItem(child.Path, folder, child, allowInternetProviders: allowInternetProviders);
|
2012-08-19 15:58:35 +00:00
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return tasks;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Transforms shortcuts into their actual paths
|
|
|
|
|
/// </summary>
|
2012-08-23 05:45:26 +00:00
|
|
|
|
private WIN32_FIND_DATA[] FilterChildFileSystemEntries(WIN32_FIND_DATA[] fileSystemChildren, bool flattenShortcuts)
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-23 05:45:26 +00:00
|
|
|
|
WIN32_FIND_DATA[] returnArray = new WIN32_FIND_DATA[fileSystemChildren.Length];
|
|
|
|
|
List<WIN32_FIND_DATA> resolvedShortcuts = new List<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-23 05:45:26 +00:00
|
|
|
|
WIN32_FIND_DATA file = fileSystemChildren[i];
|
2012-08-21 01:21:03 +00:00
|
|
|
|
|
2012-07-12 06:55:27 +00:00
|
|
|
|
// If it's a shortcut, resolve it
|
2012-08-21 03:56:28 +00:00
|
|
|
|
if (Shortcut.IsShortcut(file.Path))
|
2012-07-12 06:55:27 +00:00
|
|
|
|
{
|
2012-08-21 03:56:28 +00:00
|
|
|
|
string newPath = Shortcut.ResolveShortcut(file.Path);
|
2012-08-20 23:53:32 +00:00
|
|
|
|
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
|
2012-08-20 23:53:32 +00:00
|
|
|
|
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;
|
2012-08-23 05:45:26 +00:00
|
|
|
|
WIN32_FIND_DATA[] newChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray();
|
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-23 05:45:26 +00:00
|
|
|
|
returnArray[i] = newPathData;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-23 05:45:26 +00:00
|
|
|
|
returnArray[i] = 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();
|
|
|
|
|
}
|
2012-09-11 18:20:12 +00:00
|
|
|
|
|
|
|
|
|
return returnArray;
|
2012-07-12 06:55:27 +00:00
|
|
|
|
}
|
2012-07-16 16:50:44 +00:00
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Person
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
public Task<Person> GetPerson(string name)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-28 12:39:23 +00:00
|
|
|
|
return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Studio
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
public Task<Studio> GetStudio(string name)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-28 12:39:23 +00:00
|
|
|
|
return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Genre
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
public Task<Genre> GetGenre(string name)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
{
|
2012-08-28 12:39:23 +00:00
|
|
|
|
return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
|
2012-07-20 02:22:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 16:06:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Year
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
public Task<Year> GetYear(int value)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-28 12:39:23 +00:00
|
|
|
|
return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generically retrieves an IBN item
|
|
|
|
|
/// </summary>
|
2012-08-22 02:50:59 +00:00
|
|
|
|
private Task<T> GetImagesByNameItem<T>(string path, string name)
|
2012-08-14 16:06:46 +00:00
|
|
|
|
where T : BaseEntity, new()
|
|
|
|
|
{
|
2012-08-28 12:39:23 +00:00
|
|
|
|
name = FileData.GetValidFilename(name);
|
|
|
|
|
|
|
|
|
|
string key = Path.Combine(path, name);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
|
|
|
|
|
// Look for it in the cache, if it's not there, create it
|
|
|
|
|
if (!ImagesByNameItemCache.ContainsKey(key))
|
|
|
|
|
{
|
2012-08-22 02:50:59 +00:00
|
|
|
|
ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 02:50:59 +00:00
|
|
|
|
return ImagesByNameItemCache[key] as Task<T>;
|
2012-08-14 16:06:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
2012-09-04 19:23:15 +00:00
|
|
|
|
item.DateCreated = Directory.GetCreationTimeUtc(path);
|
|
|
|
|
item.DateModified = Directory.GetLastWriteTimeUtc(path);
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 12:59:21 +00:00
|
|
|
|
ItemResolveEventArgs args = new ItemResolveEventArgs();
|
2012-08-23 05:45:26 +00:00
|
|
|
|
args.FileInfo = FileData.GetFileData(path);
|
|
|
|
|
args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
|
2012-08-19 20:38:31 +00:00
|
|
|
|
|
2012-08-20 21:48:11 +00:00
|
|
|
|
await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
|
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
|
|
|
|
}
|
|
|
|
|
}
|