2
0

ChapterImagesTask.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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
  66. {
  67. get { return "RefreshChapterImages"; }
  68. }
  69. /// <summary>
  70. /// Returns the task to be executed
  71. /// </summary>
  72. /// <param name="cancellationToken">The cancellation token.</param>
  73. /// <param name="progress">The progress.</param>
  74. /// <returns>Task.</returns>
  75. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  76. {
  77. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  78. {
  79. MediaTypes = new[] { MediaType.Video },
  80. IsFolder = false,
  81. Recursive = true,
  82. DtoOptions = new DtoOptions(false)
  83. {
  84. EnableImages = false
  85. },
  86. SourceTypes = new SourceType[] { SourceType.Library },
  87. HasChapterImages = false,
  88. IsVirtualItem = false
  89. })
  90. .OfType<Video>()
  91. .ToList();
  92. var numComplete = 0;
  93. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  94. List<string> previouslyFailedImages;
  95. try
  96. {
  97. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  98. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  99. .ToList();
  100. }
  101. catch (FileNotFoundException)
  102. {
  103. previouslyFailedImages = new List<string>();
  104. }
  105. catch (IOException)
  106. {
  107. previouslyFailedImages = new List<string>();
  108. }
  109. var directoryService = new DirectoryService(_logger, _fileSystem);
  110. foreach (var video in videos)
  111. {
  112. cancellationToken.ThrowIfCancellationRequested();
  113. var key = video.Path + video.DateModified.Ticks;
  114. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  115. try
  116. {
  117. var chapters = _itemRepo.GetChapters(video);
  118. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  119. if (!success)
  120. {
  121. previouslyFailedImages.Add(key);
  122. var parentPath = _fileSystem.GetDirectoryName(failHistoryPath);
  123. _fileSystem.CreateDirectory(parentPath);
  124. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray()));
  125. }
  126. numComplete++;
  127. double percent = numComplete;
  128. percent /= videos.Count;
  129. progress.Report(100 * percent);
  130. }
  131. catch (ObjectDisposedException)
  132. {
  133. break;
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the name of the task
  139. /// </summary>
  140. /// <value>The name.</value>
  141. public string Name
  142. {
  143. get
  144. {
  145. return "Chapter image extraction";
  146. }
  147. }
  148. /// <summary>
  149. /// Gets the description.
  150. /// </summary>
  151. /// <value>The description.</value>
  152. public string Description
  153. {
  154. get { return "Creates thumbnails for videos that have chapters."; }
  155. }
  156. /// <summary>
  157. /// Gets the category.
  158. /// </summary>
  159. /// <value>The category.</value>
  160. public string Category
  161. {
  162. get
  163. {
  164. return "Library";
  165. }
  166. }
  167. }
  168. }