ApiService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Users;
  8. namespace MediaBrowser.Api
  9. {
  10. /// <summary>
  11. /// Contains some helpers for the api
  12. /// </summary>
  13. public static class ApiService
  14. {
  15. public static BaseItem GetItemById(string id)
  16. {
  17. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  18. return Kernel.Instance.GetItemById(guid);
  19. }
  20. /// <summary>
  21. /// Takes a BaseItem and returns the actual object that will be serialized by the api
  22. /// </summary>
  23. public static ApiBaseItemWrapper<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
  24. {
  25. ApiBaseItemWrapper<BaseItem> wrapper = new ApiBaseItemWrapper<BaseItem>()
  26. {
  27. Item = item,
  28. UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id),
  29. Type = item.GetType().Name,
  30. IsFolder = (item is Folder)
  31. };
  32. if (string.IsNullOrEmpty(item.LogoImagePath))
  33. {
  34. wrapper.ParentLogoItemId = GetParentLogoItemId(item);
  35. }
  36. if (item.BackdropImagePaths == null || !item.BackdropImagePaths.Any())
  37. {
  38. int backdropCount;
  39. wrapper.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  40. wrapper.ParentBackdropCount = backdropCount;
  41. }
  42. if (item.Parent != null)
  43. {
  44. wrapper.ParentId = item.Parent.Id;
  45. }
  46. if (includeChildren)
  47. {
  48. var folder = item as Folder;
  49. if (folder != null)
  50. {
  51. wrapper.Children = Kernel.Instance.GetParentalAllowedChildren(folder, userId).Select(c => GetSerializationObject(c, false, userId));
  52. }
  53. }
  54. return wrapper;
  55. }
  56. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  57. {
  58. backdropCount = 0;
  59. var parent = item.Parent;
  60. while (parent != null)
  61. {
  62. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  63. {
  64. backdropCount = parent.BackdropImagePaths.Count();
  65. return parent.Id;
  66. }
  67. parent = parent.Parent;
  68. }
  69. return null;
  70. }
  71. private static Guid? GetParentLogoItemId(BaseItem item)
  72. {
  73. var parent = item.Parent;
  74. while (parent != null)
  75. {
  76. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  77. {
  78. return parent.Id;
  79. }
  80. parent = parent.Parent;
  81. }
  82. return null;
  83. }
  84. }
  85. }