ChapterImagesTask.cs 6.2 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 Jellyfin.Data.Enums;
  8. using Jellyfin.Extensions;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Controller.Dto;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.Controller.Persistence;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Globalization;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.Tasks;
  20. using Microsoft.Extensions.Logging;
  21. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  22. {
  23. /// <summary>
  24. /// Class ChapterImagesTask.
  25. /// </summary>
  26. public class ChapterImagesTask : IScheduledTask
  27. {
  28. private readonly ILogger<ChapterImagesTask> _logger;
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly IItemRepository _itemRepo;
  31. private readonly IApplicationPaths _appPaths;
  32. private readonly IEncodingManager _encodingManager;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly ILocalizationManager _localization;
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
  37. /// </summary>
  38. /// <param name="logger">The logger.</param>.
  39. /// <param name="libraryManager">The library manager.</param>.
  40. /// <param name="itemRepo">The item repository.</param>
  41. /// <param name="appPaths">The application paths.</param>
  42. /// <param name="encodingManager">The encoding manager.</param>
  43. /// <param name="fileSystem">The filesystem.</param>
  44. /// <param name="localization">The localization manager.</param>
  45. public ChapterImagesTask(
  46. ILogger<ChapterImagesTask> logger,
  47. ILibraryManager libraryManager,
  48. IItemRepository itemRepo,
  49. IApplicationPaths appPaths,
  50. IEncodingManager encodingManager,
  51. IFileSystem fileSystem,
  52. ILocalizationManager localization)
  53. {
  54. _logger = logger;
  55. _libraryManager = libraryManager;
  56. _itemRepo = itemRepo;
  57. _appPaths = appPaths;
  58. _encodingManager = encodingManager;
  59. _fileSystem = fileSystem;
  60. _localization = localization;
  61. }
  62. /// <inheritdoc />
  63. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  64. /// <inheritdoc />
  65. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  66. /// <inheritdoc />
  67. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  68. /// <inheritdoc />
  69. public string Key => "RefreshChapterImages";
  70. /// <inheritdoc />
  71. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  72. {
  73. return new[]
  74. {
  75. new TaskTriggerInfo
  76. {
  77. Type = TaskTriggerInfo.TriggerDaily,
  78. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  79. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  80. }
  81. };
  82. }
  83. /// <inheritdoc />
  84. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  85. {
  86. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  87. {
  88. MediaTypes = new[] { MediaType.Video },
  89. IsFolder = false,
  90. Recursive = true,
  91. DtoOptions = new DtoOptions(false)
  92. {
  93. EnableImages = false
  94. },
  95. SourceTypes = new SourceType[] { SourceType.Library },
  96. IsVirtualItem = false
  97. })
  98. .OfType<Video>()
  99. .ToList();
  100. var numComplete = 0;
  101. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  102. List<string> previouslyFailedImages;
  103. if (File.Exists(failHistoryPath))
  104. {
  105. try
  106. {
  107. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  108. .Split('|', StringSplitOptions.RemoveEmptyEntries)
  109. .ToList();
  110. }
  111. catch (IOException)
  112. {
  113. previouslyFailedImages = new List<string>();
  114. }
  115. }
  116. else
  117. {
  118. previouslyFailedImages = new List<string>();
  119. }
  120. var directoryService = new DirectoryService(_fileSystem);
  121. foreach (var video in videos)
  122. {
  123. cancellationToken.ThrowIfCancellationRequested();
  124. var key = video.Path + video.DateModified.Ticks;
  125. var extract = !previouslyFailedImages.Contains(key, StringComparison.OrdinalIgnoreCase);
  126. try
  127. {
  128. var chapters = _itemRepo.GetChapters(video);
  129. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  130. if (!success)
  131. {
  132. previouslyFailedImages.Add(key);
  133. var parentPath = Path.GetDirectoryName(failHistoryPath);
  134. if (parentPath is not null)
  135. {
  136. Directory.CreateDirectory(parentPath);
  137. }
  138. string text = string.Join('|', previouslyFailedImages);
  139. File.WriteAllText(failHistoryPath, text);
  140. }
  141. numComplete++;
  142. double percent = numComplete;
  143. percent /= videos.Count;
  144. progress.Report(100 * percent);
  145. }
  146. catch (ObjectDisposedException ex)
  147. {
  148. // TODO Investigate and properly fix.
  149. _logger.LogError(ex, "Object Disposed");
  150. break;
  151. }
  152. }
  153. }
  154. }
  155. }