ChapterImagesTask.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. namespace MediaBrowser.Server.Implementations.ScheduledTasks
  16. {
  17. /// <summary>
  18. /// Class ChapterImagesTask
  19. /// </summary>
  20. class ChapterImagesTask : IScheduledTask
  21. {
  22. /// <summary>
  23. /// The _logger
  24. /// </summary>
  25. private readonly ILogger _logger;
  26. /// <summary>
  27. /// The _library manager
  28. /// </summary>
  29. private readonly ILibraryManager _libraryManager;
  30. /// <summary>
  31. /// The current new item timer
  32. /// </summary>
  33. /// <value>The new item timer.</value>
  34. private Timer NewItemTimer { get; set; }
  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. /// <param name="logManager">The log manager.</param>
  43. /// <param name="libraryManager">The library manager.</param>
  44. /// <param name="itemRepo">The item repo.</param>
  45. public ChapterImagesTask(ILogManager logManager, ILibraryManager libraryManager, IItemRepository itemRepo, IApplicationPaths appPaths, IEncodingManager encodingManager, IFileSystem fileSystem)
  46. {
  47. _logger = logManager.GetLogger(GetType().Name);
  48. _libraryManager = libraryManager;
  49. _itemRepo = itemRepo;
  50. _appPaths = appPaths;
  51. _encodingManager = encodingManager;
  52. _fileSystem = fileSystem;
  53. }
  54. /// <summary>
  55. /// Creates the triggers that define when the task will run
  56. /// </summary>
  57. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  58. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  59. {
  60. return new ITaskTrigger[]
  61. {
  62. new DailyTrigger
  63. {
  64. TimeOfDay = TimeSpan.FromHours(1),
  65. TaskOptions = new TaskExecutionOptions
  66. {
  67. MaxRuntimeMs = Convert.ToInt32(TimeSpan.FromHours(4).TotalMilliseconds)
  68. }
  69. }
  70. };
  71. }
  72. /// <summary>
  73. /// Returns the task to be executed
  74. /// </summary>
  75. /// <param name="cancellationToken">The cancellation token.</param>
  76. /// <param name="progress">The progress.</param>
  77. /// <returns>Task.</returns>
  78. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  79. {
  80. var videos = _libraryManager.RootFolder.GetRecursiveChildren(i => i is Video)
  81. .Cast<Video>()
  82. .ToList();
  83. var numComplete = 0;
  84. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  85. List<string> previouslyFailedImages;
  86. try
  87. {
  88. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  89. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  90. .ToList();
  91. }
  92. catch (FileNotFoundException)
  93. {
  94. previouslyFailedImages = new List<string>();
  95. }
  96. catch (DirectoryNotFoundException)
  97. {
  98. previouslyFailedImages = new List<string>();
  99. }
  100. foreach (var video in videos)
  101. {
  102. cancellationToken.ThrowIfCancellationRequested();
  103. var key = video.Path + video.DateModified.Ticks;
  104. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  105. try
  106. {
  107. var chapters = _itemRepo.GetChapters(video.Id).ToList();
  108. var success = await _encodingManager.RefreshChapterImages(new ChapterImageRefreshOptions
  109. {
  110. SaveChapters = true,
  111. ExtractImages = extract,
  112. Video = video,
  113. Chapters = chapters
  114. }, CancellationToken.None);
  115. if (!success)
  116. {
  117. previouslyFailedImages.Add(key);
  118. var parentPath = Path.GetDirectoryName(failHistoryPath);
  119. _fileSystem.CreateDirectory(parentPath);
  120. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray()));
  121. }
  122. numComplete++;
  123. double percent = numComplete;
  124. percent /= videos.Count;
  125. progress.Report(100 * percent);
  126. }
  127. catch (ObjectDisposedException)
  128. {
  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
  138. {
  139. get
  140. {
  141. return "Chapter image extraction";
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the description.
  146. /// </summary>
  147. /// <value>The description.</value>
  148. public string Description
  149. {
  150. get { return "Creates thumbnails for videos that have chapters."; }
  151. }
  152. /// <summary>
  153. /// Gets the category.
  154. /// </summary>
  155. /// <value>The category.</value>
  156. public string Category
  157. {
  158. get
  159. {
  160. return "Library";
  161. }
  162. }
  163. }
  164. }