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. 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. /// <summary>
  64. /// Returns the task to be executed
  65. /// </summary>
  66. /// <param name="cancellationToken">The cancellation token.</param>
  67. /// <param name="progress">The progress.</param>
  68. /// <returns>Task.</returns>
  69. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  70. {
  71. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  72. {
  73. MediaTypes = new[] { MediaType.Video },
  74. IsFolder = false,
  75. Recursive = true,
  76. DtoOptions = new DtoOptions(false)
  77. {
  78. EnableImages = false
  79. },
  80. SourceTypes = new SourceType[] { SourceType.Library },
  81. HasChapterImages = false,
  82. IsVirtualItem = false
  83. })
  84. .OfType<Video>()
  85. .ToList();
  86. var numComplete = 0;
  87. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  88. List<string> previouslyFailedImages;
  89. if (File.Exists(failHistoryPath))
  90. {
  91. try
  92. {
  93. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  94. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  95. .ToList();
  96. }
  97. catch (IOException)
  98. {
  99. previouslyFailedImages = new List<string>();
  100. }
  101. }
  102. else
  103. {
  104. previouslyFailedImages = new List<string>();
  105. }
  106. var directoryService = new DirectoryService(_logger, _fileSystem);
  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);
  115. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  116. if (!success)
  117. {
  118. previouslyFailedImages.Add(key);
  119. var parentPath = Path.GetDirectoryName(failHistoryPath);
  120. Directory.CreateDirectory(parentPath);
  121. string text = string.Join("|", previouslyFailedImages);
  122. File.WriteAllText(failHistoryPath, text);
  123. }
  124. numComplete++;
  125. double percent = numComplete;
  126. percent /= videos.Count;
  127. progress.Report(100 * percent);
  128. }
  129. catch (ObjectDisposedException)
  130. {
  131. //TODO Investigate and properly fix.
  132. break;
  133. }
  134. }
  135. }
  136. public string Name => "Chapter image extraction";
  137. public string Description => "Creates thumbnails for videos that have chapters.";
  138. public string Category => "Library";
  139. public string Key => "RefreshChapterImages";
  140. public bool IsHidden => false;
  141. public bool IsEnabled => true;
  142. public bool IsLogged => true;
  143. }
  144. }