ApiService.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ParentLogoItemId = GetParentLogoItemId(item)
  32. };
  33. if (item.Parent != null)
  34. {
  35. wrapper.ParentId = item.Parent.Id;
  36. }
  37. if (includeChildren)
  38. {
  39. var folder = item as Folder;
  40. if (folder != null)
  41. {
  42. wrapper.Children = Kernel.Instance.GetParentalAllowedChildren(folder, userId).Select(c => GetSerializationObject(c, false, userId));
  43. }
  44. }
  45. return wrapper;
  46. }
  47. private static Guid? GetParentLogoItemId(BaseItem item)
  48. {
  49. if (string.IsNullOrEmpty(item.LogoImagePath))
  50. {
  51. var parent = item.Parent;
  52. while (parent != null)
  53. {
  54. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  55. {
  56. return parent.Id;
  57. }
  58. parent = parent.Parent;
  59. }
  60. }
  61. return null;
  62. }
  63. }
  64. }