aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Library/Validators/CollectionPostScanTask.cs
blob: 89f64ee4f0e29dcf0a31627ae287c772c37354cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Collections;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using Microsoft.Extensions.Logging;

namespace Emby.Server.Implementations.Library.Validators
{
    /// <summary>
    /// Class CollectionPostScanTask.
    /// </summary>
    public class CollectionPostScanTask : ILibraryPostScanTask
    {
        private readonly ILibraryManager _libraryManager;
        private readonly ICollectionManager _collectionManager;
        private readonly ILogger<CollectionPostScanTask> _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionPostScanTask" /> class.
        /// </summary>
        /// <param name="libraryManager">The library manager.</param>
        /// <param name="collectionManager">The collection manager.</param>
        /// <param name="logger">The logger.</param>
        public CollectionPostScanTask(
            ILibraryManager libraryManager,
            ICollectionManager collectionManager,
            ILogger<CollectionPostScanTask> logger)
        {
            _libraryManager = libraryManager;
            _collectionManager = collectionManager;
            _logger = logger;
        }

        /// <summary>
        /// Runs the specified progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var collectionNameMoviesMap = new Dictionary<string, HashSet<Guid>>();

            foreach (var library in _libraryManager.RootFolder.Children)
            {
                if (!_libraryManager.GetLibraryOptions(library).AutomaticallyAddToCollection)
                {
                    continue;
                }

                var startIndex = 0;
                var pagesize = 1000;

                while (true)
                {
                    var movies = _libraryManager.GetItemList(new InternalItemsQuery
                    {
                        MediaTypes = new[] { MediaType.Video },
                        IncludeItemTypes = new[] { BaseItemKind.Movie },
                        IsVirtualItem = false,
                        OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
                        Parent = library,
                        StartIndex = startIndex,
                        Limit = pagesize,
                        Recursive = true
                    });

                    foreach (var m in movies)
                    {
                        if (m is Movie movie && !string.IsNullOrEmpty(movie.CollectionName))
                        {
                            if (collectionNameMoviesMap.TryGetValue(movie.CollectionName, out var movieList))
                            {
                                movieList.Add(movie.Id);
                            }
                            else
                            {
                                collectionNameMoviesMap[movie.CollectionName] = new HashSet<Guid> { movie.Id };
                            }
                        }
                    }

                    if (movies.Count < pagesize)
                    {
                        break;
                    }

                    startIndex += pagesize;
                }
            }

            var numComplete = 0;
            var count = collectionNameMoviesMap.Count;

            if (count == 0)
            {
                progress.Report(100);
                return;
            }

            var boxSets = _libraryManager.GetItemList(new InternalItemsQuery
            {
                IncludeItemTypes = new[] { BaseItemKind.BoxSet },
                CollapseBoxSetItems = false,
                Recursive = true
            });

            foreach (var (collectionName, movieIds) in collectionNameMoviesMap)
            {
                try
                {
                    var boxSet = boxSets.FirstOrDefault(b => b?.Name == collectionName) as BoxSet;
                    if (boxSet is null)
                    {
                        // won't automatically create collection if only one movie in it
                        if (movieIds.Count >= 2)
                        {
                            boxSet = await _collectionManager.CreateCollectionAsync(new CollectionCreationOptions
                            {
                                Name = collectionName,
                                IsLocked = true
                            });

                            await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
                        }
                    }
                    else
                    {
                        await _collectionManager.AddToCollectionAsync(boxSet.Id, movieIds);
                    }

                    numComplete++;
                    double percent = numComplete;
                    percent /= count;
                    percent *= 100;

                    progress.Report(percent);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error refreshing {CollectionName} with {@MovieIds}", collectionName, movieIds);
                }
            }

            progress.Report(100);
        }
    }
}