From d794eecec4f4b9a46df422b28c86e136bfd92abf Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 19 Aug 2012 11:58:35 -0400 Subject: Added initial implementation of the metadata provider network, along with the first few providers --- MediaBrowser.Controller/Library/ItemController.cs | 126 ++++------------------ 1 file changed, 20 insertions(+), 106 deletions(-) (limited to 'MediaBrowser.Controller/Library') diff --git a/MediaBrowser.Controller/Library/ItemController.cs b/MediaBrowser.Controller/Library/ItemController.cs index 8d269679f..4b0d9a983 100644 --- a/MediaBrowser.Controller/Library/ItemController.cs +++ b/MediaBrowser.Controller/Library/ItemController.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; using MediaBrowser.Controller.Events; using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Resolvers; @@ -58,67 +56,12 @@ namespace MediaBrowser.Controller.Library } #endregion - #region BaseItem Events - /// - /// Called when an item is being created. - /// This should be used to fill item values, such as metadata - /// - public event EventHandler> BaseItemCreating; - - /// - /// Called when an item has been created. - /// This should be used to process or modify item values. - /// - public event EventHandler> BaseItemCreated; - #endregion - - /// - /// Called when an item has been created - /// - private void OnBaseItemCreated(BaseItem item, Folder parent) - { - GenericItemEventArgs args = new GenericItemEventArgs { Item = item }; - - if (BaseItemCreating != null) - { - BaseItemCreating(this, args); - } - - if (BaseItemCreated != null) - { - BaseItemCreated(this, args); - } - } - - private void FireCreateEventsRecursive(Folder folder, Folder parent) - { - OnBaseItemCreated(folder, parent); - - int count = folder.Children.Length; - - Parallel.For(0, count, i => - { - BaseItem item = folder.Children[i]; - - Folder childFolder = item as Folder; - - if (childFolder != null) - { - FireCreateEventsRecursive(childFolder, folder); - } - else - { - OnBaseItemCreated(item, folder); - } - }); - } - - private BaseItem ResolveItem(ItemResolveEventArgs args) + private async Task ResolveItem(ItemResolveEventArgs args) { // If that didn't pan out, try the slow ones foreach (IBaseItemResolver resolver in Kernel.Instance.EntityResolvers) { - var item = resolver.ResolvePath(args); + var item = await resolver.ResolvePath(args); if (item != null) { @@ -132,39 +75,15 @@ namespace MediaBrowser.Controller.Library /// /// Resolves a path into a BaseItem /// - public BaseItem GetItem(string path) - { - return GetItem(null, path); - } - - /// - /// Resolves a path into a BaseItem - /// - public BaseItem GetItem(Folder parent, string path) + public async Task GetItem(Folder parent, string path) { - BaseItem item = GetItemInternal(parent, path, File.GetAttributes(path)); - - if (item != null) - { - var folder = item as Folder; - - if (folder != null) - { - FireCreateEventsRecursive(folder, parent); - } - else - { - OnBaseItemCreated(item, parent); - } - } - - return item; + return await GetItemInternal(parent, path, File.GetAttributes(path)); } /// /// Resolves a path into a BaseItem /// - private BaseItem GetItemInternal(Folder parent, string path, FileAttributes attributes) + private async Task GetItemInternal(Folder parent, string path, FileAttributes attributes) { if (!OnPreBeginResolvePath(parent, path, attributes)) { @@ -201,14 +120,14 @@ namespace MediaBrowser.Controller.Library return null; } - BaseItem item = ResolveItem(args); + BaseItem item = await ResolveItem(args); var folder = item as Folder; if (folder != null) { // If it's a folder look for child entities - AttachChildren(folder, fileSystemChildren); + await AttachChildren(folder, fileSystemChildren); } return item; @@ -217,30 +136,25 @@ namespace MediaBrowser.Controller.Library /// /// Finds child BaseItems for a given Folder /// - private void AttachChildren(Folder folder, IEnumerable> fileSystemChildren) + private async Task AttachChildren(Folder folder, IEnumerable> fileSystemChildren) { - List baseItemChildren = new List(); + KeyValuePair[] fileSystemChildrenArray = fileSystemChildren.ToArray(); - int count = fileSystemChildren.Count(); + int count = fileSystemChildrenArray.Length; - // Resolve the child folder paths into entities - Parallel.For(0, count, i => - { - KeyValuePair child = fileSystemChildren.ElementAt(i); + Task[] tasks = new Task[count]; - BaseItem item = GetItemInternal(folder, child.Key, child.Value); + for (int i = 0; i < count; i++) + { + var child = fileSystemChildrenArray[i]; - if (item != null) - { - lock (baseItemChildren) - { - baseItemChildren.Add(item); - } - } - }); + tasks[i] = GetItemInternal(folder, child.Key, child.Value); + } + BaseItem[] baseItemChildren = await Task.WhenAll(tasks); + // Sort them - folder.Children = baseItemChildren.OrderBy(f => + folder.Children = baseItemChildren.Where(i => i != null).OrderBy(f => { return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName; @@ -363,7 +277,7 @@ namespace MediaBrowser.Controller.Library /// Creates an IBN item based on a given path /// private T CreateImagesByNameItem(string path, string name) - where T : BaseEntity, new () + where T : BaseEntity, new() { T item = new T(); -- cgit v1.2.3