RefreshIntrosTask.cs 3.3 KB

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