RefreshIntrosTask.cs 3.2 KB

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