jellyfin/MediaBrowser.Controller/Library/ItemController.cs

226 lines
7.7 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Common.Extensions;
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;
namespace MediaBrowser.Controller.Library
{
public class ItemController
{
//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 06:55:27 +00:00
/// <summary>
/// Resolves a path into a BaseItem
/// </summary>
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
{
FileInfo = fileInfo ?? FileData.GetFileData(path),
Parent = parent,
Cancel = false,
Path = path
};
2012-07-12 06:55:27 +00:00
// Gather child folder and files
if (args.IsDirectory)
2012-07-12 06:55:27 +00:00
{
args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
2012-07-12 06:55:27 +00:00
bool isVirtualFolder = parent != null && parent.IsRoot;
args = FileSystemHelper.FilterChildFileSystemEntries(args, isVirtualFolder);
2012-07-12 06:55:27 +00:00
}
else
{
args.FileSystemChildren = new WIN32_FIND_DATA[] { };
2012-07-12 06:55:27 +00:00
}
// Fire BeginResolvePath to see if anyone wants to cancel this operation
if (!EntityResolutionHelper.ShouldResolvePathContents(args))
2012-07-12 06:55:27 +00:00
{
return null;
}
BaseItem item = Kernel.Instance.ResolveItem(args);
2012-07-12 06:55:27 +00:00
return item;
}
///// <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;
// }
//}
/// <summary>
/// Gets a Person
/// </summary>
2012-08-22 02:50:59 +00:00
public Task<Person> GetPerson(string name)
{
return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
}
/// <summary>
/// Gets a Studio
/// </summary>
2012-08-22 02:50:59 +00:00
public Task<Studio> GetStudio(string name)
{
return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
}
/// <summary>
/// Gets a Genre
/// </summary>
2012-08-22 02:50:59 +00:00
public Task<Genre> GetGenre(string name)
{
return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
}
/// <summary>
/// Gets a Year
/// </summary>
2012-08-22 02:50:59 +00:00
public Task<Year> GetYear(int value)
{
return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
}
2012-09-11 18:20:12 +00:00
private readonly ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Generically retrieves an IBN item
/// </summary>
2012-08-22 02:50:59 +00:00
private Task<T> GetImagesByNameItem<T>(string path, string name)
where T : BaseEntity, new()
{
name = FileData.GetValidFilename(name);
2012-09-13 20:56:45 +00:00
path = Path.Combine(path, name);
// Look for it in the cache, if it's not there, create it
2012-09-13 20:56:45 +00:00
if (!ImagesByNameItemCache.ContainsKey(path))
{
2012-09-13 20:56:45 +00:00
ImagesByNameItemCache[path] = CreateImagesByNameItem<T>(path, name);
}
2012-09-13 20:56:45 +00:00
return ImagesByNameItemCache[path] as Task<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()
{
2012-09-11 19:37:14 +00:00
var item = new T { };
item.Name = name;
item.Id = path.GetMD5();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
2012-09-04 19:23:15 +00:00
item.DateCreated = Directory.GetCreationTimeUtc(path);
item.DateModified = Directory.GetLastWriteTimeUtc(path);
2012-09-11 19:37:14 +00:00
var args = new ItemResolveEventArgs { };
args.FileInfo = FileData.GetFileData(path);
args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
await Kernel.Instance.ExecuteMetadataProviders(item).ConfigureAwait(false);
return item;
}
2012-07-12 06:55:27 +00:00
}
}