LibraryService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. try
  72. {
  73. return c.ResolveArgs.PhysicalLocations;
  74. }
  75. catch (Exception ex)
  76. {
  77. Logger.ErrorException("Error getting ResolveArgs for {0}", ex, c.Path);
  78. }
  79. }
  80. return new List<string>();
  81. })
  82. .ToList();
  83. return ToOptimizedResult(result);
  84. }
  85. /// <summary>
  86. /// Gets the specified request.
  87. /// </summary>
  88. /// <param name="request">The request.</param>
  89. /// <returns>System.Object.</returns>
  90. public object Get(GetItemTypes request)
  91. {
  92. var allTypes = _appHost.AllConcreteTypes.Where(t => t.IsSubclassOf(typeof(BaseItem)));
  93. if (request.HasInternetProvider)
  94. {
  95. allTypes = allTypes.Where(t =>
  96. {
  97. if (t == typeof(UserRootFolder) || t == typeof(AggregateFolder) || t == typeof(Folder) || t == typeof(CollectionFolder) || t == typeof(Year))
  98. {
  99. return false;
  100. }
  101. if (t == typeof(User))
  102. {
  103. return false;
  104. }
  105. // For now it seems internet providers generally only deal with video subclasses
  106. if (t == typeof(Video))
  107. {
  108. return false;
  109. }
  110. if (t.IsSubclassOf(typeof(BasePluginFolder)))
  111. {
  112. return false;
  113. }
  114. return true;
  115. });
  116. }
  117. return allTypes.Select(t => t.Name).OrderBy(s => s).ToList();
  118. }
  119. }
  120. }