CollectionPostScanTask.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. using MediaBrowser.Controller.Collections;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Entities.Movies;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Querying;
  11. using Jellyfin.Data.Enums;
  12. using Microsoft.Extensions.Logging;
  13. using System.Diagnostics;
  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 movies = _libraryManager.GetItemList(new InternalItemsQuery
  48. {
  49. IncludeItemTypes = new[] { nameof(Movie) },
  50. IsVirtualItem = false,
  51. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  52. Recursive = true
  53. });
  54. var boxSets = _libraryManager.GetItemList(new InternalItemsQuery
  55. {
  56. IncludeItemTypes = new[] { nameof(BoxSet) },
  57. CollapseBoxSetItems = false,
  58. Recursive = true
  59. });
  60. var numComplete = 0;
  61. var count = movies.Count;
  62. var collectionNameMoviesMap = new Dictionary<string, List<Movie>>();
  63. foreach (var m in movies)
  64. {
  65. if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName))
  66. {
  67. if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList))
  68. {
  69. if (!movieList.Any(m => m.Id == movie.Id))
  70. {
  71. movieList.Add(movie);
  72. }
  73. }
  74. else
  75. {
  76. collectionNameMoviesMap[movie.CollectionName] = new List<Movie> { movie };
  77. }
  78. }
  79. numComplete++;
  80. double percent = numComplete;
  81. percent /= count * 2;
  82. percent *= 100;
  83. progress.Report(percent);
  84. }
  85. foreach (var (collectionName, movieList) in collectionNameMoviesMap)
  86. {
  87. try
  88. {
  89. var boxSet = boxSets.FirstOrDefault(b => b?.Name == collectionName) as BoxSet;
  90. if (boxSet == null)
  91. {
  92. // won't automatically create collection if only one movie in it
  93. if (movieList.Count >= 2)
  94. {
  95. var movieIds = FliterMoviesByOption(movieList);
  96. if (movieIds.Count >= 2) {
  97. // at least 2 movies have AutoCollection option enable
  98. boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
  99. {
  100. Name = collectionName,
  101. IsLocked = true
  102. });
  103. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
  104. }
  105. }
  106. }
  107. else
  108. {
  109. var movieIds = FliterMoviesByOption(movieList);
  110. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
  111. }
  112. numComplete++;
  113. double percent = numComplete;
  114. percent /= count * 2;
  115. percent *= 100;
  116. progress.Report(percent);
  117. }
  118. catch (Exception ex)
  119. {
  120. _logger.LogError(ex, "Error refreshing {CollectionName} with {@MovieIds}", collectionName, movieList);
  121. }
  122. }
  123. progress.Report(100);
  124. }
  125. private List<Guid> FliterMoviesByOption(List<Movie> movieList) {
  126. List<Guid> movieIds = new List<Guid>();
  127. foreach (var movie in movieList)
  128. {
  129. if (_libraryManager.GetLibraryOptions(movie).AutoCollection)
  130. {
  131. movieIds.Add(movie.Id);
  132. }
  133. }
  134. return movieIds;
  135. }
  136. }
  137. }