LibraryService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Entities;
  5. using ServiceStack;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace MediaBrowser.Api.Library
  10. {
  11. /// <summary>
  12. /// Class GetPhyscialPaths
  13. /// </summary>
  14. [Route("/Library/PhysicalPaths", "GET")]
  15. [Api(Description = "Gets a list of physical paths from virtual folders")]
  16. public class GetPhyscialPaths : IReturn<List<string>>
  17. {
  18. }
  19. /// <summary>
  20. /// Class GetItemTypes
  21. /// </summary>
  22. [Route("/Library/ItemTypes", "GET")]
  23. [Api(Description = "Gets a list of BaseItem types")]
  24. public class GetItemTypes : IReturn<List<string>>
  25. {
  26. /// <summary>
  27. /// Gets or sets a value indicating whether this instance has internet provider.
  28. /// </summary>
  29. /// <value><c>true</c> if this instance has internet provider; otherwise, <c>false</c>.</value>
  30. [ApiMember(Name = "HasInternetProvider", Description = "Optional filter by item types that have internet providers. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  31. public bool HasInternetProvider { get; set; }
  32. }
  33. /// <summary>
  34. /// Class LibraryService
  35. /// </summary>
  36. public class LibraryService : BaseApiService
  37. {
  38. /// <summary>
  39. /// The _app host
  40. /// </summary>
  41. private readonly IApplicationHost _appHost;
  42. private readonly ILibraryManager _libraryManager;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="LibraryService" /> class.
  45. /// </summary>
  46. /// <param name="appHost">The app host.</param>
  47. /// <param name="libraryManager">The library manager.</param>
  48. /// <exception cref="System.ArgumentNullException">appHost</exception>
  49. public LibraryService(IApplicationHost appHost, ILibraryManager libraryManager)
  50. {
  51. if (appHost == null)
  52. {
  53. throw new ArgumentNullException("appHost");
  54. }
  55. _appHost = appHost;
  56. _libraryManager = libraryManager;
  57. }
  58. /// <summary>
  59. /// Gets the specified request.
  60. /// </summary>
  61. /// <param name="request">The request.</param>
  62. /// <returns>System.Object.</returns>
  63. public object Get(GetPhyscialPaths request)
  64. {
  65. var result = _libraryManager.RootFolder.Children
  66. .SelectMany(c =>
  67. {
  68. var locationType = c.LocationType;
  69. if (locationType != LocationType.Remote && locationType != LocationType.Virtual)
  70. {
  71. return c.PhysicalLocations;
  72. }
  73. return new List<string>();
  74. })
  75. .ToList();
  76. return ToOptimizedSerializedResultUsingCache(result);
  77. }
  78. /// <summary>
  79. /// Gets the specified request.
  80. /// </summary>
  81. /// <param name="request">The request.</param>
  82. /// <returns>System.Object.</returns>
  83. public object Get(GetItemTypes request)
  84. {
  85. var allTypes = _appHost.AllConcreteTypes.Where(t => t.IsSubclassOf(typeof(BaseItem)));
  86. if (request.HasInternetProvider)
  87. {
  88. allTypes = allTypes.Where(t =>
  89. {
  90. if (t == typeof(UserRootFolder) || t == typeof(AggregateFolder) || t == typeof(Folder) || t == typeof(CollectionFolder) || t == typeof(Year))
  91. {
  92. return false;
  93. }
  94. if (t == typeof(User))
  95. {
  96. return false;
  97. }
  98. // For now it seems internet providers generally only deal with video subclasses
  99. if (t == typeof(Video))
  100. {
  101. return false;
  102. }
  103. if (t.IsSubclassOf(typeof(BasePluginFolder)))
  104. {
  105. return false;
  106. }
  107. return true;
  108. });
  109. }
  110. return allTypes.Select(t => t.Name).OrderBy(s => s).ToList();
  111. }
  112. }
  113. }