jellyfin/Emby.Server.Implementations/Library/Validators/PeopleValidator.cs

122 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
2014-08-14 15:24:30 +02:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.Library.Validators
{
/// <summary>
2019-09-10 22:37:53 +02:00
/// Class PeopleValidator.
/// </summary>
public class PeopleValidator
{
/// <summary>
2019-09-10 22:37:53 +02:00
/// The _library manager.
/// </summary>
private readonly ILibraryManager _libraryManager;
2019-09-10 22:37:53 +02:00
/// <summary>
2019-09-10 22:37:53 +02:00
/// The _logger.
/// </summary>
private readonly ILogger _logger;
2015-09-14 01:07:54 +02:00
private readonly IFileSystem _fileSystem;
2014-08-14 15:24:30 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="PeopleValidator" /> class.
/// </summary>
/// <param name="libraryManager">The library manager.</param>
/// <param name="logger">The logger.</param>
2019-11-01 18:38:54 +01:00
/// <param name="fileSystem">The file system.</param>
2019-02-06 20:38:42 +01:00
public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
{
_libraryManager = libraryManager;
_logger = logger;
2015-09-14 01:07:54 +02:00
_fileSystem = fileSystem;
2014-08-14 15:24:30 +02:00
}
/// <summary>
/// Validates the people.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <returns>Task.</returns>
2014-08-14 15:24:30 +02:00
public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
{
var people = _libraryManager.GetPeopleNames(new InternalPeopleQuery());
2014-08-14 15:24:30 +02:00
var numComplete = 0;
2016-08-04 06:38:58 +02:00
var numPeople = people.Count;
2016-08-04 06:38:58 +02:00
_logger.LogDebug("Will refresh {0} people", numPeople);
2016-08-07 23:57:46 +02:00
foreach (var person in people)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
var item = _libraryManager.GetPerson(person);
2014-08-14 15:24:30 +02:00
2019-09-10 22:37:53 +02:00
var options = new MetadataRefreshOptions(new DirectoryService(_fileSystem))
2016-11-18 09:39:20 +01:00
{
2018-09-12 19:26:21 +02:00
ImageRefreshMode = MetadataRefreshMode.ValidationOnly,
2016-11-18 09:39:20 +01:00
MetadataRefreshMode = MetadataRefreshMode.ValidationOnly
};
await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
}
2015-10-05 05:24:24 +02:00
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
2018-12-20 13:11:26 +01:00
_logger.LogError(ex, "Error validating IBN entry {person}", person);
}
// Update progress
numComplete++;
double percent = numComplete;
2016-08-07 23:57:46 +02:00
percent /= numPeople;
2014-05-29 16:19:12 +02:00
progress.Report(100 * percent);
}
2018-09-12 19:26:21 +02:00
var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(Person).Name },
IsDeadPerson = true,
IsLocked = false
});
foreach (var item in deadEntities)
{
2019-09-10 22:37:53 +02:00
_logger.LogInformation(
"Deleting dead {2} {0} {1}.",
item.Id.ToString("N", CultureInfo.InvariantCulture),
item.Name,
item.GetType().Name);
_libraryManager.DeleteItem(
item,
new DeleteOptions
{
DeleteFileLocation = false
},
false);
2018-09-12 19:26:21 +02:00
}
progress.Report(100);
_logger.LogInformation("People validation complete");
}
}
}