RefreshIntrosTask.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using MediaBrowser.Controller.IO;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Providers
  9. {
  10. /// <summary>
  11. /// Class RefreshIntrosTask
  12. /// </summary>
  13. public class RefreshIntrosTask : ILibraryPostScanTask
  14. {
  15. /// <summary>
  16. /// The _library manager
  17. /// </summary>
  18. private readonly ILibraryManager _libraryManager;
  19. /// <summary>
  20. /// The _logger
  21. /// </summary>
  22. private readonly ILogger _logger;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="RefreshIntrosTask"/> class.
  25. /// </summary>
  26. /// <param name="libraryManager">The library manager.</param>
  27. /// <param name="logger">The logger.</param>
  28. public RefreshIntrosTask(ILibraryManager libraryManager, ILogger logger)
  29. {
  30. _libraryManager = libraryManager;
  31. _logger = logger;
  32. }
  33. /// <summary>
  34. /// Runs the specified progress.
  35. /// </summary>
  36. /// <param name="progress">The progress.</param>
  37. /// <param name="cancellationToken">The cancellation token.</param>
  38. /// <returns>Task.</returns>
  39. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  40. {
  41. var files = _libraryManager.GetAllIntroFiles().ToList();
  42. var numComplete = 0;
  43. foreach (var file in files)
  44. {
  45. cancellationToken.ThrowIfCancellationRequested();
  46. try
  47. {
  48. await RefreshIntro(file, cancellationToken).ConfigureAwait(false);
  49. }
  50. catch (OperationCanceledException)
  51. {
  52. throw;
  53. }
  54. catch (Exception ex)
  55. {
  56. _logger.ErrorException("Error refreshing intro {0}", ex, file);
  57. }
  58. numComplete++;
  59. double percent = numComplete;
  60. percent /= files.Count;
  61. progress.Report(percent * 100);
  62. }
  63. }
  64. /// <summary>
  65. /// Refreshes the intro.
  66. /// </summary>
  67. /// <param name="path">The path.</param>
  68. /// <param name="cancellationToken">The cancellation token.</param>
  69. /// <returns>Task.</returns>
  70. private async Task RefreshIntro(string path, CancellationToken cancellationToken)
  71. {
  72. var item = _libraryManager.ResolvePath(FileSystem.GetFileSystemInfo(path));
  73. if (item == null)
  74. {
  75. _logger.Error("Intro resolver returned null for {0}", path);
  76. return;
  77. }
  78. var dbItem = _libraryManager.RetrieveItem(item.Id);
  79. var isNewItem = false;
  80. if (dbItem != null)
  81. {
  82. dbItem.ResetResolveArgs(item.ResolveArgs);
  83. item = dbItem;
  84. }
  85. else
  86. {
  87. isNewItem = true;
  88. }
  89. // Force the save if it's a new item
  90. await item.RefreshMetadata(cancellationToken, isNewItem).ConfigureAwait(false);
  91. }
  92. }
  93. }