ChapterImagesTask.cs 5.7 KB

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