aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/AudioBook/AudioBookListResolver.cs
blob: 414ef11830c3d8c5f98c0f01339f9438fe2d69dd (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
using System.Collections.Generic;
using System.Linq;
using Emby.Naming.Common;
using Emby.Naming.Video;
using MediaBrowser.Model.IO;

namespace Emby.Naming.AudioBook
{
    public class AudioBookListResolver
    {
        private readonly NamingOptions _options;

        public AudioBookListResolver(NamingOptions options)
        {
            _options = options;
        }

        public IEnumerable<AudioBookInfo> Resolve(IEnumerable<FileSystemMetadata> files)
        {
            var audioBookResolver = new AudioBookResolver(_options);

            var audiobookFileInfos = files
                .Select(i => audioBookResolver.Resolve(i.FullName, i.IsDirectory))
                .Where(i => i != null)
                .ToList();

            // Filter out all extras, otherwise they could cause stacks to not be resolved
            // See the unit test TestStackedWithTrailer
            var metadata = audiobookFileInfos
                .Select(i => new FileSystemMetadata
                {
                    FullName = i.Path,
                    IsDirectory = i.IsDirectory
                });

            var stackResult = new StackResolver(_options)
                .ResolveAudioBooks(metadata);

            var list = new List<AudioBookInfo>();

            foreach (var stack in stackResult.Stacks)
            {
                var stackFiles = stack.Files.Select(i => audioBookResolver.Resolve(i, stack.IsDirectoryStack)).ToList();
                stackFiles.Sort();
                var info = new AudioBookInfo
                {
                    Files = stackFiles,
                    Name = stack.Name
                };
                list.Add(info);
            }

            // Whatever files are left, just add them
            /*list.AddRange(remainingFiles.Select(i => new AudioBookInfo
            {
                Files = new List<AudioBookFileInfo> { i },
                Name = i.,
                Year = i.Year
            }));*/

            var orderedList = list.OrderBy(i => i.Name);

            return orderedList;
        }
    }
}