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