aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Entities/ISupportsSpecialFeatures.cs
blob: 9b52c2f7fca7fc1eeb71ba39367f38c434df09fd (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
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Win32;
using MediaBrowser.Controller.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace MediaBrowser.Controller.Entities
{
    /// <summary>
    /// Allows some code sharing between entities that support special features
    /// </summary>
    public interface ISupportsSpecialFeatures
    {
        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <value>The path.</value>
        string Path { get; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        string Name { get; }

        /// <summary>
        /// Gets the resolve args.
        /// </summary>
        /// <value>The resolve args.</value>
        ItemResolveArgs ResolveArgs { get; }

        /// <summary>
        /// Gets the special features.
        /// </summary>
        /// <value>The special features.</value>
        List<Video> SpecialFeatures { get; }
    }

    /// <summary>
    /// Class SpecialFeatures
    /// </summary>
    public static class SpecialFeatures
    {
        /// <summary>
        /// Loads special features from the file system
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns>List{Video}.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static IEnumerable<Video> LoadSpecialFeatures(ISupportsSpecialFeatures entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            WIN32_FIND_DATA? folder;

            try
            {
                folder = entity.ResolveArgs.GetFileSystemEntryByName("specials");
            }
            catch (IOException ex)
            {
                Logger.LogException("Error getting ResolveArgs for {0}", ex, entity.Path);
                return new List<Video> { };
            }

            // Path doesn't exist. No biggie
            if (folder == null)
            {
                return new List<Video> {};
            }

            IEnumerable<WIN32_FIND_DATA> files;

            try
            {
                files = FileSystem.GetFiles(folder.Value.Path);
            }
            catch (IOException ex)
            {
                Logger.LogException("Error loading trailers for {0}", ex, entity.Name);
                return new List<Video> { };
            }

            return Kernel.Instance.LibraryManager.GetItems<Video>(files, null).Select(video =>
            {
                // Try to retrieve it from the db. If we don't find it, use the resolved version
                var dbItem = Kernel.Instance.ItemRepository.RetrieveItem(video.Id) as Video;

                if (dbItem != null)
                {
                    dbItem.ResolveArgs = video.ResolveArgs;
                    video = dbItem;
                }

                return video;
            });
        }
    }
}