using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Implementations.HttpServer; using MediaBrowser.Controller; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; using ServiceStack.ServiceHost; using ServiceStack.Text.Controller; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace MediaBrowser.Api { /// /// Class Plugins /// [Route("/Plugins", "GET")] public class GetPlugins : IReturn> { } /// /// Class GetPluginAssembly /// [Route("/Plugins/{Id}/Assembly", "GET")] public class GetPluginAssembly { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } } /// /// Class UninstallPlugin /// [Route("/Plugins/{Id}", "DELETE")] public class UninstallPlugin : IReturnVoid { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } } /// /// Class GetPluginConfiguration /// [Route("/Plugins/{Id}/Configuration", "GET")] public class GetPluginConfiguration { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } } /// /// Class UpdatePluginConfiguration /// [Route("/Plugins/{Id}/Configuration", "POST")] public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } /// /// The raw Http Request Input Stream /// /// The request stream. public Stream RequestStream { get; set; } } /// /// Class GetPluginConfigurationFile /// [Route("/Plugins/{Id}/ConfigurationFile", "GET")] public class GetPluginConfigurationFile { /// /// Gets or sets the id. /// /// The id. public Guid Id { get; set; } } /// /// Class GetPluginSecurityInfo /// [Route("/Plugins/SecurityInfo", "GET")] public class GetPluginSecurityInfo : IReturn { } /// /// Class UpdatePluginSecurityInfo /// [Route("/Plugins/SecurityInfo", "GET")] public class UpdatePluginSecurityInfo : IReturnVoid, IRequiresRequestStream { /// /// The raw Http Request Input Stream /// /// The request stream. public Stream RequestStream { get; set; } } /// /// Class PluginsService /// public class PluginService : BaseRestService { /// /// The _json serializer /// private readonly IJsonSerializer _jsonSerializer; /// /// Initializes a new instance of the class. /// /// The json serializer. /// jsonSerializer public PluginService(IJsonSerializer jsonSerializer) : base() { if (jsonSerializer == null) { throw new ArgumentNullException("jsonSerializer"); } _jsonSerializer = jsonSerializer; } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPlugins request) { var result = Kernel.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList(); return ToOptimizedResult(result); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPluginAssembly request) { var plugin = Kernel.Plugins.First(p => p.Id == request.Id); return ToStaticFileResult(plugin.AssemblyFilePath); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPluginConfiguration request) { var plugin = Kernel.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(GetPluginConfigurationFile request) { var plugin = Kernel.Plugins.First(p => p.Id == request.Id); return ToStaticFileResult(plugin.ConfigurationFilePath); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetPluginSecurityInfo request) { var kernel = (Kernel)Kernel; var result = new PluginSecurityInfo { IsMBSupporter = kernel.SecurityManager.IsMBSupporter, SupporterKey = kernel.SecurityManager.SupporterKey, LegacyKey = kernel.SecurityManager.LegacyKey }; return ToOptimizedResult(result); } /// /// Posts the specified request. /// /// The request. public void Post(UpdatePluginSecurityInfo request) { var kernel = (Kernel)Kernel; var info = _jsonSerializer.DeserializeFromStream(request.RequestStream); kernel.SecurityManager.SupporterKey = info.SupporterKey; kernel.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 = Kernel.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 kernel = (Kernel)Kernel; var plugin = kernel.Plugins.First(p => p.Id == request.Id); kernel.InstallationManager.UninstallPlugin(plugin); } } }