PeopleValidator.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using MediaBrowser.Controller.IO;
  15. using MediaBrowser.Model.IO;
  16. namespace Emby.Server.Implementations.Library.Validators
  17. {
  18. /// <summary>
  19. /// Class PeopleValidator
  20. /// </summary>
  21. public class PeopleValidator
  22. {
  23. /// <summary>
  24. /// The _library manager
  25. /// </summary>
  26. private readonly ILibraryManager _libraryManager;
  27. /// <summary>
  28. /// The _logger
  29. /// </summary>
  30. private readonly ILogger _logger;
  31. private readonly IServerConfigurationManager _config;
  32. private readonly IFileSystem _fileSystem;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
  35. /// </summary>
  36. /// <param name="libraryManager">The library manager.</param>
  37. /// <param name="logger">The logger.</param>
  38. public PeopleValidator(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem)
  39. {
  40. _libraryManager = libraryManager;
  41. _logger = logger;
  42. _config = config;
  43. _fileSystem = fileSystem;
  44. }
  45. /// <summary>
  46. /// Validates the people.
  47. /// </summary>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. /// <param name="progress">The progress.</param>
  50. /// <returns>Task.</returns>
  51. public async Task ValidatePeople(CancellationToken cancellationToken, IProgress<double> progress)
  52. {
  53. var innerProgress = new ActionableProgress<double>();
  54. innerProgress.RegisterAction(pct => progress.Report(pct * .15));
  55. var people = _libraryManager.GetPeople(new InternalPeopleQuery());
  56. var dict = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  57. foreach (var person in people)
  58. {
  59. dict[person.Name] = true;
  60. }
  61. var numComplete = 0;
  62. _logger.Debug("Will refresh {0} people", dict.Count);
  63. var numPeople = dict.Count;
  64. foreach (var person in dict)
  65. {
  66. cancellationToken.ThrowIfCancellationRequested();
  67. try
  68. {
  69. var item = _libraryManager.GetPerson(person.Key);
  70. var options = new MetadataRefreshOptions(_fileSystem)
  71. {
  72. ImageRefreshMode = ImageRefreshMode.ValidationOnly,
  73. MetadataRefreshMode = MetadataRefreshMode.ValidationOnly
  74. };
  75. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  76. }
  77. catch (OperationCanceledException)
  78. {
  79. throw;
  80. }
  81. catch (Exception ex)
  82. {
  83. _logger.ErrorException("Error validating IBN entry {0}", ex, person);
  84. }
  85. // Update progress
  86. numComplete++;
  87. double percent = numComplete;
  88. percent /= numPeople;
  89. progress.Report(100 * percent);
  90. }
  91. progress.Report(100);
  92. _logger.Info("People validation complete");
  93. }
  94. }
  95. }