using MediaBrowser.Common; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; using MediaBrowser.Server.Implementations.HttpServer; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.Linq; namespace MediaBrowser.Api.Library { /// /// Class GetPhyscialPaths /// [Route("/Library/PhysicalPaths", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a list of physical paths from virtual folders")] public class GetPhyscialPaths : IReturn> { } /// /// Class GetItemTypes /// [Route("/Library/ItemTypes", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a list of BaseItem types")] public class GetItemTypes : IReturn> { /// /// Gets or sets a value indicating whether this instance has internet provider. /// /// true if this instance has internet provider; otherwise, false. [ApiMember(Name = "HasInternetProvider", Description = "Optional filter by item types that have internet providers. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")] public bool HasInternetProvider { get; set; } } /// /// Class GetPerson /// [Route("/Persons/{Name}", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a person, by name")] public class GetPerson : IReturn { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "Name", Description = "The person name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } /// /// Class GetStudio /// [Route("/Studios/{Name}", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a studio, by name")] public class GetStudio : IReturn { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "Name", Description = "The studio name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } /// /// Class GetGenre /// [Route("/Genres/{Name}", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a genre, by name")] public class GetGenre : IReturn { /// /// Gets or sets the name. /// /// The name. [ApiMember(Name = "Name", Description = "The genre name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } /// /// Class GetYear /// [Route("/Years/{Year}", "GET")] [ServiceStack.ServiceHost.Api(Description = "Gets a year")] public class GetYear : IReturn { /// /// Gets or sets the year. /// /// The year. [ApiMember(Name = "Year", Description = "The year", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")] public int Year { get; set; } } /// /// Class LibraryService /// public class LibraryService : BaseRestService { /// /// The _app host /// private readonly IApplicationHost _appHost; private readonly ILibraryManager _libraryManager; /// /// Initializes a new instance of the class. /// /// The app host. /// appHost public LibraryService(IApplicationHost appHost, ILibraryManager libraryManager) { if (appHost == null) { throw new ArgumentNullException("appHost"); } _appHost = appHost; _libraryManager = libraryManager; } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPerson request) { var item = _libraryManager.GetPerson(request.Name).Result; // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); var result = new DtoBuilder(Logger).GetBaseItemDto(item, fields.ToList(), _libraryManager).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetGenre request) { var item = _libraryManager.GetGenre(request.Name).Result; // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); var result = new DtoBuilder(Logger).GetBaseItemDto(item, fields.ToList(), _libraryManager).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetStudio request) { var item = _libraryManager.GetStudio(request.Name).Result; // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); var result = new DtoBuilder(Logger).GetBaseItemDto(item, fields.ToList(), _libraryManager).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetYear request) { var item = _libraryManager.GetYear(request.Year).Result; // Get everything var fields = Enum.GetNames(typeof(ItemFields)).Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true)); var result = new DtoBuilder(Logger).GetBaseItemDto(item, fields.ToList(), _libraryManager).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPhyscialPaths request) { var result = _libraryManager.RootFolder.Children.SelectMany(c => c.ResolveArgs.PhysicalLocations).ToList(); return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetItemTypes request) { var allTypes = _appHost.AllConcreteTypes.Where(t => t.IsSubclassOf(typeof(BaseItem))); if (request.HasInternetProvider) { allTypes = allTypes.Where(t => { if (t == typeof(UserRootFolder) || t == typeof(AggregateFolder) || t == typeof(Folder) || t == typeof(IndexFolder) || t == typeof(CollectionFolder) || t == typeof(Year)) { return false; } if (t == typeof(User)) { return false; } // For now it seems internet providers generally only deal with video subclasses if (t == typeof(Video)) { return false; } if (t.IsSubclassOf(typeof(BasePluginFolder))) { return false; } return true; }); } return allTypes.Select(t => t.Name).OrderBy(s => s).ToList(); } } }