ChapterImagesTask.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.MediaEncoding;
  6. using MediaBrowser.Controller.Persistence;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using CommonIO;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Tasks;
  17. namespace MediaBrowser.Server.Implementations.ScheduledTasks
  18. {
  19. /// <summary>
  20. /// Class ChapterImagesTask
  21. /// </summary>
  22. class ChapterImagesTask : IScheduledTask
  23. {
  24. /// <summary>
  25. /// The _logger
  26. /// </summary>
  27. private readonly ILogger _logger;
  28. /// <summary>
  29. /// The _library manager
  30. /// </summary>
  31. private readonly ILibraryManager _libraryManager;
  32. /// <summary>
  33. /// The current new item timer
  34. /// </summary>
  35. /// <value>The new item timer.</value>
  36. private Timer NewItemTimer { get; set; }
  37. private readonly IItemRepository _itemRepo;
  38. private readonly IApplicationPaths _appPaths;
  39. private readonly IEncodingManager _encodingManager;
  40. private readonly IFileSystem _fileSystem;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
  43. /// </summary>
  44. /// <param name="logManager">The log manager.</param>
  45. /// <param name="libraryManager">The library manager.</param>
  46. /// <param name="itemRepo">The item repo.</param>
  47. public ChapterImagesTask(ILogManager logManager, ILibraryManager libraryManager, IItemRepository itemRepo, IApplicationPaths appPaths, IEncodingManager encodingManager, IFileSystem fileSystem)
  48. {
  49. _logger = logManager.GetLogger(GetType().Name);
  50. _libraryManager = libraryManager;
  51. _itemRepo = itemRepo;
  52. _appPaths = appPaths;
  53. _encodingManager = encodingManager;
  54. _fileSystem = fileSystem;
  55. }
  56. /// <summary>
  57. /// Creates the triggers that define when the task will run
  58. /// </summary>
  59. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  60. {
  61. return new[] {
  62. new TaskTriggerInfo
  63. {
  64. Type = TaskTriggerInfo.TriggerDaily,
  65. TimeOfDayTicks = TimeSpan.FromHours(1).Ticks,
  66. MaxRuntimeMs = Convert.ToInt32(TimeSpan.FromHours(4).TotalMilliseconds)
  67. }
  68. };
  69. }
  70. public string Key
  71. {
  72. get { return "RefreshChapterImages"; }
  73. }
  74. /// <summary>
  75. /// Returns the task to be executed
  76. /// </summary>
  77. /// <param name="cancellationToken">The cancellation token.</param>
  78. /// <param name="progress">The progress.</param>
  79. /// <returns>Task.</returns>
  80. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  81. {
  82. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  83. {
  84. MediaTypes = new[] { MediaType.Video },
  85. IsFolder = false,
  86. Recursive = true
  87. })
  88. .OfType<Video>()
  89. .ToList();
  90. var numComplete = 0;
  91. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  92. List<string> previouslyFailedImages;
  93. try
  94. {
  95. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  96. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  97. .ToList();
  98. }
  99. catch (FileNotFoundException)
  100. {
  101. previouslyFailedImages = new List<string>();
  102. }
  103. catch (DirectoryNotFoundException)
  104. {
  105. previouslyFailedImages = new List<string>();
  106. }
  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.Id).ToList();
  115. var success = await _encodingManager.RefreshChapterImages(new ChapterImageRefreshOptions
  116. {
  117. SaveChapters = true,
  118. ExtractImages = extract,
  119. Video = video,
  120. Chapters = chapters
  121. }, CancellationToken.None);
  122. if (!success)
  123. {
  124. previouslyFailedImages.Add(key);
  125. var parentPath = Path.GetDirectoryName(failHistoryPath);
  126. _fileSystem.CreateDirectory(parentPath);
  127. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray()));
  128. }
  129. numComplete++;
  130. double percent = numComplete;
  131. percent /= videos.Count;
  132. progress.Report(100 * percent);
  133. }
  134. catch (ObjectDisposedException)
  135. {
  136. break;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Gets the name of the task
  142. /// </summary>
  143. /// <value>The name.</value>
  144. public string Name
  145. {
  146. get
  147. {
  148. return "Chapter image extraction";
  149. }
  150. }
  151. /// <summary>
  152. /// Gets the description.
  153. /// </summary>
  154. /// <value>The description.</value>
  155. public string Description
  156. {
  157. get { return "Creates thumbnails for videos that have chapters."; }
  158. }
  159. /// <summary>
  160. /// Gets the category.
  161. /// </summary>
  162. /// <value>The category.</value>
  163. public string Category
  164. {
  165. get
  166. {
  167. return "Library";
  168. }
  169. }
  170. }
  171. }