fixed validation not refreshing virtual items

This commit is contained in:
Luke Pulverenti 2013-12-26 19:52:36 -05:00
parent 809ed093e8
commit 39e8411594

View file

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