aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Library/ItemController.cs
blob: dd08c193e79aa28b3c9edf59679c71868168ca2d (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Common.Extensions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MediaBrowser.Controller.Library
{
    public class ItemController
    {
        //private BaseItem ResolveItem(ItemResolveEventArgs args)
        //{
        //    // Try first priority resolvers
        //    for (int i = 0; i < Kernel.Instance.EntityResolvers.Length; i++)
        //    {
        //        var item = Kernel.Instance.EntityResolvers[i].ResolvePath(args);

        //        if (item != null)
        //        {
        //            return item;
        //        }
        //    }

        //    return null;
        //}

        /// <summary>
        /// Resolves a path into a BaseItem
        /// </summary>
        public async Task<BaseItem> GetItem(string path, Folder parent = null, WIN32_FIND_DATA? fileInfo = null, bool allowInternetProviders = true)
        {
            ItemResolveEventArgs args = new ItemResolveEventArgs()
            {
                FileInfo = fileInfo ?? FileData.GetFileData(path),
                Parent = parent,
                Cancel = false,
                Path = path
            };

            // Gather child folder and files
            if (args.IsDirectory)
            {
                args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();

                bool isVirtualFolder = parent != null && parent.IsRoot;
                args = FileSystemHelper.FilterChildFileSystemEntries(args, isVirtualFolder);
            }
            else
            {
                args.FileSystemChildren = new WIN32_FIND_DATA[] { };
            }


            // Fire BeginResolvePath to see if anyone wants to cancel this operation
            if (!EntityResolutionHelper.ShouldResolvePathContents(args))
            {
                return null;
            }

            BaseItem item = Kernel.Instance.ResolveItem(args);

            return item;
        }

        ///// <summary>
        ///// Finds child BaseItems for a given Folder
        ///// </summary>
        //private Task<BaseItem>[] GetChildren(Folder folder, WIN32_FIND_DATA[] fileSystemChildren, bool allowInternetProviders)
        //{
        //    Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];

        //    for (int i = 0; i < fileSystemChildren.Length; i++)
        //    {
        //        var child = fileSystemChildren[i];

        //        tasks[i] = GetItem(child.Path, folder, child, allowInternetProviders: allowInternetProviders);
        //    }

        //    return tasks;
        //}

        ///// <summary>
        ///// Transforms shortcuts into their actual paths
        ///// </summary>
        //private WIN32_FIND_DATA[] FilterChildFileSystemEntries(WIN32_FIND_DATA[] fileSystemChildren, bool flattenShortcuts)
        //{
        //    WIN32_FIND_DATA[] returnArray = new WIN32_FIND_DATA[fileSystemChildren.Length];
        //    List<WIN32_FIND_DATA> resolvedShortcuts = new List<WIN32_FIND_DATA>();

        //    for (int i = 0; i < fileSystemChildren.Length; i++)
        //    {
        //        WIN32_FIND_DATA file = fileSystemChildren[i];

        //        // If it's a shortcut, resolve it
        //        if (Shortcut.IsShortcut(file.Path))
        //        {
        //            string newPath = Shortcut.ResolveShortcut(file.Path);
        //            WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);

        //            // Find out if the shortcut is pointing to a directory or file
        //            if (newPathData.IsDirectory)
        //            {
        //                // If we're flattening then get the shortcut's children

        //                if (flattenShortcuts)
        //                {
        //                    returnArray[i] = file;
        //                    WIN32_FIND_DATA[] newChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray();

        //                    resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newChildren, false));
        //                }
        //                else
        //                {
        //                    returnArray[i] = newPathData;
        //                }
        //            }
        //            else
        //            {
        //                returnArray[i] = newPathData;
        //            }
        //        }
        //        else
        //        {
        //            returnArray[i] = file;
        //        }
        //    }

        //    if (resolvedShortcuts.Count > 0)
        //    {
        //        resolvedShortcuts.InsertRange(0, returnArray);
        //        return resolvedShortcuts.ToArray();
        //    }
        //    else
        //    {
        //        return returnArray;
        //    }
        //}

        /// <summary>
        /// Gets a Person
        /// </summary>
        public Task<Person> GetPerson(string name)
        {
            return GetImagesByNameItem<Person>(Kernel.Instance.ApplicationPaths.PeoplePath, name);
        }

        /// <summary>
        /// Gets a Studio
        /// </summary>
        public Task<Studio> GetStudio(string name)
        {
            return GetImagesByNameItem<Studio>(Kernel.Instance.ApplicationPaths.StudioPath, name);
        }

        /// <summary>
        /// Gets a Genre
        /// </summary>
        public Task<Genre> GetGenre(string name)
        {
            return GetImagesByNameItem<Genre>(Kernel.Instance.ApplicationPaths.GenrePath, name);
        }

        /// <summary>
        /// Gets a Year
        /// </summary>
        public Task<Year> GetYear(int value)
        {
            return GetImagesByNameItem<Year>(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
        }

        private ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);

        /// <summary>
        /// Generically retrieves an IBN item
        /// </summary>
        private Task<T> GetImagesByNameItem<T>(string path, string name)
            where T : BaseEntity, new()
        {
            name = FileData.GetValidFilename(name);

            string key = Path.Combine(path, name);

            // Look for it in the cache, if it's not there, create it
            if (!ImagesByNameItemCache.ContainsKey(key))
            {
                ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
            }

            return ImagesByNameItemCache[key] as Task<T>;
        }

        /// <summary>
        /// Creates an IBN item based on a given path
        /// </summary>
        private async Task<T> CreateImagesByNameItem<T>(string path, string name)
            where T : BaseEntity, new()
        {
            T item = new T();

            item.Name = name;
            item.Id = path.GetMD5();

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            item.DateCreated = Directory.GetCreationTimeUtc(path);
            item.DateModified = Directory.GetLastWriteTimeUtc(path);

            ItemResolveEventArgs args = new ItemResolveEventArgs();
            args.FileInfo = FileData.GetFileData(path);
            args.FileSystemChildren = FileData.GetFileSystemEntries(path, "*").ToArray();

            await Kernel.Instance.ExecuteMetadataProviders(item).ConfigureAwait(false);

            return item;
        }
    }
}