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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
using MediaBrowser.Controller;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
/// <summary>
/// Contains some helpers for the api
/// </summary>
public static class ApiService
{
/// <summary>
/// Gets an Item by Id, or the root item if none is supplied
/// </summary>
public static BaseItem GetItemById(string id)
{
Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
return Kernel.Instance.GetItemById(guid);
}
/// <summary>
/// Gets a User by Id
/// </summary>
/// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
public static User GetUserById(string id, bool logActivity)
{
Guid guid = new Guid(id);
User user = Kernel.Instance.Users.FirstOrDefault(u => u.Id == guid);
if (logActivity)
{
LogUserActivity(user);
}
return user;
}
/// <summary>
/// Gets the default User
/// </summary>
/// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
public static User GetDefaultUser(bool logActivity)
{
User user = Kernel.Instance.GetDefaultUser();
if (logActivity)
{
LogUserActivity(user);
}
return user;
}
/// <summary>
/// Updates LastActivityDate for a given User
/// </summary>
public static void LogUserActivity(User user)
{
user.LastActivityDate = DateTime.UtcNow;
Kernel.Instance.SaveUser(user);
}
/// <summary>
/// Converts a BaseItem to a DTOBaseItem
/// </summary>
public async static Task<DTOBaseItem> GetDTOBaseItem(BaseItem item, User user,
bool includeChildren = true,
bool includePeople = true)
{
DTOBaseItem dto = new DTOBaseItem();
List<Task> tasks = new List<Task>();
tasks.Add(AttachStudios(dto, item));
if (includeChildren)
{
tasks.Add(AttachChildren(dto, item, user));
tasks.Add(AttachLocalTrailers(dto, item, user));
}
if (includePeople)
{
tasks.Add(AttachPeople(dto, item));
}
AttachBasicFields(dto, item, user);
// Make sure all the tasks we kicked off have completed.
if (tasks.Count > 0)
{
await Task.WhenAll(tasks).ConfigureAwait(false);
}
return dto;
}
/// <summary>
/// Sets simple property values on a DTOBaseItem
/// </summary>
private static void AttachBasicFields(DTOBaseItem dto, BaseItem item, User user)
{
dto.AspectRatio = item.AspectRatio;
dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
dto.DateCreated = item.DateCreated;
dto.DisplayMediaType = item.DisplayMediaType;
if (item.Genres != null)
{
dto.Genres = item.Genres.ToArray();
}
dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
dto.HasPrimaryImage = !string.IsNullOrEmpty(item.PrimaryImagePath);
dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
dto.Id = item.Id;
dto.IsNew = item.IsRecentlyAdded(user);
dto.IndexNumber = item.IndexNumber;
dto.IsFolder = item.IsFolder;
dto.Language = item.Language;
dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
dto.Name = item.Name;
dto.OfficialRating = item.OfficialRating;
dto.Overview = item.Overview;
// If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
if (dto.BackdropCount == 0)
{
int backdropCount;
dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
dto.ParentBackdropCount = backdropCount;
}
if (item.Parent != null)
{
dto.ParentId = item.Parent.Id;
}
dto.ParentIndexNumber = item.ParentIndexNumber;
// If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
if (!dto.HasLogo)
{
dto.ParentLogoItemId = GetParentLogoItemId(item);
}
dto.Path = item.Path;
dto.PremiereDate = item.PremiereDate;
dto.ProductionYear = item.ProductionYear;
dto.ProviderIds = item.ProviderIds;
dto.RunTimeTicks = item.RunTimeTicks;
dto.SortName = item.SortName;
if (item.Taglines != null)
{
dto.Taglines = item.Taglines.ToArray();
}
dto.TrailerUrl = item.TrailerUrl;
dto.Type = item.GetType().Name;
dto.UserRating = item.UserRating;
dto.UserData = item.GetUserData(user);
Folder folder = item as Folder;
if (folder != null)
{
dto.SpecialCounts = folder.GetSpecialCounts(user);
dto.IsRoot = folder.IsRoot;
dto.IsVirtualFolder = folder.IsVirtualFolder;
}
Audio audio = item as Audio;
if (audio != null)
{
dto.AudioInfo = new AudioInfo()
{
Album = audio.Album,
AlbumArtist = audio.AlbumArtist,
Artist = audio.Artist,
BitRate = audio.BitRate,
Channels = audio.Channels
};
}
Video video = item as Video;
if (video != null)
{
dto.VideoInfo = new VideoInfo()
{
Height = video.Height,
Width = video.Width,
Codec = video.Codec,
VideoType = video.VideoType,
ScanType = video.ScanType
};
if (video.AudioStreams != null)
{
dto.VideoInfo.AudioStreams = video.AudioStreams.ToArray();
}
if (video.Subtitles != null)
{
dto.VideoInfo.Subtitles = video.Subtitles.ToArray();
}
}
}
/// <summary>
/// Attaches Studio DTO's to a DTOBaseItem
/// </summary>
private static async Task AttachStudios(DTOBaseItem dto, BaseItem item)
{
// Attach Studios by transforming them into BaseItemStudio (DTO)
if (item.Studios != null)
{
Studio[] entities = await Task.WhenAll<Studio>(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
dto.Studios = new BaseItemStudio[entities.Length];
for (int i = 0; i < entities.Length; i++)
{
Studio entity = entities[i];
BaseItemStudio baseItemStudio = new BaseItemStudio();
baseItemStudio.Name = entity.Name;
baseItemStudio.HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath);
dto.Studios[i] = baseItemStudio;
}
}
}
/// <summary>
/// Attaches child DTO's to a DTOBaseItem
/// </summary>
private static async Task AttachChildren(DTOBaseItem dto, BaseItem item, User user)
{
var folder = item as Folder;
if (folder != null)
{
IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
dto.Children = await Task.WhenAll<DTOBaseItem>(children.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
}
}
/// <summary>
/// Attaches trailer DTO's to a DTOBaseItem
/// </summary>
private static async Task AttachLocalTrailers(DTOBaseItem dto, BaseItem item, User user)
{
if (item.LocalTrailers != null && item.LocalTrailers.Any())
{
dto.LocalTrailers = await Task.WhenAll<DTOBaseItem>(item.LocalTrailers.Select(c => GetDTOBaseItem(c, user, false, false))).ConfigureAwait(false);
}
}
/// <summary>
/// Attaches People DTO's to a DTOBaseItem
/// </summary>
private static async Task AttachPeople(DTOBaseItem dto, BaseItem item)
{
// Attach People by transforming them into BaseItemPerson (DTO)
if (item.People != null)
{
IEnumerable<Person> entities = await Task.WhenAll<Person>(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
dto.People = item.People.Select(p =>
{
BaseItemPerson baseItemPerson = new BaseItemPerson();
baseItemPerson.Name = p.Key;
baseItemPerson.Overview = p.Value.Overview;
baseItemPerson.Type = p.Value.Type;
Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase));
if (ibnObject != null)
{
baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
}
return baseItemPerson;
}).ToArray();
}
}
/// <summary>
/// If an item does not any backdrops, this can be used to find the first parent that does have one
/// </summary>
private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
{
backdropCount = 0;
var parent = item.Parent;
while (parent != null)
{
if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
{
backdropCount = parent.BackdropImagePaths.Count();
return parent.Id;
}
parent = parent.Parent;
}
return null;
}
/// <summary>
/// If an item does not have a logo, this can be used to find the first parent that does have one
/// </summary>
private static Guid? GetParentLogoItemId(BaseItem item)
{
var parent = item.Parent;
while (parent != null)
{
if (!string.IsNullOrEmpty(parent.LogoImagePath))
{
return parent.Id;
}
parent = parent.Parent;
}
return null;
}
/// <summary>
/// Gets an ImagesByName entity along with the number of items containing it
/// </summary>
public static IBNItem GetIBNItem(BaseEntity entity, int itemCount)
{
return new IBNItem()
{
Id = entity.Id,
BaseItemCount = itemCount,
HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath),
Name = entity.Name
};
}
/// <summary>
/// Converts a User to a DTOUser
/// </summary>
public static DTOUser GetDTOUser(User user)
{
return new DTOUser()
{
Id = user.Id,
Name = user.Name,
HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath),
HasPassword = !string.IsNullOrEmpty(user.Password),
LastActivityDate = user.LastActivityDate,
LastLoginDate = user.LastLoginDate
};
}
}
}
|