aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/Folder.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Controller/Entities/Folder.cs')
-rw-r--r--MediaBrowser.Controller/Entities/Folder.cs308
1 files changed, 289 insertions, 19 deletions
diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs
index b9a72e727..1cad13f22 100644
--- a/MediaBrowser.Controller/Entities/Folder.cs
+++ b/MediaBrowser.Controller/Entities/Folder.cs
@@ -1,5 +1,10 @@
using MediaBrowser.Model.Entities;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Common.Logging;
+using MediaBrowser.Controller.Resolvers;
using System;
+using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
@@ -7,6 +12,28 @@ namespace MediaBrowser.Controller.Entities
{
public class Folder : BaseItem
{
+ #region Events
+ /// <summary>
+ /// Fires whenever a validation routine updates our children. The added and removed children are properties of the args.
+ /// *** Will fire asynchronously. ***
+ /// </summary>
+ public event EventHandler<ChildrenChangedEventArgs> ChildrenChanged;
+ protected void OnChildrenChanged(ChildrenChangedEventArgs args)
+ {
+ if (ChildrenChanged != null)
+ {
+ Task.Run( () =>
+ {
+ ChildrenChanged(this, args);
+ Kernel.Instance.OnLibraryChanged(args);
+ });
+ }
+ }
+
+ #endregion
+
+ public IEnumerable<string> PhysicalLocations { get; set; }
+
public override bool IsFolder
{
get
@@ -24,23 +51,266 @@ namespace MediaBrowser.Controller.Entities
return Parent != null && Parent.IsRoot;
}
}
+ protected object childLock = new object();
+ protected List<BaseItem> children;
+ protected virtual List<BaseItem> ActualChildren
+ {
+ get
+ {
+ if (children == null)
+ {
+ LoadChildren();
+ }
+ return children;
+ }
+
+ set
+ {
+ children = value;
+ }
+ }
+
+ /// <summary>
+ /// thread-safe access to the actual children of this folder - without regard to user
+ /// </summary>
+ public IEnumerable<BaseItem> Children
+ {
+ get
+ {
+ lock (childLock)
+ return ActualChildren.ToList();
+ }
+ }
- public IEnumerable<BaseItem> Children { get; set; }
+ /// <summary>
+ /// thread-safe access to all recursive children of this folder - without regard to user
+ /// </summary>
+ public IEnumerable<BaseItem> RecursiveChildren
+ {
+ get
+ {
+ foreach (var item in Children)
+ {
+ yield return item;
+
+ var subFolder = item as Folder;
+
+ if (subFolder != null)
+ {
+ foreach (var subitem in subFolder.RecursiveChildren)
+ {
+ yield return subitem;
+ }
+ }
+ }
+ }
+ }
+
+
+ /// <summary>
+ /// Loads and validates our children
+ /// </summary>
+ protected virtual void LoadChildren()
+ {
+ //first - load our children from the repo
+ lock (childLock)
+ children = GetCachedChildren();
+
+ //then kick off a validation against the actual file system
+ Task.Run(() => ValidateChildren());
+ }
+
+ protected bool ChildrenValidating = false;
+
+ /// <summary>
+ /// Compare our current children (presumably just read from the repo) with the current state of the file system and adjust for any changes
+ /// ***Currently does not contain logic to maintain items that are unavailable in the file system***
+ /// </summary>
+ /// <returns></returns>
+ protected async virtual void ValidateChildren()
+ {
+ if (ChildrenValidating) return; //only ever want one of these going at once and don't want them to fire off in sequence so don't use lock
+ ChildrenValidating = true;
+ bool changed = false; //this will save us a little time at the end if nothing changes
+ var changedArgs = new ChildrenChangedEventArgs(this);
+ //get the current valid children from filesystem (or wherever)
+ var nonCachedChildren = await GetNonCachedChildren();
+ if (nonCachedChildren == null) return; //nothing to validate
+ //build a dictionary of the current children we have now by Id so we can compare quickly and easily
+ Dictionary<Guid, BaseItem> currentChildren;
+ lock (childLock)
+ currentChildren = ActualChildren.ToDictionary(i => i.Id);
+
+ //create a list for our validated children
+ var validChildren = new List<BaseItem>();
+ //now traverse the valid children and find any changed or new items
+ foreach (var child in nonCachedChildren)
+ {
+ BaseItem currentChild;
+ currentChildren.TryGetValue(child.Id, out currentChild);
+ if (currentChild == null)
+ {
+ //brand new item - needs to be added
+ changed = true;
+ changedArgs.ItemsAdded.Add(child);
+ //Logger.LogInfo("New Item Added to Library: ("+child.GetType().Name+")"+ child.Name + "(" + child.Path + ")");
+ //refresh it
+ child.RefreshMetadata();
+ //save it in repo...
+
+ //and add it to our valid children
+ validChildren.Add(child);
+ //fire an added event...?
+ //if it is a folder we need to validate its children as well
+ Folder folder = child as Folder;
+ if (folder != null)
+ {
+ folder.ValidateChildren();
+ //probably need to refresh too...
+ }
+ }
+ else
+ {
+ //existing item - check if it has changed
+ if (currentChild.IsChanged(child))
+ {
+ changed = true;
+ //update resolve args and refresh meta
+ // Note - we are refreshing the existing child instead of the newly found one so the "Except" operation below
+ // will identify this item as the same one
+ currentChild.ResolveArgs = child.ResolveArgs;
+ currentChild.RefreshMetadata();
+ //save it in repo...
+ validChildren.Add(currentChild);
+ }
+ else
+ {
+ //current child that didn't change - just put it in the valid children
+ validChildren.Add(currentChild);
+ }
+ }
+ }
+
+ //that's all the new and changed ones - now see if there are any that are missing
+ changedArgs.ItemsRemoved = currentChildren.Values.Except(validChildren);
+ changed |= changedArgs.ItemsRemoved != null;
+
+ //now, if anything changed - replace our children
+ if (changed)
+ {
+ lock (childLock)
+ ActualChildren = validChildren;
+ //and save children in repo...
+
+ //and fire event
+ this.OnChildrenChanged(changedArgs);
+ }
+ ChildrenValidating = false;
+
+ }
+
+ /// <summary>
+ /// Get the children of this folder from the actual file system
+ /// </summary>
+ /// <returns></returns>
+ protected async virtual Task<IEnumerable<BaseItem>> GetNonCachedChildren()
+ {
+ ItemResolveEventArgs args = new ItemResolveEventArgs()
+ {
+ FileInfo = FileData.GetFileData(this.Path),
+ Parent = this.Parent,
+ Cancel = false,
+ Path = this.Path
+ };
+
+ // Gather child folder and files
+ if (args.IsDirectory)
+ {
+ args.FileSystemChildren = FileData.GetFileSystemEntries(this.Path, "*").ToArray();
+
+ bool isVirtualFolder = Parent != null && Parent.IsRoot;
+ args = FileSystemHelper.FilterChildFileSystemEntries(args, isVirtualFolder);
+ }
+ else
+ {
+ Logger.LogError("Folder has a path that is not a directory: " + this.Path);
+ return null;
+ }
+
+ if (!EntityResolutionHelper.ShouldResolvePathContents(args))
+ {
+ return null;
+ }
+ return (await Task.WhenAll<BaseItem>(GetChildren(args.FileSystemChildren)).ConfigureAwait(false))
+ .Where(i => i != null).OrderBy(f =>
+ {
+ return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
+
+ });
+
+ }
+
+ /// <summary>
+ /// Resolves a path into a BaseItem
+ /// </summary>
+ protected async Task<BaseItem> GetChild(string path, WIN32_FIND_DATA? fileInfo = null)
+ {
+ ItemResolveEventArgs args = new ItemResolveEventArgs()
+ {
+ FileInfo = fileInfo ?? FileData.GetFileData(path),
+ Parent = this,
+ Cancel = false,
+ Path = path
+ };
+
+ args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();
+ args = FileSystemHelper.FilterChildFileSystemEntries(args, false);
+
+ return Kernel.Instance.ResolveItem(args);
+
+ }
+
+ /// <summary>
+ /// Finds child BaseItems for a given Folder
+ /// </summary>
+ protected Task<BaseItem>[] GetChildren(WIN32_FIND_DATA[] fileSystemChildren)
+ {
+ Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];
+
+ for (int i = 0; i < fileSystemChildren.Length; i++)
+ {
+ var child = fileSystemChildren[i];
+
+ tasks[i] = GetChild(child.Path, child);
+ }
+
+ return tasks;
+ }
+
+
+ /// <summary>
+ /// Get our children from the repo - stubbed for now
+ /// </summary>
+ /// <returns></returns>
+ protected virtual List<BaseItem> GetCachedChildren()
+ {
+ return new List<BaseItem>();
+ }
/// <summary>
/// Gets allowed children of an item
/// </summary>
- public IEnumerable<BaseItem> GetParentalAllowedChildren(User user)
+ public IEnumerable<BaseItem> GetChildren(User user)
{
- return Children.Where(c => c.IsParentalAllowed(user));
+ return ActualChildren.Where(c => c.IsParentalAllowed(user));
}
/// <summary>
/// Gets allowed recursive children of an item
/// </summary>
- public IEnumerable<BaseItem> GetParentalAllowedRecursiveChildren(User user)
+ public IEnumerable<BaseItem> GetRecursiveChildren(User user)
{
- foreach (var item in GetParentalAllowedChildren(user))
+ foreach (var item in GetChildren(user))
{
yield return item;
@@ -48,7 +318,7 @@ namespace MediaBrowser.Controller.Entities
if (subFolder != null)
{
- foreach (var subitem in subFolder.GetParentalAllowedRecursiveChildren(user))
+ foreach (var subitem in subFolder.GetRecursiveChildren(user))
{
yield return subitem;
}
@@ -63,7 +333,7 @@ namespace MediaBrowser.Controller.Entities
{
var counts = new ItemSpecialCounts();
- IEnumerable<BaseItem> recursiveChildren = GetParentalAllowedRecursiveChildren(user);
+ IEnumerable<BaseItem> recursiveChildren = GetRecursiveChildren(user);
var recentlyAddedItems = GetRecentlyAddedItems(recursiveChildren, user);
@@ -80,7 +350,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public IEnumerable<BaseItem> GetItemsWithGenre(string genre, User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
+ return GetRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
}
/// <summary>
@@ -88,7 +358,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public IEnumerable<BaseItem> GetItemsWithYear(int year, User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
+ return GetRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
}
/// <summary>
@@ -96,7 +366,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public IEnumerable<BaseItem> GetItemsWithStudio(string studio, User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase)));
+ return GetRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase)));
}
/// <summary>
@@ -104,7 +374,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public IEnumerable<BaseItem> GetFavoriteItems(User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(c =>
+ return GetRecursiveChildren(user).Where(c =>
{
UserItemData data = c.GetUserData(user, false);
@@ -122,7 +392,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public IEnumerable<BaseItem> GetItemsWithPerson(string person, User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(c =>
+ return GetRecursiveChildren(user).Where(c =>
{
if (c.People != null)
{
@@ -139,7 +409,7 @@ namespace MediaBrowser.Controller.Entities
/// <param name="personType">Specify this to limit results to a specific PersonType</param>
public IEnumerable<BaseItem> GetItemsWithPerson(string person, string personType, User user)
{
- return GetParentalAllowedRecursiveChildren(user).Where(c =>
+ return GetRecursiveChildren(user).Where(c =>
{
if (c.People != null)
{
@@ -155,7 +425,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public List<BaseItem> GetRecentlyAddedItems(User user)
{
- return GetRecentlyAddedItems(GetParentalAllowedRecursiveChildren(user), user);
+ return GetRecentlyAddedItems(GetRecursiveChildren(user), user);
}
/// <summary>
@@ -163,7 +433,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public List<BaseItem> GetRecentlyAddedUnplayedItems(User user)
{
- return GetRecentlyAddedUnplayedItems(GetParentalAllowedRecursiveChildren(user), user);
+ return GetRecentlyAddedUnplayedItems(GetRecursiveChildren(user), user);
}
/// <summary>
@@ -171,7 +441,7 @@ namespace MediaBrowser.Controller.Entities
/// </summary>
public List<BaseItem> GetInProgressItems(User user)
{
- return GetInProgressItems(GetParentalAllowedRecursiveChildren(user), user);
+ return GetInProgressItems(GetRecursiveChildren(user), user);
}
/// <summary>
@@ -288,7 +558,7 @@ namespace MediaBrowser.Controller.Entities
base.SetPlayedStatus(user, wasPlayed);
// Now sweep through recursively and update status
- foreach (BaseItem item in GetParentalAllowedChildren(user))
+ foreach (BaseItem item in GetChildren(user))
{
item.SetPlayedStatus(user, wasPlayed);
}
@@ -306,7 +576,7 @@ namespace MediaBrowser.Controller.Entities
return result;
}
- foreach (BaseItem item in Children)
+ foreach (BaseItem item in ActualChildren)
{
result = item.FindItemById(id);
@@ -329,7 +599,7 @@ namespace MediaBrowser.Controller.Entities
return this;
}
- foreach (BaseItem item in Children)
+ foreach (BaseItem item in ActualChildren)
{
var folder = item as Folder;