ChapterImagesTask.cs 6.0 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. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  20. {
  21. /// <summary>
  22. /// Class ChapterImagesTask.
  23. /// </summary>
  24. public class ChapterImagesTask : IScheduledTask
  25. {
  26. /// <summary>
  27. /// The _library manager.
  28. /// </summary>
  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="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. ILibraryManager libraryManager,
  46. IItemRepository itemRepo,
  47. IApplicationPaths appPaths,
  48. IEncodingManager encodingManager,
  49. IFileSystem fileSystem,
  50. ILocalizationManager localization)
  51. {
  52. _libraryManager = libraryManager;
  53. _itemRepo = itemRepo;
  54. _appPaths = appPaths;
  55. _encodingManager = encodingManager;
  56. _fileSystem = fileSystem;
  57. _localization = localization;
  58. }
  59. /// <inheritdoc />
  60. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  61. /// <inheritdoc />
  62. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  63. /// <inheritdoc />
  64. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  65. /// <inheritdoc />
  66. public string Key => "RefreshChapterImages";
  67. /// <inheritdoc />
  68. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  69. {
  70. return new[]
  71. {
  72. new TaskTriggerInfo
  73. {
  74. Type = TaskTriggerInfo.TriggerDaily,
  75. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  76. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  77. }
  78. };
  79. }
  80. /// <inheritdoc />
  81. public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  82. {
  83. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  84. {
  85. MediaTypes = new[] { MediaType.Video },
  86. IsFolder = false,
  87. Recursive = true,
  88. DtoOptions = new DtoOptions(false)
  89. {
  90. EnableImages = false
  91. },
  92. SourceTypes = new SourceType[] { SourceType.Library },
  93. HasChapterImages = false,
  94. IsVirtualItem = false
  95. })
  96. .OfType<Video>()
  97. .ToList();
  98. var numComplete = 0;
  99. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  100. List<string> previouslyFailedImages;
  101. if (File.Exists(failHistoryPath))
  102. {
  103. try
  104. {
  105. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  106. .Split('|', StringSplitOptions.RemoveEmptyEntries)
  107. .ToList();
  108. }
  109. catch (IOException)
  110. {
  111. previouslyFailedImages = new List<string>();
  112. }
  113. }
  114. else
  115. {
  116. previouslyFailedImages = new List<string>();
  117. }
  118. var directoryService = new DirectoryService(_fileSystem);
  119. foreach (var video in videos)
  120. {
  121. cancellationToken.ThrowIfCancellationRequested();
  122. var key = video.Path + video.DateModified.Ticks;
  123. var extract = !previouslyFailedImages.Contains(key, StringComparison.OrdinalIgnoreCase);
  124. try
  125. {
  126. var chapters = _itemRepo.GetChapters(video);
  127. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  128. if (!success)
  129. {
  130. previouslyFailedImages.Add(key);
  131. var parentPath = Path.GetDirectoryName(failHistoryPath);
  132. if (parentPath != null)
  133. {
  134. Directory.CreateDirectory(parentPath);
  135. }
  136. string text = string.Join('|', previouslyFailedImages);
  137. File.WriteAllText(failHistoryPath, text);
  138. }
  139. numComplete++;
  140. double percent = numComplete;
  141. percent /= videos.Count;
  142. progress.Report(100 * percent);
  143. }
  144. catch (ObjectDisposedException)
  145. {
  146. // TODO Investigate and properly fix.
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. }