CollectionPostScanTask.cs 5.1 KB

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