using MediaBrowser.Common.Progress;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
namespace Emby.Server.Implementations.Library.Validators
{
///
/// Class PeopleValidator
///
public class PeopleValidator
{
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// The _logger
///
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
///
/// Initializes a new instance of the class.
///
/// The library manager.
/// The logger.
public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
{
_libraryManager = libraryManager;
_logger = logger;
_config = config;
_fileSystem = fileSystem;
}
///
/// Validates the people.
///
/// The cancellation token.
/// The progress.
/// Task.
public async Task ValidatePeople(CancellationToken cancellationToken, IProgress progress)
{
var people = _libraryManager.GetPeople(new InternalPeopleQuery());
var dict = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (var person in people)
{
dict[person.Name] = true;
}
var numComplete = 0;
_logger.Debug("Will refresh {0} people", dict.Count);
var numPeople = dict.Count;
foreach (var person in dict)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var item = _libraryManager.GetPerson(person.Key);
var options = new MetadataRefreshOptions(_fileSystem)
{
ImageRefreshMode = ImageRefreshMode.ValidationOnly,
MetadataRefreshMode = MetadataRefreshMode.ValidationOnly
};
await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_logger.ErrorException("Error validating IBN entry {0}", ex, person);
}
// Update progress
numComplete++;
double percent = numComplete;
percent /= numPeople;
progress.Report(100 * percent);
}
progress.Report(100);
_logger.Info("People validation complete");
}
}
}