CollectionPostScanTask.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Database.Implementations.Enums;
  8. using MediaBrowser.Controller.Collections;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Movies;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Querying;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Server.Implementations.Library.Validators
  16. {
  17. /// <summary>
  18. /// Class CollectionPostScanTask.
  19. /// </summary>
  20. public class CollectionPostScanTask : ILibraryPostScanTask
  21. {
  22. private readonly ILibraryManager _libraryManager;
  23. private readonly ICollectionManager _collectionManager;
  24. private readonly ILogger<CollectionPostScanTask> _logger;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="CollectionPostScanTask" /> class.
  27. /// </summary>
  28. /// <param name="libraryManager">The library manager.</param>
  29. /// <param name="collectionManager">The collection manager.</param>
  30. /// <param name="logger">The logger.</param>
  31. public CollectionPostScanTask(
  32. ILibraryManager libraryManager,
  33. ICollectionManager collectionManager,
  34. ILogger<CollectionPostScanTask> logger)
  35. {
  36. _libraryManager = libraryManager;
  37. _collectionManager = collectionManager;
  38. _logger = logger;
  39. }
  40. /// <summary>
  41. /// Runs the specified progress.
  42. /// </summary>
  43. /// <param name="progress">The progress.</param>
  44. /// <param name="cancellationToken">The cancellation token.</param>
  45. /// <returns>Task.</returns>
  46. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  47. {
  48. var collectionNameMoviesMap = new Dictionary<string, HashSet<Guid>>();
  49. foreach (var library in _libraryManager.RootFolder.Children)
  50. {
  51. if (!_libraryManager.GetLibraryOptions(library).AutomaticallyAddToCollection)
  52. {
  53. continue;
  54. }
  55. var startIndex = 0;
  56. var pagesize = 1000;
  57. while (true)
  58. {
  59. var movies = _libraryManager.GetItemList(new InternalItemsQuery
  60. {
  61. MediaTypes = new[] { MediaType.Video },
  62. IncludeItemTypes = new[] { BaseItemKind.Movie },
  63. IsVirtualItem = false,
  64. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  65. Parent = library,
  66. StartIndex = startIndex,
  67. Limit = pagesize,
  68. Recursive = true
  69. });
  70. foreach (var m in movies)
  71. {
  72. if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName))
  73. {
  74. if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList))
  75. {
  76. movieList.Add(movie.Id);
  77. }
  78. else
  79. {
  80. collectionNameMoviesMap[movie.CollectionName] = new HashSet<Guid> { movie.Id };
  81. }
  82. }
  83. }
  84. if (movies.Count < pagesize)
  85. {
  86. break;
  87. }
  88. startIndex += pagesize;
  89. }
  90. }
  91. var numComplete = 0;
  92. var count = collectionNameMoviesMap.Count;
  93. if (count == 0)
  94. {
  95. progress.Report(100);
  96. return;
  97. }
  98. var boxSets = _libraryManager.GetItemList(new InternalItemsQuery
  99. {
  100. IncludeItemTypes = new[] { BaseItemKind.BoxSet },
  101. CollapseBoxSetItems = false,
  102. Recursive = true
  103. });
  104. foreach (var (collectionName, movieIds) in collectionNameMoviesMap)
  105. {
  106. try
  107. {
  108. var boxSet = boxSets.FirstOrDefault(b => b?.Name == collectionName) as BoxSet;
  109. if (boxSet is null)
  110. {
  111. // won't automatically create collection if only one movie in it
  112. if (movieIds.Count >= 2)
  113. {
  114. boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
  115. {
  116. Name = collectionName,
  117. IsLocked = true
  118. });
  119. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
  120. }
  121. }
  122. else
  123. {
  124. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
  125. }
  126. numComplete++;
  127. double percent = numComplete;
  128. percent /= count;
  129. percent *= 100;
  130. progress.Report(percent);
  131. }
  132. catch (Exception ex)
  133. {
  134. _logger.LogError(ex, "Error refreshing {CollectionName} with {@MovieIds}", collectionName, movieIds);
  135. }
  136. }
  137. progress.Report(100);
  138. }
  139. }
  140. }