ChapterImagesTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Controller.Persistence;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Controller.Dto;
  14. using MediaBrowser.Controller.IO;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Tasks;
  18. using MediaBrowser.Model.Extensions;
  19. using MediaBrowser.Controller.Providers;
  20. namespace Emby.Server.Implementations.ScheduledTasks
  21. {
  22. /// <summary>
  23. /// Class ChapterImagesTask
  24. /// </summary>
  25. class ChapterImagesTask : IScheduledTask
  26. {
  27. /// <summary>
  28. /// The _logger
  29. /// </summary>
  30. private readonly ILogger _logger;
  31. /// <summary>
  32. /// The _library manager
  33. /// </summary>
  34. private readonly ILibraryManager _libraryManager;
  35. private readonly IItemRepository _itemRepo;
  36. private readonly IApplicationPaths _appPaths;
  37. private readonly IEncodingManager _encodingManager;
  38. private readonly IFileSystem _fileSystem;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
  41. /// </summary>
  42. public ChapterImagesTask(ILoggerFactory loggerFactory, ILibraryManager libraryManager, IItemRepository itemRepo, IApplicationPaths appPaths, IEncodingManager encodingManager, IFileSystem fileSystem)
  43. {
  44. _logger = loggerFactory.CreateLogger(GetType().Name);
  45. _libraryManager = libraryManager;
  46. _itemRepo = itemRepo;
  47. _appPaths = appPaths;
  48. _encodingManager = encodingManager;
  49. _fileSystem = fileSystem;
  50. }
  51. /// <summary>
  52. /// Creates the triggers that define when the task will run
  53. /// </summary>
  54. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  55. {
  56. return new[] {
  57. new TaskTriggerInfo
  58. {
  59. Type = TaskTriggerInfo.TriggerDaily,
  60. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  61. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  62. }
  63. };
  64. }
  65. public string Key => "RefreshChapterImages";
  66. /// <summary>
  67. /// Returns the task to be executed
  68. /// </summary>
  69. /// <param name="cancellationToken">The cancellation token.</param>
  70. /// <param name="progress">The progress.</param>
  71. /// <returns>Task.</returns>
  72. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  73. {
  74. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  75. {
  76. MediaTypes = new[] { MediaType.Video },
  77. IsFolder = false,
  78. Recursive = true,
  79. DtoOptions = new DtoOptions(false)
  80. {
  81. EnableImages = false
  82. },
  83. SourceTypes = new SourceType[] { SourceType.Library },
  84. HasChapterImages = false,
  85. IsVirtualItem = false
  86. })
  87. .OfType<Video>()
  88. .ToList();
  89. var numComplete = 0;
  90. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  91. List<string> previouslyFailedImages;
  92. try
  93. {
  94. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  95. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  96. .ToList();
  97. }
  98. catch (FileNotFoundException)
  99. {
  100. previouslyFailedImages = new List<string>();
  101. }
  102. catch (IOException)
  103. {
  104. previouslyFailedImages = new List<string>();
  105. }
  106. var directoryService = new DirectoryService(_logger, _fileSystem);
  107. foreach (var video in videos)
  108. {
  109. cancellationToken.ThrowIfCancellationRequested();
  110. var key = video.Path + video.DateModified.Ticks;
  111. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  112. try
  113. {
  114. var chapters = _itemRepo.GetChapters(video);
  115. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  116. if (!success)
  117. {
  118. previouslyFailedImages.Add(key);
  119. var parentPath = _fileSystem.GetDirectoryName(failHistoryPath);
  120. _fileSystem.CreateDirectory(parentPath);
  121. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray()));
  122. }
  123. numComplete++;
  124. double percent = numComplete;
  125. percent /= videos.Count;
  126. progress.Report(100 * percent);
  127. }
  128. catch (ObjectDisposedException)
  129. {
  130. //TODO Investigate and properly fix.
  131. break;
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Gets the name of the task
  137. /// </summary>
  138. /// <value>The name.</value>
  139. public string Name => "Chapter image extraction";
  140. /// <summary>
  141. /// Gets the description.
  142. /// </summary>
  143. /// <value>The description.</value>
  144. public string Description => "Creates thumbnails for videos that have chapters.";
  145. /// <summary>
  146. /// Gets the category.
  147. /// </summary>
  148. /// <value>The category.</value>
  149. public string Category => "Library";
  150. }
  151. }