diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 8ef86e54e7..fed206a305 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -8,7 +8,6 @@ using MediaBrowser.Model.Entities; using MoreLinq; using System; using System.Collections; -using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -365,123 +364,125 @@ namespace MediaBrowser.Controller.Entities { var locationType = LocationType; - // Nothing to do here - if (locationType == LocationType.Remote || locationType == LocationType.Virtual) - { - return; - } - cancellationToken.ThrowIfCancellationRequested(); - IEnumerable nonCachedChildren; + var validChildren = new List>(); - try - { - nonCachedChildren = GetNonCachedChildren(); - } - catch (IOException ex) + if (locationType != LocationType.Remote && locationType != LocationType.Virtual) { - nonCachedChildren = new BaseItem[] { }; + IEnumerable nonCachedChildren; - Logger.ErrorException("Error getting file system entries for {0}", ex, Path); - } + try + { + nonCachedChildren = GetNonCachedChildren(); + } + catch (IOException ex) + { + nonCachedChildren = new BaseItem[] {}; - if (nonCachedChildren == null) return; //nothing to validate + Logger.ErrorException("Error getting file system entries for {0}", ex, Path); + } - progress.Report(5); + 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 - var currentChildren = ActualChildren.ToDictionary(i => i.Id); + progress.Report(5); - //create a list for our validated children - var validChildren = new List>(); - var newItems = new List(); + //build a dictionary of the current children we have now by Id so we can compare quickly and easily + var currentChildren = ActualChildren.ToDictionary(i => i.Id); - cancellationToken.ThrowIfCancellationRequested(); + //create a list for our validated children + var newItems = new List(); - foreach (var child in nonCachedChildren) - { - BaseItem currentChild; + cancellationToken.ThrowIfCancellationRequested(); - if (currentChildren.TryGetValue(child.Id, out currentChild)) + foreach (var child in nonCachedChildren) { - currentChild.ResetResolveArgs(child.ResolveArgs); + BaseItem currentChild; - //existing item - check if it has changed - if (currentChild.HasChanged(child)) + if (currentChildren.TryGetValue(child.Id, out currentChild)) { - var currentChildLocationType = currentChild.LocationType; - if (currentChildLocationType != LocationType.Remote && - currentChildLocationType != LocationType.Virtual) + currentChild.ResetResolveArgs(child.ResolveArgs); + + //existing item - check if it has changed + if (currentChild.HasChanged(child)) + { + var currentChildLocationType = currentChild.LocationType; + if (currentChildLocationType != LocationType.Remote && + currentChildLocationType != LocationType.Virtual) + { + EntityResolutionHelper.EnsureDates(FileSystem, currentChild, child.ResolveArgs, false); + } + + validChildren.Add(new Tuple(currentChild, true)); + } + else { - EntityResolutionHelper.EnsureDates(FileSystem, currentChild, child.ResolveArgs, false); + validChildren.Add(new Tuple(currentChild, false)); } - validChildren.Add(new Tuple(currentChild, true)); + currentChild.IsOffline = false; } else { - validChildren.Add(new Tuple(currentChild, false)); - } + //brand new item - needs to be added + newItems.Add(child); - currentChild.IsOffline = false; + validChildren.Add(new Tuple(child, true)); + } } - else + + // If any items were added or removed.... + if (newItems.Count > 0 || currentChildren.Count != validChildren.Count) { - //brand new item - needs to be added - newItems.Add(child); + var newChildren = validChildren.Select(c => c.Item1).ToList(); - validChildren.Add(new Tuple(child, true)); - } - } + // That's all the new and changed ones - now see if there are any that are missing + var itemsRemoved = currentChildren.Values.Except(newChildren).ToList(); - // If any items were added or removed.... - if (newItems.Count > 0 || currentChildren.Count != validChildren.Count) - { - var newChildren = validChildren.Select(c => c.Item1).ToList(); + var actualRemovals = new List(); - // That's all the new and changed ones - now see if there are any that are missing - var itemsRemoved = currentChildren.Values.Except(newChildren).ToList(); + foreach (var item in itemsRemoved) + { + if (item.LocationType == LocationType.Virtual || + item.LocationType == LocationType.Remote) + { + // Don't remove these because there's no way to accurately validate them. + validChildren.Add(new Tuple(item, false)); + } - var actualRemovals = new List(); + else if (!string.IsNullOrEmpty(item.Path) && IsPathOffline(item.Path)) + { + item.IsOffline = true; - foreach (var item in itemsRemoved) - { - if (item.LocationType == LocationType.Virtual || - item.LocationType == LocationType.Remote) - { - // Don't remove these because there's no way to accurately validate them. - continue; + validChildren.Add(new Tuple(item, false)); + } + else + { + item.IsOffline = false; + actualRemovals.Add(item); + } } - - if (!string.IsNullOrEmpty(item.Path) && IsPathOffline(item.Path)) - { - item.IsOffline = true; - validChildren.Add(new Tuple(item, false)); - } - else + if (actualRemovals.Count > 0) { - item.IsOffline = false; - actualRemovals.Add(item); - } - } + RemoveChildrenInternal(actualRemovals); - if (actualRemovals.Count > 0) - { - RemoveChildrenInternal(actualRemovals); - - foreach (var item in actualRemovals) - { - LibraryManager.ReportItemRemoved(item); + foreach (var item in actualRemovals) + { + LibraryManager.ReportItemRemoved(item); + } } - } - await LibraryManager.CreateItems(newItems, cancellationToken).ConfigureAwait(false); + await LibraryManager.CreateItems(newItems, cancellationToken).ConfigureAwait(false); - AddChildrenInternal(newItems); + AddChildrenInternal(newItems); - await ItemRepository.SaveChildren(Id, ActualChildren.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false); + await ItemRepository.SaveChildren(Id, ActualChildren.Select(i => i.Id).ToList(), cancellationToken).ConfigureAwait(false); + } + } + else + { + validChildren.AddRange(ActualChildren.Select(i => new Tuple(i, false))); } progress.Report(10);