2012-09-11 03:34:02 +02:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
2012-09-17 17:12:43 +02:00
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2012-09-07 21:22:37 +02:00
|
|
|
|
using System;
|
2012-08-19 22:38:31 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2012-07-12 08:55:27 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Library
|
|
|
|
|
{
|
|
|
|
|
public class ItemController
|
|
|
|
|
{
|
2012-09-17 17:12:43 +02:00
|
|
|
|
//private BaseItem ResolveItem(ItemResolveEventArgs args)
|
|
|
|
|
//{
|
|
|
|
|
// // Try first priority resolvers
|
|
|
|
|
// for (int i = 0; i < Kernel.Instance.EntityResolvers.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// var item = Kernel.Instance.EntityResolvers[i].ResolvePath(args);
|
|
|
|
|
|
|
|
|
|
// if (item != null)
|
|
|
|
|
// {
|
|
|
|
|
// return item;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return null;
|
|
|
|
|
//}
|
2012-07-12 08:55:27 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves a path into a BaseItem
|
|
|
|
|
/// </summary>
|
2012-08-23 14:26:08 +02:00
|
|
|
|
public async Task<BaseItem> GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null, bool allowInternetProviders = true)
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-08-23 07:45:26 +02:00
|
|
|
|
ItemResolveEventArgs args = new ItemResolveEventArgs()
|
|
|
|
|
{
|
|
|
|
|
FileInfo = fileInfo ?? FileData.GetFileData(path),
|
|
|
|
|
Parent = parent,
|
|
|
|
|
Cancel = false,
|
|
|
|
|
Path = path
|
|
|
|
|
};
|
2012-08-21 01:53:32 +02:00
|
|
|
|
|
2012-07-12 08:55:27 +02:00
|
|
|
|
// Gather child folder and files
|
2012-08-23 07:45:26 +02:00
|
|
|
|
if (args.IsDirectory)
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-09-17 17:12:43 +02:00
|
|
|
|
args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
|
2012-07-12 08:55:27 +02:00
|
|
|
|
|
|
|
|
|
bool isVirtualFolder = parent != null && parent.IsRoot;
|
2012-09-17 17:12:43 +02:00
|
|
|
|
args = FileSystemHelper.FilterChildFileSystemEntries(args, isVirtualFolder);
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-09-17 17:12:43 +02:00
|
|
|
|
args.FileSystemChildren = new WIN32_FIND_DATA[] { };
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fire BeginResolvePath to see if anyone wants to cancel this operation
|
2012-09-17 17:12:43 +02:00
|
|
|
|
if (!EntityResolutionHelper.ShouldResolvePathContents(args))
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 17:12:43 +02:00
|
|
|
|
BaseItem item = Kernel.Instance.ResolveItem(args);
|
2012-07-12 08:55:27 +02:00
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 17:12:43 +02:00
|
|
|
|
///// <summary>
|
|
|
|
|
///// Finds child BaseItems for a given Folder
|
|
|
|
|
///// </summary>
|
|
|
|
|
//private Task<BaseItem>[] GetChildren(Folder folder, WIN32_FIND_DATA[] fileSystemChildren, bool allowInternetProviders)
|
|
|
|
|
//{
|
|
|
|
|
// Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i < fileSystemChildren.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// var child = fileSystemChildren[i];
|
|
|
|
|
|
|
|
|
|
// tasks[i] = GetItem(child.Path, folder, child, allowInternetProviders: allowInternetProviders);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return tasks;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// Transforms shortcuts into their actual paths
|
|
|
|
|
///// </summary>
|
|
|
|
|
//private WIN32_FIND_DATA[] FilterChildFileSystemEntries(WIN32_FIND_DATA[] fileSystemChildren, bool flattenShortcuts)
|
|
|
|
|
//{
|
|
|
|
|
// WIN32_FIND_DATA[] returnArray = new WIN32_FIND_DATA[fileSystemChildren.Length];
|
|
|
|
|
// List<WIN32_FIND_DATA> resolvedShortcuts = new List<WIN32_FIND_DATA>();
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i < fileSystemChildren.Length; i++)
|
|
|
|
|
// {
|
|
|
|
|
// WIN32_FIND_DATA file = fileSystemChildren[i];
|
|
|
|
|
|
|
|
|
|
// // If it's a shortcut, resolve it
|
|
|
|
|
// if (Shortcut.IsShortcut(file.Path))
|
|
|
|
|
// {
|
|
|
|
|
// string newPath = Shortcut.ResolveShortcut(file.Path);
|
|
|
|
|
// WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
|
|
|
|
|
|
|
|
|
|
// // Find out if the shortcut is pointing to a directory or file
|
|
|
|
|
// if (newPathData.IsDirectory)
|
|
|
|
|
// {
|
|
|
|
|
// // If we're flattening then get the shortcut's children
|
|
|
|
|
|
|
|
|
|
// if (flattenShortcuts)
|
|
|
|
|
// {
|
|
|
|
|
// returnArray[i] = file;
|
|
|
|
|
// WIN32_FIND_DATA[] newChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray();
|
|
|
|
|
|
|
|
|
|
// resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false));
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// returnArray[i] = newPathData;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// returnArray[i] = newPathData;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// returnArray[i] = file;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (resolvedShortcuts.Count > 0)
|
|
|
|
|
// {
|
|
|
|
|
// resolvedShortcuts.InsertRange(0, returnArray);
|
|
|
|
|
// return resolvedShortcuts.ToArray();
|
|
|
|
|
// }
|
|
|
|
|
// else
|
|
|
|
|
// {
|
|
|
|
|
// return returnArray;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2012-07-16 18:50:44 +02:00
|
|
|
|
|
2012-08-14 18:06:46 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Person
|
|
|
|
|
/// </summary>
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public Task<Person> GetPerson(string name)
|
2012-07-16 18:50:44 +02:00
|
|
|
|
{
|
2012-08-28 14:39:23 +02:00
|
|
|
|
return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
|
2012-07-16 18:50:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 18:06:46 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Studio
|
|
|
|
|
/// </summary>
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public Task<Studio> GetStudio(string name)
|
2012-07-16 18:50:44 +02:00
|
|
|
|
{
|
2012-08-28 14:39:23 +02:00
|
|
|
|
return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
|
2012-07-16 18:50:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 18:06:46 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Genre
|
|
|
|
|
/// </summary>
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public Task<Genre> GetGenre(string name)
|
2012-07-20 04:22:44 +02:00
|
|
|
|
{
|
2012-08-28 14:39:23 +02:00
|
|
|
|
return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
|
2012-07-20 04:22:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-14 18:06:46 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a Year
|
|
|
|
|
/// </summary>
|
2012-08-22 04:50:59 +02:00
|
|
|
|
public Task<Year> GetYear(int value)
|
2012-07-16 18:50:44 +02:00
|
|
|
|
{
|
2012-08-28 14:39:23 +02:00
|
|
|
|
return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
|
2012-08-14 18:06:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-28 14:39:23 +02:00
|
|
|
|
private ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generically retrieves an IBN item
|
|
|
|
|
/// </summary>
|
2012-08-22 04:50:59 +02:00
|
|
|
|
private Task<T> GetImagesByNameItem<T>(string path, string name)
|
2012-08-14 18:06:46 +02:00
|
|
|
|
where T : BaseEntity, new()
|
|
|
|
|
{
|
2012-08-28 14:39:23 +02:00
|
|
|
|
name = FileData.GetValidFilename(name);
|
|
|
|
|
|
|
|
|
|
string key = Path.Combine(path, name);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
|
|
|
|
|
// Look for it in the cache, if it's not there, create it
|
|
|
|
|
if (!ImagesByNameItemCache.ContainsKey(key))
|
|
|
|
|
{
|
2012-08-22 04:50:59 +02:00
|
|
|
|
ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
return ImagesByNameItemCache[key] as Task<T>;
|
2012-08-14 18:06:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an IBN item based on a given path
|
|
|
|
|
/// </summary>
|
2012-08-19 22:38:31 +02:00
|
|
|
|
private async Task<T> CreateImagesByNameItem<T>(string path, string name)
|
2012-08-19 17:58:35 +02:00
|
|
|
|
where T : BaseEntity, new()
|
2012-08-14 18:06:46 +02:00
|
|
|
|
{
|
|
|
|
|
T item = new T();
|
|
|
|
|
|
|
|
|
|
item.Name = name;
|
2012-09-17 17:12:43 +02:00
|
|
|
|
item.Id = path.GetMD5();
|
2012-08-14 18:06:46 +02:00
|
|
|
|
|
2012-08-19 22:38:31 +02:00
|
|
|
|
if (!Directory.Exists(path))
|
2012-08-14 18:06:46 +02:00
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
Directory.CreateDirectory(path);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
}
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-09-04 21:23:15 +02:00
|
|
|
|
item.DateCreated = Directory.GetCreationTimeUtc(path);
|
|
|
|
|
item.DateModified = Directory.GetLastWriteTimeUtc(path);
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-20 14:59:21 +02:00
|
|
|
|
ItemResolveEventArgs args = new ItemResolveEventArgs();
|
2012-08-23 07:45:26 +02:00
|
|
|
|
args.FileInfo = FileData.GetFileData(path);
|
|
|
|
|
args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
|
2012-08-19 22:38:31 +02:00
|
|
|
|
|
2012-08-20 23:48:11 +02:00
|
|
|
|
await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
|
2012-08-14 18:06:46 +02:00
|
|
|
|
|
|
|
|
|
return item;
|
2012-07-16 18:50:44 +02:00
|
|
|
|
}
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|