aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Resolvers/BaseItemResolver.cs
blob: 200a2444dfcd09bb3221ba484d9c416aa42a7f45 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MediaBrowser.Controller.Events;
using MediaBrowser.Model.Entities;

namespace MediaBrowser.Controller.Resolvers
{
    public abstract class BaseItemResolver<T> : IBaseItemResolver
        where T : BaseItem, new ()
    {
        protected virtual T Resolve(ItemResolveEventArgs args)
        {
            return null;
        }

        /// <summary>
        /// Sets initial values on the newly resolved item
        /// </summary>
        protected virtual void SetItemValues(T item, ItemResolveEventArgs args)
        {
            // If the subclass didn't specify this
            if (string.IsNullOrEmpty(item.Path))
            {
                item.Path = args.Path;
            }

            // If the subclass didn't specify this
            if (args.Parent != null)
            {
                item.Parent = args.Parent;
            }

            item.Id = Kernel.GetMD5(item.Path);
            
            PopulateImages(item, args);
            PopulateLocalTrailers(item, args);
        }

        public BaseItem ResolvePath(ItemResolveEventArgs args)
        {
            T item = Resolve(args);
            
            if (item != null)
            {
                // Set initial values on the newly resolved item
                
                SetItemValues(item, args);

                // Make sure the item has a name
                EnsureName(item);

                // Make sure DateCreated and DateModified have values
                EnsureDates(item);
            }

            return item;
        }

        private void EnsureName(T item)
        {
            // If the subclass didn't supply a name, add it here
            if (string.IsNullOrEmpty(item.Name))
            {
                item.Name = Path.GetFileNameWithoutExtension(item.Path);
            }

        }

        /// <summary>
        /// Ensures DateCreated and DateModified have values
        /// </summary>
        private void EnsureDates(T item)
        {
            // If the subclass didn't supply dates, add them here
            if (item.DateCreated == DateTime.MinValue)
            {
                item.DateCreated = Path.IsPathRooted(item.Path) ? File.GetCreationTime(item.Path) : DateTime.Now;
            }

            if (item.DateModified == DateTime.MinValue)
            {
                item.DateModified = Path.IsPathRooted(item.Path) ? File.GetLastWriteTime(item.Path) : DateTime.Now;
            }
        }

        /// <summary>
        /// Fills in image paths based on files win the folder
        /// </summary>
        protected virtual void PopulateImages(T item, ItemResolveEventArgs args)
        {
            List<string> backdropFiles = new List<string>();

            foreach (KeyValuePair<string,FileAttributes> file in args.FileSystemChildren)
            {
                if (file.Value.HasFlag(FileAttributes.Directory))
                {
                    continue;
                }

                string filePath = file.Key;

                string ext = Path.GetExtension(filePath);

                // Only support png and jpg files
                if (!ext.EndsWith("png", StringComparison.OrdinalIgnoreCase) && !ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                string name = Path.GetFileNameWithoutExtension(filePath);

                if (name.Equals("folder", StringComparison.OrdinalIgnoreCase))
                {
                    item.PrimaryImagePath = filePath;
                }
                else if (name.StartsWith("backdrop", StringComparison.OrdinalIgnoreCase))
                {
                    backdropFiles.Add(filePath);
                }
                if (name.Equals("logo", StringComparison.OrdinalIgnoreCase))
                {
                    item.LogoImagePath = filePath;
                }
                if (name.Equals("banner", StringComparison.OrdinalIgnoreCase))
                {
                    item.BannerImagePath = filePath;
                }
                if (name.Equals("art", StringComparison.OrdinalIgnoreCase))
                {
                    item.ArtImagePath = filePath;
                }
                if (name.Equals("thumb", StringComparison.OrdinalIgnoreCase))
                {
                    item.ThumbnailImagePath = filePath;
                }
            }

            if (backdropFiles.Any())
            {
                item.BackdropImagePaths = backdropFiles;
            }
        }

        protected virtual void PopulateLocalTrailers(T item, ItemResolveEventArgs args)
        {
            var trailerPath = args.GetFolderByName("trailers");

            if (trailerPath.HasValue)
            {
                string[] allFiles = Directory.GetFileSystemEntries(trailerPath.Value.Key, "*", SearchOption.TopDirectoryOnly);

                item.LocalTrailers = allFiles.Select(f => Kernel.Instance.ItemController.GetItem(f)).OfType<Video>();
            }
        }
    }

    /// <summary>
    /// Weed this to keep a list of resolvers, since Resolvers are built with generics
    /// </summary>
    public interface IBaseItemResolver
    {
        BaseItem ResolvePath(ItemResolveEventArgs args);
    }
}