ChapterImagesTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. using MediaBrowser.Model.Globalization;
  19. namespace Emby.Server.Implementations.ScheduledTasks
  20. {
  21. /// <summary>
  22. /// Class ChapterImagesTask.
  23. /// </summary>
  24. public class ChapterImagesTask : IScheduledTask
  25. {
  26. /// <summary>
  27. /// The _logger.
  28. /// </summary>
  29. private readonly ILogger _logger;
  30. /// <summary>
  31. /// The _library manager.
  32. /// </summary>
  33. private readonly ILibraryManager _libraryManager;
  34. private readonly IItemRepository _itemRepo;
  35. private readonly IApplicationPaths _appPaths;
  36. private readonly IEncodingManager _encodingManager;
  37. private readonly IFileSystem _fileSystem;
  38. private readonly ILocalizationManager _localization;
  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. {
  58. new TaskTriggerInfo
  59. {
  60. Type = TaskTriggerInfo.TriggerDaily,
  61. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  62. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  63. }
  64. };
  65. }
  66. /// <summary>
  67. /// Returns the task to be executed.
  68. /// </summary>
  69. /// <param name="cancellationToken">The cancellation token.</param>
  70. /// <param name="progress">The progress.</param>
  71. /// <returns>Task.</returns>
  72. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  73. {
  74. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  75. {
  76. MediaTypes = new[] { MediaType.Video },
  77. IsFolder = false,
  78. Recursive = true,
  79. DtoOptions = new DtoOptions(false)
  80. {
  81. EnableImages = false
  82. },
  83. SourceTypes = new SourceType[] { SourceType.Library },
  84. HasChapterImages = false,
  85. IsVirtualItem = false
  86. })
  87. .OfType<Video>()
  88. .ToList();
  89. var numComplete = 0;
  90. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  91. List<string> previouslyFailedImages;
  92. if (File.Exists(failHistoryPath))
  93. {
  94. try
  95. {
  96. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  97. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  98. .ToList();
  99. }
  100. catch (IOException)
  101. {
  102. previouslyFailedImages = new List<string>();
  103. }
  104. }
  105. else
  106. {
  107. previouslyFailedImages = new List<string>();
  108. }
  109. var directoryService = new DirectoryService(_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 = Path.GetDirectoryName(failHistoryPath);
  123. Directory.CreateDirectory(parentPath);
  124. string text = string.Join("|", previouslyFailedImages);
  125. File.WriteAllText(failHistoryPath, text);
  126. }
  127. numComplete++;
  128. double percent = numComplete;
  129. percent /= videos.Count;
  130. progress.Report(100 * percent);
  131. }
  132. catch (ObjectDisposedException)
  133. {
  134. //TODO Investigate and properly fix.
  135. break;
  136. }
  137. }
  138. }
  139. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  140. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  141. public string Category => _localization.GetLocalizedString("TasksLibrary");
  142. public string Key => "RefreshChapterImages";
  143. public bool IsHidden => false;
  144. public bool IsEnabled => true;
  145. public bool IsLogged => true;
  146. }
  147. }