ChapterImagesTask.cs 5.9 KB

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