ApiService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Api.Transcoding;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Model.Entities;
  7. namespace MediaBrowser.Api
  8. {
  9. /// <summary>
  10. /// Contains some helpers for the api
  11. /// </summary>
  12. public static class ApiService
  13. {
  14. /// <summary>
  15. /// Holds the list of active transcoding jobs
  16. /// </summary>
  17. private static List<TranscodingJob> CurrentTranscodingJobs = new List<TranscodingJob>();
  18. /// <summary>
  19. /// Finds an active transcoding job
  20. /// </summary>
  21. public static TranscodingJob GetTranscodingJob(string outputPath)
  22. {
  23. lock (CurrentTranscodingJobs)
  24. {
  25. return CurrentTranscodingJobs.FirstOrDefault(j => j.OutputFile.Equals(outputPath, StringComparison.OrdinalIgnoreCase));
  26. }
  27. }
  28. /// <summary>
  29. /// Removes a transcoding job from the active list
  30. /// </summary>
  31. public static void RemoveTranscodingJob(TranscodingJob job)
  32. {
  33. lock (CurrentTranscodingJobs)
  34. {
  35. CurrentTranscodingJobs.Remove(job);
  36. }
  37. }
  38. /// <summary>
  39. /// Adds a transcoding job to the active list
  40. /// </summary>
  41. public static void AddTranscodingJob(TranscodingJob job)
  42. {
  43. lock (CurrentTranscodingJobs)
  44. {
  45. CurrentTranscodingJobs.Add(job);
  46. }
  47. }
  48. public static BaseItem GetItemById(string id)
  49. {
  50. Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
  51. return Kernel.Instance.GetItemById(guid);
  52. }
  53. /// <summary>
  54. /// Takes a BaseItem and returns the actual object that will be serialized by the api
  55. /// </summary>
  56. public static ApiBaseItemWrapper<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
  57. {
  58. ApiBaseItemWrapper<BaseItem> wrapper = new ApiBaseItemWrapper<BaseItem>()
  59. {
  60. Item = item,
  61. UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id),
  62. Type = item.GetType().Name,
  63. IsFolder = (item is Folder)
  64. };
  65. if (string.IsNullOrEmpty(item.LogoImagePath))
  66. {
  67. wrapper.ParentLogoItemId = GetParentLogoItemId(item);
  68. }
  69. if (item.BackdropImagePaths == null || !item.BackdropImagePaths.Any())
  70. {
  71. int backdropCount;
  72. wrapper.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
  73. wrapper.ParentBackdropCount = backdropCount;
  74. }
  75. if (item.Parent != null)
  76. {
  77. wrapper.ParentId = item.Parent.Id;
  78. }
  79. if (includeChildren)
  80. {
  81. var folder = item as Folder;
  82. if (folder != null)
  83. {
  84. wrapper.Children = Kernel.Instance.GetParentalAllowedChildren(folder, userId).Select(c => GetSerializationObject(c, false, userId));
  85. }
  86. }
  87. return wrapper;
  88. }
  89. private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
  90. {
  91. backdropCount = 0;
  92. var parent = item.Parent;
  93. while (parent != null)
  94. {
  95. if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
  96. {
  97. backdropCount = parent.BackdropImagePaths.Count();
  98. return parent.Id;
  99. }
  100. parent = parent.Parent;
  101. }
  102. return null;
  103. }
  104. private static Guid? GetParentLogoItemId(BaseItem item)
  105. {
  106. var parent = item.Parent;
  107. while (parent != null)
  108. {
  109. if (!string.IsNullOrEmpty(parent.LogoImagePath))
  110. {
  111. return parent.Id;
  112. }
  113. parent = parent.Parent;
  114. }
  115. return null;
  116. }
  117. }
  118. }