ChapterImagesTask.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.MediaEncoding;
  12. using MediaBrowser.Controller.Persistence;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Tasks;
  17. using MediaBrowser.Model.Globalization;
  18. namespace Emby.Server.Implementations.ScheduledTasks
  19. {
  20. /// <summary>
  21. /// Class ChapterImagesTask.
  22. /// </summary>
  23. public class ChapterImagesTask : IScheduledTask
  24. {
  25. /// <summary>
  26. /// The _library manager.
  27. /// </summary>
  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. public ChapterImagesTask(
  38. ILibraryManager libraryManager,
  39. IItemRepository itemRepo,
  40. IApplicationPaths appPaths,
  41. IEncodingManager encodingManager,
  42. IFileSystem fileSystem,
  43. ILocalizationManager localization)
  44. {
  45. _libraryManager = libraryManager;
  46. _itemRepo = itemRepo;
  47. _appPaths = appPaths;
  48. _encodingManager = encodingManager;
  49. _fileSystem = fileSystem;
  50. _localization = localization;
  51. }
  52. /// <summary>
  53. /// Creates the triggers that define when the task will run.
  54. /// </summary>
  55. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  56. {
  57. return new[]
  58. {
  59. new TaskTriggerInfo
  60. {
  61. Type = TaskTriggerInfo.TriggerDaily,
  62. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  63. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  64. }
  65. };
  66. }
  67. /// <summary>
  68. /// Returns the task to be executed.
  69. /// </summary>
  70. /// <param name="cancellationToken">The cancellation token.</param>
  71. /// <param name="progress">The progress.</param>
  72. /// <returns>Task.</returns>
  73. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  74. {
  75. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  76. {
  77. MediaTypes = new[] { MediaType.Video },
  78. IsFolder = false,
  79. Recursive = true,
  80. DtoOptions = new DtoOptions(false)
  81. {
  82. EnableImages = false
  83. },
  84. SourceTypes = new SourceType[] { SourceType.Library },
  85. HasChapterImages = false,
  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 = File.ReadAllText(failHistoryPath)
  98. .Split('|', StringSplitOptions.RemoveEmptyEntries)
  99. .ToList();
  100. }
  101. catch (IOException)
  102. {
  103. previouslyFailedImages = new List<string>();
  104. }
  105. }
  106. else
  107. {
  108. previouslyFailedImages = new List<string>();
  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, StringComparer.OrdinalIgnoreCase);
  116. try
  117. {
  118. var chapters = _itemRepo.GetChapters(video);
  119. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  120. if (!success)
  121. {
  122. previouslyFailedImages.Add(key);
  123. var parentPath = Path.GetDirectoryName(failHistoryPath);
  124. Directory.CreateDirectory(parentPath);
  125. string text = string.Join("|", previouslyFailedImages);
  126. File.WriteAllText(failHistoryPath, text);
  127. }
  128. numComplete++;
  129. double percent = numComplete;
  130. percent /= videos.Count;
  131. progress.Report(100 * percent);
  132. }
  133. catch (ObjectDisposedException)
  134. {
  135. // TODO Investigate and properly fix.
  136. break;
  137. }
  138. }
  139. }
  140. /// <inheritdoc />
  141. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  142. /// <inheritdoc />
  143. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  144. /// <inheritdoc />
  145. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  146. /// <inheritdoc />
  147. public string Key => "RefreshChapterImages";
  148. /// <inheritdoc />
  149. public bool IsHidden => false;
  150. /// <inheritdoc />
  151. public bool IsEnabled => true;
  152. /// <inheritdoc />
  153. public bool IsLogged => true;
  154. }
  155. }