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.Extensions;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Controller.Dto;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Controller.Persistence;
  14. using MediaBrowser.Controller.Providers;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Globalization;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Tasks;
  19. using Microsoft.Extensions.Logging;
  20. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  21. {
  22. /// <summary>
  23. /// Class ChapterImagesTask.
  24. /// </summary>
  25. public class ChapterImagesTask : IScheduledTask
  26. {
  27. private readonly ILogger _logger;
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly IItemRepository _itemRepo;
  30. private readonly IApplicationPaths _appPaths;
  31. private readonly IEncodingManager _encodingManager;
  32. private readonly IFileSystem _fileSystem;
  33. private readonly ILocalizationManager _localization;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
  36. /// </summary>
  37. /// <param name="logger">The logger.</param>.
  38. /// <param name="libraryManager">The library manager.</param>.
  39. /// <param name="itemRepo">The item repository.</param>
  40. /// <param name="appPaths">The application paths.</param>
  41. /// <param name="encodingManager">The encoding manager.</param>
  42. /// <param name="fileSystem">The filesystem.</param>
  43. /// <param name="localization">The localization manager.</param>
  44. public ChapterImagesTask(
  45. ILogger<ChapterImagesTask> logger,
  46. ILibraryManager libraryManager,
  47. IItemRepository itemRepo,
  48. IApplicationPaths appPaths,
  49. IEncodingManager encodingManager,
  50. IFileSystem fileSystem,
  51. ILocalizationManager localization)
  52. {
  53. _logger = logger;
  54. _libraryManager = libraryManager;
  55. _itemRepo = itemRepo;
  56. _appPaths = appPaths;
  57. _encodingManager = encodingManager;
  58. _fileSystem = fileSystem;
  59. _localization = localization;
  60. }
  61. /// <inheritdoc />
  62. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  63. /// <inheritdoc />
  64. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  65. /// <inheritdoc />
  66. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  67. /// <inheritdoc />
  68. public string Key => "RefreshChapterImages";
  69. /// <inheritdoc />
  70. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  71. {
  72. return new[]
  73. {
  74. new TaskTriggerInfo
  75. {
  76. Type = TaskTriggerInfo.TriggerDaily,
  77. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  78. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  79. }
  80. };
  81. }
  82. /// <inheritdoc />
  83. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  84. {
  85. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  86. {
  87. MediaTypes = new[] { MediaType.Video },
  88. IsFolder = false,
  89. Recursive = true,
  90. DtoOptions = new DtoOptions(false)
  91. {
  92. EnableImages = false
  93. },
  94. SourceTypes = new SourceType[] { SourceType.Library },
  95. HasChapterImages = false,
  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 != 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. }