using MediaBrowser.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; using ServiceStack; using System; using System.Collections.Generic; using System.IO; using System.Linq; using ServiceStack.Text.Controller; using ServiceStack.Web; namespace MediaBrowser.Api { /// /// Class Plugins /// [Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")] public class GetPlugins : IReturn> { } /// /// Class UninstallPlugin /// [Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")] public class UninstallPlugin : IReturnVoid { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] public Guid Id { get; set; } } /// /// Class GetPluginConfiguration /// [Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")] public class GetPluginConfiguration { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public Guid Id { get; set; } } /// /// Class UpdatePluginConfiguration /// [Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")] public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public Guid Id { get; set; } /// /// The raw Http Request Input Stream /// /// The request stream. public Stream RequestStream { get; set; } } /// /// Class GetPluginSecurityInfo /// [Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")] public class GetPluginSecurityInfo : IReturn { } /// /// Class UpdatePluginSecurityInfo /// [Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")] public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid { } [Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")] public class GetRegistrationStatus { [ApiMember(Name = "Name", Description = "Feature Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } [ApiMember(Name = "Mb2Equivalent", Description = "Optional. The equivalent feature name in MB2", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string Mb2Equivalent { get; set; } } /// /// Class PluginsService /// [Authenticated] public class PluginService : BaseApiService { /// /// The _json serializer /// private readonly IJsonSerializer _jsonSerializer; /// /// The _app host /// private readonly IApplicationHost _appHost; private readonly ISecurityManager _securityManager; private readonly IInstallationManager _installationManager; /// /// Initializes a new instance of the class. /// /// The json serializer. /// The app host. /// The security manager. /// The installation manager. /// jsonSerializer public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager) : base() { if (jsonSerializer == null) { throw new ArgumentNullException("jsonSerializer"); } _appHost = appHost; _securityManager = securityManager; _installationManager = installationManager; _jsonSerializer = jsonSerializer; } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetRegistrationStatus request) { var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result; return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPlugins request) { var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList(); return ToOptimizedSerializedResultUsingCache(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPluginConfiguration request) { var plugin = _appHost.Plugins.First(p => p.Id == request.Id); var dateModified = plugin.ConfigurationDateLastModified; var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5(); return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPluginSecurityInfo request) { var result = new PluginSecurityInfo { IsMBSupporter = _securityManager.IsMBSupporter, SupporterKey = _securityManager.SupporterKey, LegacyKey = _securityManager.LegacyKey }; return ToOptimizedSerializedResultUsingCache(result); } /// /// Posts the specified request. /// /// The request. public void Post(UpdatePluginSecurityInfo request) { var info = request; _securityManager.SupporterKey = info.SupporterKey; _securityManager.LegacyKey = info.LegacyKey; } /// /// Posts the specified request. /// /// The request. public void Post(UpdatePluginConfiguration request) { // We need to parse this manually because we told service stack not to with IRequiresRequestStream // https://code.google.com/p/servicestack/source/browse/trunk/Common/ServiceStack.Text/ServiceStack.Text/Controller/PathInfo.cs var pathInfo = PathInfo.Parse(Request.PathInfo); var id = new Guid(pathInfo.GetArgumentValue(1)); var plugin = _appHost.Plugins.First(p => p.Id == id); var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration; plugin.UpdateConfiguration(configuration); } /// /// Deletes the specified request. /// /// The request. public void Delete(UninstallPlugin request) { var plugin = _appHost.Plugins.First(p => p.Id == request.Id); _installationManager.UninstallPlugin(plugin); } } }