2
0

ChapterImagesTask.cs 6.2 KB

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