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

namespace Emby.Naming.AudioBook
{
    /// <summary>
    /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
    /// </summary>
    public class AudioBookResolver
    {
        private readonly NamingOptions _options;

        /// <summary>
        /// Initializes a new instance of the <see cref="AudioBookResolver"/> class.
        /// </summary>
        /// <param name="options"><see cref="NamingOptions"/> containing AudioFileExtensions and also used to pass to AudioBookFilePathParser.</param>
        public AudioBookResolver(NamingOptions options)
        {
            _options = options;
        }

        /// <summary>
        /// Resolve specifics (path, container, partNumber, chapterNumber) about audiobook file.
        /// </summary>
        /// <param name="path">Path to audiobook file.</param>
        /// <returns>Returns <see cref="AudioBookResolver"/> object.</returns>
        public AudioBookFileInfo? Resolve(string path)
        {
            if (path.Length == 0 || Path.GetFileNameWithoutExtension(path).Length == 0)
            {
                // Return null to indicate this path will not be used, instead of stopping whole process with exception
                return null;
            }

            var extension = Path.GetExtension(path);

            // Check supported extensions
            if (!_options.AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
            {
                return null;
            }

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

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

            return new AudioBookFileInfo(
                path,
                container,
                chapterNumber: parsingResult.ChapterNumber,
                partNumber: parsingResult.PartNumber);
        }
    }
}