aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Library/Resolvers/BaseVideoResolver.cs
blob: ba2a32c13fd12d435f76452898bc31328b40b91e (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
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Naming.Audio;
using MediaBrowser.Naming.Video;
using System;

namespace MediaBrowser.Server.Implementations.Library.Resolvers
{
    /// <summary>
    /// Resolves a Path into a Video or Video subclass
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseVideoResolver<T> : Controller.Resolvers.ItemResolver<T>
        where T : Video, new()
    {
        protected readonly ILibraryManager LibraryManager;

        protected BaseVideoResolver(ILibraryManager libraryManager)
        {
            LibraryManager = libraryManager;
        }

        /// <summary>
        /// Resolves the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        /// <returns>`0.</returns>
        protected override T Resolve(ItemResolveArgs args)
        {
            return ResolveVideo<T>(args);
        }

        /// <summary>
        /// Resolves the video.
        /// </summary>
        /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
        /// <param name="args">The args.</param>
        /// <returns>``0.</returns>
        protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args)
              where TVideoType : Video, new()
        {
            // If the path is a file check for a matching extensions
            if (!args.IsDirectory)
            {
                var parser = new Naming.Video.VideoResolver(new ExpandedVideoOptions(), new AudioOptions(), new Naming.Logging.NullLogger());
                var videoInfo = parser.ResolveFile(args.Path);

                if (videoInfo == null)
                {
                    return null;
                }

                var isShortcut = string.Equals(videoInfo.Container, "strm", StringComparison.OrdinalIgnoreCase);

                if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub || isShortcut)
                {
                    var type = string.Equals(videoInfo.Container, "iso", StringComparison.OrdinalIgnoreCase) || string.Equals(videoInfo.Container, "img", StringComparison.OrdinalIgnoreCase) ?
                        VideoType.Iso : 
                        VideoType.VideoFile;

                    var path = args.Path;

                    var video = new TVideoType
                    {
                        VideoType = type,
                        Path = path,
                        IsInMixedFolder = true,
                        IsPlaceHolder = videoInfo.IsStub,
                        IsShortcut = isShortcut,
                        Name = videoInfo.Name,
                        ProductionYear = videoInfo.Year
                    };

                    if (videoInfo.IsStub)
                    {
                        if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
                        {
                            video.VideoType = VideoType.Dvd;
                        }
                        else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase))
                        {
                            video.VideoType = VideoType.HdDvd;
                        }
                        else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
                        {
                            video.VideoType = VideoType.BluRay;
                        }
                    }

                    return video;
                }
            }

            return null;
        }
    }
}