2
0

ChapterImagesTask.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.Globalization;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.Tasks;
  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. /// <inheritdoc />
  53. public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
  54. /// <inheritdoc />
  55. public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
  56. /// <inheritdoc />
  57. public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
  58. /// <inheritdoc />
  59. public string Key => "RefreshChapterImages";
  60. /// <inheritdoc />
  61. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  62. {
  63. return new[]
  64. {
  65. new TaskTriggerInfo
  66. {
  67. Type = TaskTriggerInfo.TriggerDaily,
  68. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  69. MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
  70. }
  71. };
  72. }
  73. /// <summary>
  74. /// Returns the task to be executed.
  75. /// </summary>
  76. /// <param name="cancellationToken">The cancellation token.</param>
  77. /// <param name="progress">The progress.</param>
  78. /// <returns>Task.</returns>
  79. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  80. {
  81. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  82. {
  83. MediaTypes = new[] { MediaType.Video },
  84. IsFolder = false,
  85. Recursive = true,
  86. DtoOptions = new DtoOptions(false)
  87. {
  88. EnableImages = false
  89. },
  90. SourceTypes = new SourceType[] { SourceType.Library },
  91. HasChapterImages = false,
  92. IsVirtualItem = false
  93. })
  94. .OfType<Video>()
  95. .ToList();
  96. var numComplete = 0;
  97. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  98. List<string> previouslyFailedImages;
  99. if (File.Exists(failHistoryPath))
  100. {
  101. try
  102. {
  103. previouslyFailedImages = File.ReadAllText(failHistoryPath)
  104. .Split('|', StringSplitOptions.RemoveEmptyEntries)
  105. .ToList();
  106. }
  107. catch (IOException)
  108. {
  109. previouslyFailedImages = new List<string>();
  110. }
  111. }
  112. else
  113. {
  114. previouslyFailedImages = new List<string>();
  115. }
  116. var directoryService = new DirectoryService(_fileSystem);
  117. foreach (var video in videos)
  118. {
  119. cancellationToken.ThrowIfCancellationRequested();
  120. var key = video.Path + video.DateModified.Ticks;
  121. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  122. try
  123. {
  124. var chapters = _itemRepo.GetChapters(video);
  125. var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
  126. if (!success)
  127. {
  128. previouslyFailedImages.Add(key);
  129. var parentPath = Path.GetDirectoryName(failHistoryPath);
  130. if (parentPath != null)
  131. {
  132. Directory.CreateDirectory(parentPath);
  133. }
  134. string text = string.Join('|', previouslyFailedImages);
  135. File.WriteAllText(failHistoryPath, text);
  136. }
  137. numComplete++;
  138. double percent = numComplete;
  139. percent /= videos.Count;
  140. progress.Report(100 * percent);
  141. }
  142. catch (ObjectDisposedException)
  143. {
  144. // TODO Investigate and properly fix.
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. }