2
0

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