PeopleValidator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Data.Enums;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.IO;
  10. using Microsoft.Extensions.Logging;
  11. namespace Emby.Server.Implementations.Library.Validators
  12. {
  13. /// <summary>
  14. /// Class PeopleValidator.
  15. /// </summary>
  16. public class PeopleValidator
  17. {
  18. /// <summary>
  19. /// The _library manager.
  20. /// </summary>
  21. private readonly ILibraryManager _libraryManager;
  22. /// <summary>
  23. /// The _logger.
  24. /// </summary>
  25. private readonly ILogger _logger;
  26. private readonly IFileSystem _fileSystem;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
  29. /// </summary>
  30. /// <param name="libraryManager">The library manager.</param>
  31. /// <param name="logger">The logger.</param>
  32. /// <param name="fileSystem">The file system.</param>
  33. public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IFileSystem fileSystem)
  34. {
  35. _libraryManager = libraryManager;
  36. _logger = logger;
  37. _fileSystem = fileSystem;
  38. }
  39. /// <summary>
  40. /// Validates the people.
  41. /// </summary>
  42. /// <param name="cancellationToken">The cancellation token.</param>
  43. /// <param name="progress">The progress.</param>
  44. /// <returns>Task.</returns>
  45. public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
  46. {
  47. var people = _libraryManager.GetPeopleNames(new InternalPeopleQuery());
  48. var numComplete = 0;
  49. var numPeople = people.Count;
  50. _logger.LogDebug("Will refresh {0} people", numPeople);
  51. foreach (var person in people)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. try
  55. {
  56. var item = _libraryManager.GetPerson(person);
  57. if (item is null)
  58. {
  59. _logger.LogWarning("Failed to get person: {Name}", person);
  60. continue;
  61. }
  62. var options = new MetadataRefreshOptions(new DirectoryService(_fileSystem))
  63. {
  64. ImageRefreshMode = MetadataRefreshMode.ValidationOnly,
  65. MetadataRefreshMode = MetadataRefreshMode.ValidationOnly
  66. };
  67. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  68. }
  69. catch (OperationCanceledException)
  70. {
  71. throw;
  72. }
  73. catch (Exception ex)
  74. {
  75. _logger.LogError(ex, "Error validating IBN entry {Person}", person);
  76. }
  77. // Update progress
  78. numComplete++;
  79. double percent = numComplete;
  80. percent /= numPeople;
  81. progress.Report(100 * percent);
  82. }
  83. var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery
  84. {
  85. IncludeItemTypes = [BaseItemKind.Person],
  86. IsDeadPerson = true,
  87. IsLocked = false
  88. });
  89. foreach (var item in deadEntities)
  90. {
  91. _logger.LogInformation(
  92. "Deleting dead {2} {0} {1}.",
  93. item.Id.ToString("N", CultureInfo.InvariantCulture),
  94. item.Name,
  95. item.GetType().Name);
  96. _libraryManager.DeleteItem(
  97. item,
  98. new DeleteOptions
  99. {
  100. DeleteFileLocation = false
  101. },
  102. false);
  103. }
  104. progress.Report(100);
  105. _logger.LogInformation("People validation complete");
  106. }
  107. }
  108. }