2
0

ChapterImagesTask.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public 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. {
  56. new TaskTriggerInfo
  57. {
  58. Type = TaskTriggerInfo.TriggerDaily,
  59. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  60. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  61. }
  62. };
  63. }
  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. if (File.Exists(failHistoryPath))
  91. {
  92. try
  93. {
  94. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  95. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  96. .ToList();
  97. }
  98. catch (IOException)
  99. {
  100. previouslyFailedImages = new List<string>();
  101. }
  102. }
  103. else
  104. {
  105. previouslyFailedImages = new List<string>();
  106. }
  107. var directoryService = new DirectoryService(_fileSystem);
  108. foreach (var video in videos)
  109. {
  110. cancellationToken.ThrowIfCancellationRequested();
  111. var key = video.Path + video.DateModified.Ticks;
  112. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  113. try
  114. {
  115. var chapters = _itemRepo.GetChapters(video);
  116. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  117. if (!success)
  118. {
  119. previouslyFailedImages.Add(key);
  120. var parentPath = Path.GetDirectoryName(failHistoryPath);
  121. Directory.CreateDirectory(parentPath);
  122. string text = string.Join("|", previouslyFailedImages);
  123. File.WriteAllText(failHistoryPath, text);
  124. }
  125. numComplete++;
  126. double percent = numComplete;
  127. percent /= videos.Count;
  128. progress.Report(100 * percent);
  129. }
  130. catch (ObjectDisposedException)
  131. {
  132. //TODO Investigate and properly fix.
  133. break;
  134. }
  135. }
  136. }
  137. public string Name => "Extract Chapter Images";
  138. public string Description => "Creates thumbnails for videos that have chapters.";
  139. public string Category => "Library";
  140. public string Key => "RefreshChapterImages";
  141. public bool IsHidden => false;
  142. public bool IsEnabled => true;
  143. public bool IsLogged => true;
  144. }
  145. }