PeopleValidator.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 people = _libraryManager.GetPeople(new InternalPeopleQuery());
  54. var dict = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  55. foreach (var person in people)
  56. {
  57. dict[person.Name] = true;
  58. }
  59. var numComplete = 0;
  60. _logger.Debug("Will refresh {0} people", dict.Count);
  61. var numPeople = dict.Count;
  62. foreach (var person in dict)
  63. {
  64. cancellationToken.ThrowIfCancellationRequested();
  65. try
  66. {
  67. var item = _libraryManager.GetPerson(person.Key);
  68. var options = new MetadataRefreshOptions(_fileSystem)
  69. {
  70. ImageRefreshMode = ImageRefreshMode.ValidationOnly,
  71. MetadataRefreshMode = MetadataRefreshMode.ValidationOnly
  72. };
  73. await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false);
  74. }
  75. catch (OperationCanceledException)
  76. {
  77. throw;
  78. }
  79. catch (Exception ex)
  80. {
  81. _logger.ErrorException("Error validating IBN entry {0}", ex, person);
  82. }
  83. // Update progress
  84. numComplete++;
  85. double percent = numComplete;
  86. percent /= numPeople;
  87. progress.Report(100 * percent);
  88. }
  89. progress.Report(100);
  90. _logger.Info("People validation complete");
  91. }
  92. }
  93. }