GenresValidator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using Microsoft.Extensions.Logging;
  7. namespace Emby.Server.Implementations.Library.Validators
  8. {
  9. /// <summary>
  10. /// Class GenresValidator.
  11. /// </summary>
  12. public class GenresValidator
  13. {
  14. /// <summary>
  15. /// The library manager.
  16. /// </summary>
  17. private readonly ILibraryManager _libraryManager;
  18. private readonly IItemRepository _itemRepo;
  19. /// <summary>
  20. /// The logger.
  21. /// </summary>
  22. private readonly ILogger<GenresValidator> _logger;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="GenresValidator"/> class.
  25. /// </summary>
  26. /// <param name="libraryManager">The library manager.</param>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="itemRepo">The item repository.</param>
  29. public GenresValidator(ILibraryManager libraryManager, ILogger<GenresValidator> logger, IItemRepository itemRepo)
  30. {
  31. _libraryManager = libraryManager;
  32. _logger = logger;
  33. _itemRepo = itemRepo;
  34. }
  35. /// <summary>
  36. /// Runs the specified progress.
  37. /// </summary>
  38. /// <param name="progress">The progress.</param>
  39. /// <param name="cancellationToken">The cancellation token.</param>
  40. /// <returns>Task.</returns>
  41. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  42. {
  43. var names = _itemRepo.GetGenreNames();
  44. var numComplete = 0;
  45. var count = names.Count;
  46. foreach (var name in names)
  47. {
  48. try
  49. {
  50. var item = _libraryManager.GetGenre(name);
  51. await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  52. }
  53. catch (OperationCanceledException)
  54. {
  55. // Don't clutter the log
  56. throw;
  57. }
  58. catch (Exception ex)
  59. {
  60. _logger.LogError(ex, "Error refreshing {GenreName}", name);
  61. }
  62. numComplete++;
  63. double percent = numComplete;
  64. percent /= count;
  65. percent *= 100;
  66. progress.Report(percent);
  67. }
  68. progress.Report(100);
  69. }
  70. }
  71. }