ApiService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Model.DTO;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Contains some helpers for the api
  15. /// </summary>
  16. public static class ApiService
  17. {
  18. /// <summary>
  19. /// Gets an Item by Id, or the root item if none is supplied
  20. /// </summary>
  21. public static BaseItem GetItemById(string id)
  22. {
  23. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  24. return Kernel.Instance.GetItemById(guid);
  25. }
  26. /// <summary>
  27. /// Gets a User by Id
  28. /// </summary>
  29. /// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
  30. public static User GetUserById(string id, bool logActivity)
  31. {
  32. var guid = new Guid(id);
  33. var user = Kernel.Instance.Users.FirstOrDefault(u => u.Id == guid);
  34. if (logActivity)
  35. {
  36. LogUserActivity(user);
  37. }
  38. return user;
  39. }
  40. /// <summary>
  41. /// Gets the default User
  42. /// </summary>
  43. /// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
  44. public static User GetDefaultUser(bool logActivity)
  45. {
  46. User user = Kernel.Instance.GetDefaultUser();
  47. if (logActivity)
  48. {
  49. LogUserActivity(user);
  50. }
  51. return user;
  52. }
  53. /// <summary>
  54. /// Updates LastActivityDate for a given User
  55. /// </summary>
  56. public static void LogUserActivity(User user)
  57. {
  58. user.LastActivityDate = DateTime.UtcNow;
  59. Kernel.Instance.SaveUser(user);
  60. }
  61. /// <summary>
  62. /// Converts a BaseItem to a DTOBaseItem
  63. /// </summary>
  64. public async static Task<DtoBaseItem> GetDtoBaseItem(BaseItem item, User user,
  65. bool includeChildren = true,
  66. bool includePeople = true)
  67. {
  68. var dto = new DtoBaseItem();
  69. var tasks = new List<Task>();
  70. tasks.Add(AttachStudios(dto, item));
  71. if (includeChildren)
  72. {
  73. tasks.Add(AttachChildren(dto, item, user));
  74. tasks.Add(AttachLocalTrailers(dto, item, user));
  75. }
  76. if (includePeople)
  77. {
  78. tasks.Add(AttachPeople(dto, item));
  79. }
  80. AttachBasicFields(dto, item, user);
  81. // Make sure all the tasks we kicked off have completed.
  82. if (tasks.Count > 0)
  83. {
  84. await Task.WhenAll(tasks).ConfigureAwait(false);
  85. }
  86. return dto;
  87. }
  88. /// <summary>
  89. /// Sets simple property values on a DTOBaseItem
  90. /// </summary>
  91. private static void AttachBasicFields(DtoBaseItem dto, BaseItem item, User user)
  92. {
  93. dto.AspectRatio = item.AspectRatio;
  94. dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
  95. dto.DateCreated = item.DateCreated;
  96. dto.DisplayMediaType = item.DisplayMediaType;
  97. if (item.Genres != null)
  98. {
  99. dto.Genres = item.Genres.ToArray();
  100. }
  101. dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
  102. dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
  103. dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
  104. dto.HasPrimaryImage = !string.IsNullOrEmpty(item.PrimaryImagePath);
  105. dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
  106. dto.Id = item.Id;
  107. dto.IsNew = item.IsRecentlyAdded(user);
  108. dto.IndexNumber = item.IndexNumber;
  109. dto.IsFolder = item.IsFolder;
  110. dto.Language = item.Language;
  111. dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
  112. dto.Name = item.Name;
  113. dto.OfficialRating = item.OfficialRating;
  114. dto.Overview = item.Overview;
  115. // If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance
  116. if (dto.BackdropCount == 0)
  117. {
  118. int backdropCount;
  119. dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  120. dto.ParentBackdropCount = backdropCount;
  121. }
  122. if (item.Parent != null)
  123. {
  124. dto.ParentId = item.Parent.Id;
  125. }
  126. dto.ParentIndexNumber = item.ParentIndexNumber;
  127. // If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance
  128. if (!dto.HasLogo)
  129. {
  130. dto.ParentLogoItemId = GetParentLogoItemId(item);
  131. }
  132. dto.Path = item.Path;
  133. dto.PremiereDate = item.PremiereDate;
  134. dto.ProductionYear = item.ProductionYear;
  135. dto.ProviderIds = item.ProviderIds;
  136. dto.RunTimeTicks = item.RunTimeTicks;
  137. dto.SortName = item.SortName;
  138. if (item.Taglines != null)
  139. {
  140. dto.Taglines = item.Taglines.ToArray();
  141. }
  142. dto.TrailerUrl = item.TrailerUrl;
  143. dto.Type = item.GetType().Name;
  144. dto.CommunityRating = item.CommunityRating;
  145. dto.UserData = GetDtoUserItemData(item.GetUserData(user, false));
  146. var folder = item as Folder;
  147. if (folder != null)
  148. {
  149. dto.SpecialCounts = folder.GetSpecialCounts(user);
  150. dto.IsRoot = folder.IsRoot;
  151. dto.IsVirtualFolder = folder.IsVirtualFolder;
  152. }
  153. // Add AudioInfo
  154. var audio = item as Audio;
  155. if (audio != null)
  156. {
  157. dto.AudioInfo = new AudioInfo
  158. {
  159. Album = audio.Album,
  160. AlbumArtist = audio.AlbumArtist,
  161. Artist = audio.Artist,
  162. BitRate = audio.BitRate,
  163. Channels = audio.Channels
  164. };
  165. }
  166. // Add VideoInfo
  167. var video = item as Video;
  168. if (video != null)
  169. {
  170. dto.VideoInfo = new VideoInfo
  171. {
  172. Height = video.Height,
  173. Width = video.Width,
  174. Codec = video.Codec,
  175. VideoType = video.VideoType,
  176. ScanType = video.ScanType
  177. };
  178. if (video.AudioStreams != null)
  179. {
  180. dto.VideoInfo.AudioStreams = video.AudioStreams.ToArray();
  181. }
  182. if (video.Subtitles != null)
  183. {
  184. dto.VideoInfo.Subtitles = video.Subtitles.ToArray();
  185. }
  186. }
  187. // Add SeriesInfo
  188. var series = item as Series;
  189. if (series != null)
  190. {
  191. DayOfWeek[] airDays = series.AirDays == null ? new DayOfWeek[] { } : series.AirDays.ToArray();
  192. dto.SeriesInfo = new SeriesInfo
  193. {
  194. AirDays = airDays,
  195. AirTime = series.AirTime,
  196. Status = series.Status
  197. };
  198. }
  199. // Add MovieInfo
  200. var movie = item as Movie;
  201. if (movie != null)
  202. {
  203. int specialFeatureCount = movie.SpecialFeatures == null ? 0 : movie.SpecialFeatures.Count();
  204. dto.MovieInfo = new MovieInfo
  205. {
  206. SpecialFeatureCount = specialFeatureCount
  207. };
  208. }
  209. }
  210. /// <summary>
  211. /// Attaches Studio DTO's to a DTOBaseItem
  212. /// </summary>
  213. private static async Task AttachStudios(DtoBaseItem dto, BaseItem item)
  214. {
  215. // Attach Studios by transforming them into BaseItemStudio (DTO)
  216. if (item.Studios != null)
  217. {
  218. Studio[] entities = await Task.WhenAll(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
  219. dto.Studios = new BaseItemStudio[entities.Length];
  220. for (int i = 0; i < entities.Length; i++)
  221. {
  222. Studio entity = entities[i];
  223. var baseItemStudio = new BaseItemStudio{};
  224. baseItemStudio.Name = entity.Name;
  225. baseItemStudio.HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath);
  226. dto.Studios[i] = baseItemStudio;
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// Attaches child DTO's to a DTOBaseItem
  232. /// </summary>
  233. private static async Task AttachChildren(DtoBaseItem dto, BaseItem item, User user)
  234. {
  235. var folder = item as Folder;
  236. if (folder != null)
  237. {
  238. IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
  239. dto.Children = await Task.WhenAll(children.Select(c => GetDtoBaseItem(c, user, false, false))).ConfigureAwait(false);
  240. }
  241. }
  242. /// <summary>
  243. /// Attaches trailer DTO's to a DTOBaseItem
  244. /// </summary>
  245. private static async Task AttachLocalTrailers(DtoBaseItem dto, BaseItem item, User user)
  246. {
  247. if (item.LocalTrailers != null && item.LocalTrailers.Any())
  248. {
  249. dto.LocalTrailers = await Task.WhenAll(item.LocalTrailers.Select(c => GetDtoBaseItem(c, user, false, false))).ConfigureAwait(false);
  250. }
  251. }
  252. /// <summary>
  253. /// Attaches People DTO's to a DTOBaseItem
  254. /// </summary>
  255. private static async Task AttachPeople(DtoBaseItem dto, BaseItem item)
  256. {
  257. // Attach People by transforming them into BaseItemPerson (DTO)
  258. if (item.People != null)
  259. {
  260. IEnumerable<Person> entities = await Task.WhenAll(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
  261. dto.People = item.People.Select(p =>
  262. {
  263. var baseItemPerson = new BaseItemPerson{};
  264. baseItemPerson.Name = p.Key;
  265. baseItemPerson.Overview = p.Value.Overview;
  266. baseItemPerson.Type = p.Value.Type;
  267. Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase));
  268. if (ibnObject != null)
  269. {
  270. baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
  271. }
  272. return baseItemPerson;
  273. }).ToArray();
  274. }
  275. }
  276. /// <summary>
  277. /// If an item does not any backdrops, this can be used to find the first parent that does have one
  278. /// </summary>
  279. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  280. {
  281. backdropCount = 0;
  282. var parent = item.Parent;
  283. while (parent != null)
  284. {
  285. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  286. {
  287. backdropCount = parent.BackdropImagePaths.Count();
  288. return parent.Id;
  289. }
  290. parent = parent.Parent;
  291. }
  292. return null;
  293. }
  294. /// <summary>
  295. /// If an item does not have a logo, this can be used to find the first parent that does have one
  296. /// </summary>
  297. private static Guid? GetParentLogoItemId(BaseItem item)
  298. {
  299. var parent = item.Parent;
  300. while (parent != null)
  301. {
  302. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  303. {
  304. return parent.Id;
  305. }
  306. parent = parent.Parent;
  307. }
  308. return null;
  309. }
  310. /// <summary>
  311. /// Gets an ImagesByName entity along with the number of items containing it
  312. /// </summary>
  313. public static IbnItem GetIbnItem(BaseEntity entity, int itemCount)
  314. {
  315. return new IbnItem
  316. {
  317. Id = entity.Id,
  318. BaseItemCount = itemCount,
  319. HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath),
  320. Name = entity.Name
  321. };
  322. }
  323. /// <summary>
  324. /// Converts a User to a DTOUser
  325. /// </summary>
  326. public static DtoUser GetDtoUser(User user)
  327. {
  328. return new DtoUser
  329. {
  330. Id = user.Id,
  331. Name = user.Name,
  332. HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath),
  333. HasPassword = !string.IsNullOrEmpty(user.Password),
  334. LastActivityDate = user.LastActivityDate,
  335. LastLoginDate = user.LastLoginDate
  336. };
  337. }
  338. /// <summary>
  339. /// Converts a UserItemData to a DTOUserItemData
  340. /// </summary>
  341. public static DtoUserItemData GetDtoUserItemData(UserItemData data)
  342. {
  343. if (data == null)
  344. {
  345. return null;
  346. }
  347. return new DtoUserItemData
  348. {
  349. IsFavorite = data.IsFavorite,
  350. Likes = data.Likes,
  351. PlaybackPositionTicks = data.PlaybackPositionTicks,
  352. PlayCount = data.PlayCount,
  353. Rating = data.Rating
  354. };
  355. }
  356. public static bool IsApiUrlMatch(string url, HttpListenerRequest request)
  357. {
  358. url = "/api/" + url;
  359. return request.Url.LocalPath.EndsWith(url, StringComparison.OrdinalIgnoreCase);
  360. }
  361. }
  362. }