using MediaBrowser.Common.Net; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Logging; using ServiceStack.Common.Web; using ServiceStack.ServiceHost; using System; using System.Collections.Generic; using System.Linq; namespace MediaBrowser.Api { /// /// Class BaseApiService /// [RequestFilter] public class BaseApiService : IHasResultFactory, IRestfulService { /// /// Gets or sets the logger. /// /// The logger. public ILogger Logger { get; set; } /// /// Gets or sets the HTTP result factory. /// /// The HTTP result factory. public IHttpResultFactory ResultFactory { get; set; } /// /// Gets or sets the request context. /// /// The request context. public IRequestContext RequestContext { get; set; } /// /// To the optimized result. /// /// /// The result. /// System.Object. protected object ToOptimizedResult(T result) where T : class { return ResultFactory.GetOptimizedResult(RequestContext, result); } /// /// To the optimized result using cache. /// /// /// The cache key. /// The last date modified. /// Duration of the cache. /// The factory fn. /// System.Object. /// cacheKey protected object ToOptimizedResultUsingCache(Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func factoryFn) where T : class { return ResultFactory.GetOptimizedResultUsingCache(RequestContext, cacheKey, lastDateModified, cacheDuration, factoryFn); } /// /// To the cached result. /// /// /// The cache key. /// The last date modified. /// Duration of the cache. /// The factory fn. /// Type of the content. /// System.Object. /// cacheKey protected object ToCachedResult(Guid cacheKey, DateTime lastDateModified, TimeSpan? cacheDuration, Func factoryFn, string contentType) where T : class { return ResultFactory.GetCachedResult(RequestContext, cacheKey, lastDateModified, cacheDuration, factoryFn, contentType); } /// /// To the static file result. /// /// The path. /// System.Object. protected object ToStaticFileResult(string path) { return ResultFactory.GetStaticFileResult(RequestContext, path); } private readonly char[] _dashReplaceChars = new[] { '?', '/' }; private const char SlugChar = '-'; protected Artist GetArtist(string name, ILibraryManager libraryManager) { return libraryManager.GetArtist(DeSlugArtistName(name, libraryManager)); } protected Studio GetStudio(string name, ILibraryManager libraryManager) { return libraryManager.GetStudio(DeSlugStudioName(name, libraryManager)); } protected Genre GetGenre(string name, ILibraryManager libraryManager) { return libraryManager.GetGenre(DeSlugGenreName(name, libraryManager)); } protected MusicGenre GetMusicGenre(string name, ILibraryManager libraryManager) { return libraryManager.GetMusicGenre(DeSlugGenreName(name, libraryManager)); } protected GameGenre GetGameGenre(string name, ILibraryManager libraryManager) { return libraryManager.GetGameGenre(DeSlugGameGenreName(name, libraryManager)); } protected Person GetPerson(string name, ILibraryManager libraryManager) { return libraryManager.GetPerson(DeSlugPersonName(name, libraryManager)); } protected IEnumerable GetAllLibraryItems(Guid? userId, IUserManager userManager, ILibraryManager libraryManager) { if (userId.HasValue) { var user = userManager.GetUserById(userId.Value); return userManager.GetUserById(userId.Value).RootFolder.GetRecursiveChildren(user); } return libraryManager.RootFolder.RecursiveChildren; } /// /// Deslugs an artist name by finding the correct entry in the library /// /// /// /// protected string DeSlugArtistName(string name, ILibraryManager libraryManager) { if (name.IndexOf(SlugChar) == -1) { return name; } return libraryManager.RootFolder.RecursiveChildren .OfType