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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Events;
using MediaBrowser.Controller.Events;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Library
{
public class ItemController
{
private List<IBaseItemResolver> Resolvers = new List<IBaseItemResolver>();
/// <summary>
/// Registers a new BaseItem resolver.
/// </summary>
public void AddResovler<TBaseItemType, TResolverType>()
where TBaseItemType : BaseItem, new()
where TResolverType : BaseItemResolver<TBaseItemType>, new()
{
Resolvers.Insert(0, new TResolverType());
}
#region PreBeginResolvePath Event
/// <summary>
/// Fires when a path is about to be resolved, but before child folders and files
/// have been collected from the file system.
/// This gives listeners a chance to cancel the operation and cause the path to be ignored.
/// </summary>
public event EventHandler<PreBeginResolveEventArgs> PreBeginResolvePath;
private bool OnPreBeginResolvePath(Folder parent, string path, FileAttributes attributes)
{
PreBeginResolveEventArgs args = new PreBeginResolveEventArgs()
{
Path = path,
Parent = parent,
FileAttributes = attributes,
Cancel = false
};
if (PreBeginResolvePath != null)
{
PreBeginResolvePath(this, args);
}
return !args.Cancel;
}
#endregion
#region BeginResolvePath Event
/// <summary>
/// Fires when a path is about to be resolved, but after child folders and files
/// have been collected from the file system.
/// This gives listeners a chance to cancel the operation and cause the path to be ignored.
/// </summary>
public event EventHandler<ItemResolveEventArgs> BeginResolvePath;
private bool OnBeginResolvePath(ItemResolveEventArgs args)
{
if (BeginResolvePath != null)
{
BeginResolvePath(this, args);
}
return !args.Cancel;
}
#endregion
#region Item Events
/// <summary>
/// Called when an item is being created.
/// This should be used to fill item values, such as metadata
/// </summary>
public event EventHandler<GenericItemEventArgs<BaseItem>> ItemCreating;
/// <summary>
/// Called when an item has been created.
/// This should be used to process or modify item values.
/// </summary>
public event EventHandler<GenericItemEventArgs<BaseItem>> ItemCreated;
#endregion
/// <summary>
/// Called when an item has been created
/// </summary>
private void OnItemCreated(BaseItem item, Folder parent)
{
GenericItemEventArgs<BaseItem> args = new GenericItemEventArgs<BaseItem> { Item = item };
if (ItemCreating != null)
{
ItemCreating(this, args);
}
if (ItemCreated != null)
{
ItemCreated(this, args);
}
}
private void FireCreateEventsRecursive(Folder folder, Folder parent)
{
OnItemCreated(folder, parent);
int count = folder.Children.Length;
Parallel.For(0, count, i =>
{
BaseItem item = folder.Children[i];
Folder childFolder = item as Folder;
if (childFolder != null)
{
FireCreateEventsRecursive(childFolder, folder);
}
else
{
OnItemCreated(item, folder);
}
});
}
private BaseItem ResolveItem(ItemResolveEventArgs args)
{
// If that didn't pan out, try the slow ones
foreach (IBaseItemResolver resolver in Resolvers)
{
var item = resolver.ResolvePath(args);
if (item != null)
{
return item;
}
}
return null;
}
/// <summary>
/// Resolves a path into a BaseItem
/// </summary>
public BaseItem GetItem(string path)
{
return GetItem(null, path);
}
/// <summary>
/// Resolves a path into a BaseItem
/// </summary>
public BaseItem GetItem(Folder parent, string path)
{
BaseItem item = GetItemInternal(parent, path, File.GetAttributes(path));
if (item != null)
{
var folder = item as Folder;
if (folder != null)
{
FireCreateEventsRecursive(folder, parent);
}
else
{
OnItemCreated(item, parent);
}
}
return item;
}
/// <summary>
/// Resolves a path into a BaseItem
/// </summary>
private BaseItem GetItemInternal(Folder parent, string path, FileAttributes attributes)
{
if (!OnPreBeginResolvePath(parent, path, attributes))
{
return null;
}
IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren;
// Gather child folder and files
if (attributes.HasFlag(FileAttributes.Directory))
{
fileSystemChildren = Directory.GetFileSystemEntries(path, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair<string, FileAttributes>(f, File.GetAttributes(f)));
bool isVirtualFolder = parent != null && parent.IsRoot;
fileSystemChildren = FilterChildFileSystemEntries(fileSystemChildren, isVirtualFolder);
}
else
{
fileSystemChildren = new KeyValuePair<string, FileAttributes>[] { };
}
ItemResolveEventArgs args = new ItemResolveEventArgs()
{
Path = path,
FileAttributes = attributes,
FileSystemChildren = fileSystemChildren,
Parent = parent,
Cancel = false
};
// Fire BeginResolvePath to see if anyone wants to cancel this operation
if (!OnBeginResolvePath(args))
{
return null;
}
BaseItem item = ResolveItem(args);
var folder = item as Folder;
if (folder != null)
{
// If it's a folder look for child entities
AttachChildren(folder, fileSystemChildren);
}
return item;
}
/// <summary>
/// Finds child BaseItems for a given Folder
/// </summary>
private void AttachChildren(Folder folder, IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren)
{
List<BaseItem> baseItemChildren = new List<BaseItem>();
int count = fileSystemChildren.Count();
// Resolve the child folder paths into entities
Parallel.For(0, count, i =>
{
KeyValuePair<string, FileAttributes> child = fileSystemChildren.ElementAt(i);
BaseItem item = GetItemInternal(folder, child.Key, child.Value);
if (item != null)
{
lock (baseItemChildren)
{
baseItemChildren.Add(item);
}
}
});
// Sort them
folder.Children = baseItemChildren.OrderBy(f =>
{
return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
}).ToArray();
}
/// <summary>
/// Transforms shortcuts into their actual paths
/// </summary>
private List<KeyValuePair<string, FileAttributes>> FilterChildFileSystemEntries(IEnumerable<KeyValuePair<string, FileAttributes>> fileSystemChildren, bool flattenShortcuts)
{
List<KeyValuePair<string, FileAttributes>> returnFiles = new List<KeyValuePair<string, FileAttributes>>();
// Loop through each file
foreach (KeyValuePair<string, FileAttributes> file in fileSystemChildren)
{
// Folders
if (file.Value.HasFlag(FileAttributes.Directory))
{
returnFiles.Add(file);
}
// If it's a shortcut, resolve it
else if (Shortcut.IsShortcut(file.Key))
{
string newPath = Shortcut.ResolveShortcut(file.Key);
FileAttributes newPathAttributes = File.GetAttributes(newPath);
// Find out if the shortcut is pointing to a directory or file
if (newPathAttributes.HasFlag(FileAttributes.Directory))
{
// If we're flattening then get the shortcut's children
if (flattenShortcuts)
{
IEnumerable<KeyValuePair<string, FileAttributes>> newChildren = Directory.GetFileSystemEntries(newPath, "*", SearchOption.TopDirectoryOnly).Select(f => new KeyValuePair<string, FileAttributes>(f, File.GetAttributes(f)));
returnFiles.AddRange(FilterChildFileSystemEntries(newChildren, false));
}
else
{
returnFiles.Add(new KeyValuePair<string, FileAttributes>(newPath, newPathAttributes));
}
}
else
{
returnFiles.Add(new KeyValuePair<string, FileAttributes>(newPath, newPathAttributes));
}
}
else
{
returnFiles.Add(file);
}
}
return returnFiles;
}
public Person GetPerson(string name)
{
// not yet implemented
return null;
}
public Studio GetStudio(string name)
{
// not yet implemented
return null;
}
public Genre GetGenre(string name)
{
// not yet implemented
return null;
}
public Year GetYear(int value)
{
// not yet implemented
return null;
}
}
}
|