2
0

PeopleValidator.cs 3.7 KB

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