CollectionPostScanTask.cs 5.6 KB

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