ChapterImagesTask.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.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<ChapterImagesTask> _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">Instance of the <see cref="ILogger"/> interface.</param>
  38. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  39. /// <param name="itemRepo">Instance of the <see cref="IItemRepository"/> interface.</param>
  40. /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
  41. /// <param name="encodingManager">Instance of the <see cref="IEncodingManager"/> interface.</param>
  42. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  43. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</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
  73. [
  74. new TaskTriggerInfo
  75. {
  76. Type = TaskTriggerInfoType.DailyTrigger,
  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 = [MediaType.Video],
  88. IsFolder = false,
  89. Recursive = true,
  90. DtoOptions = new DtoOptions(false)
  91. {
  92. EnableImages = false
  93. },
  94. SourceTypes = [SourceType.Library],
  95. IsVirtualItem = false
  96. })
  97. .OfType<Video>()
  98. .ToList();
  99. var numComplete = 0;
  100. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  101. List<string> previouslyFailedImages;
  102. if (File.Exists(failHistoryPath))
  103. {
  104. try
  105. {
  106. previouslyFailedImages = (await File.ReadAllTextAsync(failHistoryPath, cancellationToken).ConfigureAwait(false))
  107. .Split('|', StringSplitOptions.RemoveEmptyEntries)
  108. .ToList();
  109. }
  110. catch (IOException)
  111. {
  112. previouslyFailedImages = new List<string>();
  113. }
  114. }
  115. else
  116. {
  117. previouslyFailedImages = new List<string>();
  118. }
  119. var directoryService = new DirectoryService(_fileSystem);
  120. foreach (var video in videos)
  121. {
  122. cancellationToken.ThrowIfCancellationRequested();
  123. var key = video.Path + video.DateModified.Ticks;
  124. var extract = !previouslyFailedImages.Contains(key, StringComparison.OrdinalIgnoreCase);
  125. try
  126. {
  127. var chapters = _itemRepo.GetChapters(video);
  128. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  129. if (!success)
  130. {
  131. previouslyFailedImages.Add(key);
  132. var parentPath = Path.GetDirectoryName(failHistoryPath);
  133. if (parentPath is not null)
  134. {
  135. Directory.CreateDirectory(parentPath);
  136. }
  137. string text = string.Join('|', previouslyFailedImages);
  138. await File.WriteAllTextAsync(failHistoryPath, text, cancellationToken).ConfigureAwait(false);
  139. }
  140. numComplete++;
  141. double percent = numComplete;
  142. percent /= videos.Count;
  143. progress.Report(100 * percent);
  144. }
  145. catch (ObjectDisposedException ex)
  146. {
  147. // TODO Investigate and properly fix.
  148. _logger.LogError(ex, "Object Disposed");
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. }