using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Serialization; using ServiceStack; using ServiceStack.Text.Controller; using ServiceStack.Web; using System.Collections.Generic; using System.IO; using System.Linq; namespace MediaBrowser.Api { /// /// Class GetConfiguration /// [Route("/System/Configuration", "GET", Summary = "Gets application configuration")] [Authenticated] public class GetConfiguration : IReturn { } [Route("/System/Configuration/{Key}", "GET", Summary = "Gets a named configuration")] [Authenticated] public class GetNamedConfiguration { [ApiMember(Name = "Key", Description = "Key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Key { get; set; } } /// /// Class UpdateConfiguration /// [Route("/System/Configuration", "POST", Summary = "Updates application configuration")] [Authenticated(Roles = "Admin")] public class UpdateConfiguration : ServerConfiguration, IReturnVoid { } [Route("/System/Configuration/{Key}", "POST", Summary = "Updates named configuration")] [Authenticated(Roles = "Admin")] public class UpdateNamedConfiguration : IReturnVoid, IRequiresRequestStream { [ApiMember(Name = "Key", Description = "Key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Key { get; set; } public Stream RequestStream { get; set; } } [Route("/System/Configuration/MetadataOptions/Default", "GET", Summary = "Gets a default MetadataOptions object")] [Authenticated(Roles = "Admin")] public class GetDefaultMetadataOptions : IReturn { } [Route("/System/Configuration/MetadataPlugins", "GET", Summary = "Gets all available metadata plugins")] [Authenticated(Roles = "Admin")] public class GetMetadataPlugins : IReturn> { } [Route("/System/Configuration/MetadataPlugins/Autoset", "POST")] [Authenticated(Roles = "Admin", AllowBeforeStartupWizard = true)] public class AutoSetMetadataOptions : IReturnVoid { } public class ConfigurationService : BaseApiService { /// /// The _json serializer /// private readonly IJsonSerializer _jsonSerializer; /// /// The _configuration manager /// private readonly IServerConfigurationManager _configurationManager; private readonly IFileSystem _fileSystem; private readonly IProviderManager _providerManager; private readonly ILibraryManager _libraryManager; public ConfigurationService(IJsonSerializer jsonSerializer, IServerConfigurationManager configurationManager, IFileSystem fileSystem, IProviderManager providerManager, ILibraryManager libraryManager) { _jsonSerializer = jsonSerializer; _configurationManager = configurationManager; _fileSystem = fileSystem; _providerManager = providerManager; _libraryManager = libraryManager; } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetConfiguration request) { var configPath = _configurationManager.ApplicationPaths.SystemConfigurationFilePath; var dateModified = _fileSystem.GetLastWriteTimeUtc(configPath); var cacheKey = (configPath + dateModified.Ticks).GetMD5(); return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration); } public object Get(GetNamedConfiguration request) { var result = _configurationManager.GetConfiguration(request.Key); return ToOptimizedResult(result); } public void Post(AutoSetMetadataOptions request) { _configurationManager.DisableMetadataService("Media Browser Legacy Xml"); _configurationManager.SaveConfiguration(); } /// /// Posts the specified configuraiton. /// /// The request. public void Post(UpdateConfiguration request) { // Silly, but we need to serialize and deserialize or the XmlSerializer will write the xml with an element name of UpdateConfiguration var json = _jsonSerializer.SerializeToString(request); var config = _jsonSerializer.DeserializeFromString(json); _configurationManager.ReplaceConfiguration(config); } public void Post(UpdateNamedConfiguration request) { var pathInfo = PathInfo.Parse(Request.PathInfo); var key = pathInfo.GetArgumentValue(2); var configurationType = _configurationManager.GetConfigurationType(key); var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, configurationType); _configurationManager.SaveConfiguration(key, configuration); } public object Get(GetDefaultMetadataOptions request) { return ToOptimizedSerializedResultUsingCache(new MetadataOptions()); } public object Get(GetMetadataPlugins request) { return ToOptimizedSerializedResultUsingCache(_providerManager.GetAllMetadataPlugins().ToList()); } } }