CollectionPostScanTask.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 MediaBrowser.Model.Entities;
  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 boxSets = _libraryManager.GetItemList(new InternalItemsQuery
  48. {
  49. IncludeItemTypes = new[] { nameof(BoxSet) },
  50. CollapseBoxSetItems = false,
  51. Recursive = true
  52. });
  53. var collectionNameMoviesMap = new Dictionary<string, List<Movie>>();
  54. foreach (var library in _libraryManager.RootFolder.Children.ToList()) {
  55. if (!_libraryManager.GetLibraryOptions(library).AutoCollection) {
  56. continue;
  57. }
  58. var movies = _libraryManager.GetItemList(new InternalItemsQuery
  59. {
  60. MediaTypes = new string[] { MediaType.Video },
  61. IncludeItemTypes = new[] { nameof(Movie) },
  62. IsVirtualItem = false,
  63. OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
  64. SourceTypes = new[] { SourceType.Library },
  65. Parent = library,
  66. Recursive = true
  67. });
  68. foreach (var m in movies)
  69. {
  70. if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName))
  71. {
  72. if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList))
  73. {
  74. if (!movieList.Any(m => m.Id == movie.Id))
  75. {
  76. movieList.Add(movie);
  77. }
  78. }
  79. else
  80. {
  81. collectionNameMoviesMap[movie.CollectionName] = new List<Movie> { movie };
  82. }
  83. }
  84. }
  85. }
  86. var numComplete = 0;
  87. var count = collectionNameMoviesMap.Count;
  88. foreach (var (collectionName, movieList) in collectionNameMoviesMap)
  89. {
  90. try
  91. {
  92. var boxSet = boxSets.FirstOrDefault(b => b?.Name == collectionName) as BoxSet;
  93. if (boxSet == null)
  94. {
  95. // won't automatically create collection if only one movie in it
  96. if (movieList.Count >= 2)
  97. {
  98. boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
  99. {
  100. Name = collectionName,
  101. IsLocked = true
  102. });
  103. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieList.Select(m => m.Id));
  104. }
  105. }
  106. else
  107. {
  108. await _collectionManager.AddToCollectionAsync(boxSet.Id, movieList.Select(m => m.Id));
  109. }
  110. numComplete++;
  111. double percent = numComplete;
  112. percent /= count;
  113. percent *= 100;
  114. progress.Report(percent);
  115. }
  116. catch (Exception ex)
  117. {
  118. _logger.LogError(ex, "Error refreshing {CollectionName} with {@MovieIds}", collectionName, movieList);
  119. }
  120. }
  121. progress.Report(100);
  122. }
  123. }
  124. }