aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/AudioBook/AudioBookResolver.cs
blob: 67451a639645cae06fd651882d87b96b1896cf97 (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
using System;
using System.IO;
using System.Linq;
using Emby.Naming.Common;

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

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

        public AudioBookFileInfo ParseFile(string path)
        {
            return Resolve(path, false);
        }

        public AudioBookFileInfo ParseDirectory(string path)
        {
            return Resolve(path, true);
        }

        public AudioBookFileInfo Resolve(string path, bool IsDirectory = false)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (IsDirectory) // TODO
            {
                return null;
            }

            var extension = Path.GetExtension(path) ?? string.Empty;
            // Check supported extensions
            if (!_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                return null;
            }

            var container = extension.TrimStart('.');

            var parsingResult = new AudioBookFilePathParser(_options)
                .Parse(path, IsDirectory);

            return new AudioBookFileInfo
            {
                Path = path,
                Container = container,
                PartNumber = parsingResult.PartNumber,
                ChapterNumber = parsingResult.ChapterNumber,
                IsDirectory = IsDirectory
            };
        }
    }
}