using System.Linq; using System.Threading.Tasks; 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; 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 Task GetArtist(string name, ILibraryManager libraryManager) { return libraryManager.GetArtist(DeSlugArtistName(name, libraryManager)); } protected Task GetStudio(string name, ILibraryManager libraryManager) { return libraryManager.GetStudio(DeSlugStudioName(name, libraryManager)); } protected Task GetGenre(string name, ILibraryManager libraryManager) { return libraryManager.GetGenre(DeSlugGenreName(name, libraryManager)); } protected Task GetPerson(string name, ILibraryManager libraryManager) { return libraryManager.GetPerson(DeSlugPersonName(name, libraryManager)); } /// /// 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