jellyfin/MediaBrowser.Api/PluginService.cs

277 lines
9.4 KiB
C#
Raw Normal View History

2013-03-04 06:43:06 +01:00
using MediaBrowser.Common;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Security;
using MediaBrowser.Controller.Updates;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Plugins;
2013-02-24 22:53:54 +01:00
using MediaBrowser.Model.Serialization;
2013-03-07 06:34:00 +01:00
using MediaBrowser.Server.Implementations.HttpServer;
2013-02-21 02:33:05 +01:00
using ServiceStack.ServiceHost;
2013-02-24 22:53:54 +01:00
using ServiceStack.Text.Controller;
2013-02-21 02:33:05 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Api
{
/// <summary>
/// Class Plugins
/// </summary>
[Route("/Plugins", "GET")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Gets a list of currently installed plugins"))]
2013-02-21 02:33:05 +01:00
public class GetPlugins : IReturn<List<PluginInfo>>
{
}
/// <summary>
/// Class GetPluginAssembly
/// </summary>
[Route("/Plugins/{Id}/Assembly", "GET")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Gets a plugin assembly file"))]
2013-02-21 02:33:05 +01:00
public class GetPluginAssembly
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 02:33:05 +01:00
public Guid Id { get; set; }
}
/// <summary>
/// Class UninstallPlugin
/// </summary>
[Route("/Plugins/{Id}", "DELETE")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Uninstalls a plugin"))]
2013-02-21 02:33:05 +01:00
public class UninstallPlugin : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 02:33:05 +01:00
public Guid Id { get; set; }
}
/// <summary>
/// Class GetPluginConfiguration
/// </summary>
[Route("/Plugins/{Id}/Configuration", "GET")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Gets a plugin's configuration"))]
2013-02-21 02:33:05 +01:00
public class GetPluginConfiguration
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 02:33:05 +01:00
public Guid Id { get; set; }
}
/// <summary>
/// Class UpdatePluginConfiguration
/// </summary>
[Route("/Plugins/{Id}/Configuration", "POST")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Updates a plugin's configuration"))]
2013-02-21 02:33:05 +01:00
public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 02:33:05 +01:00
public Guid Id { get; set; }
/// <summary>
/// The raw Http Request Input Stream
/// </summary>
/// <value>The request stream.</value>
public Stream RequestStream { get; set; }
}
/// <summary>
/// Class GetPluginConfigurationFile
/// </summary>
[Route("/Plugins/{Id}/ConfigurationFile", "GET")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Gets a plugin's configuration file, in plain text"))]
2013-02-21 02:33:05 +01:00
public class GetPluginConfigurationFile
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 20:14:09 +01:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 02:33:05 +01:00
public Guid Id { get; set; }
}
/// <summary>
/// Class GetPluginSecurityInfo
/// </summary>
[Route("/Plugins/SecurityInfo", "GET")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Gets plugin registration information"))]
2013-02-21 02:33:05 +01:00
public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
{
}
/// <summary>
/// Class UpdatePluginSecurityInfo
/// </summary>
2013-03-02 18:51:22 +01:00
[Route("/Plugins/SecurityInfo", "POST")]
2013-03-08 20:14:09 +01:00
[ServiceStack.ServiceHost.Api(("Updates plugin registration information"))]
2013-03-05 03:05:59 +01:00
public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
2013-02-21 02:33:05 +01:00
{
}
/// <summary>
/// Class PluginsService
/// </summary>
public class PluginService : BaseRestService
{
2013-02-24 22:53:54 +01:00
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// The _app host
/// </summary>
private readonly IApplicationHost _appHost;
2013-03-04 06:43:06 +01:00
private readonly ISecurityManager _securityManager;
private readonly IInstallationManager _installationManager;
2013-02-24 22:53:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="PluginService" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="appHost">The app host.</param>
2013-03-04 06:43:06 +01:00
/// <param name="securityManager">The security manager.</param>
2013-02-24 22:53:54 +01:00
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
2013-02-24 22:53:54 +01:00
: base()
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
_appHost = appHost;
2013-03-04 06:43:06 +01:00
_securityManager = securityManager;
_installationManager = installationManager;
2013-02-24 22:53:54 +01:00
_jsonSerializer = jsonSerializer;
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPlugins request)
{
var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
2013-02-21 02:33:05 +01:00
return ToOptimizedResult(result);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPluginAssembly request)
{
var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
2013-02-21 02:33:05 +01:00
return ToStaticFileResult(plugin.AssemblyFilePath);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPluginConfiguration request)
{
var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
2013-02-21 02:33:05 +01:00
var dateModified = plugin.ConfigurationDateLastModified;
var cacheKey = (plugin.Version.ToString() + dateModified.Ticks).GetMD5();
return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => plugin.Configuration);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPluginConfigurationFile request)
{
var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
2013-02-21 02:33:05 +01:00
return ToStaticFileResult(plugin.ConfigurationFilePath);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPluginSecurityInfo request)
{
var result = new PluginSecurityInfo
{
2013-03-04 06:43:06 +01:00
IsMBSupporter = _securityManager.IsMBSupporter,
SupporterKey = _securityManager.SupporterKey,
LegacyKey = _securityManager.LegacyKey
2013-02-21 02:33:05 +01:00
};
return ToOptimizedResult(result);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdatePluginSecurityInfo request)
{
2013-03-05 03:05:59 +01:00
var info = request;
2013-02-21 02:33:05 +01:00
2013-03-04 06:43:06 +01:00
_securityManager.SupporterKey = info.SupporterKey;
_securityManager.LegacyKey = info.LegacyKey;
2013-02-21 02:33:05 +01:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
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<string>(1));
var plugin = _appHost.Plugins.First(p => p.Id == id);
2013-02-21 02:33:05 +01:00
2013-02-24 22:53:54 +01:00
var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
2013-02-21 02:33:05 +01:00
plugin.UpdateConfiguration(configuration);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Delete(UninstallPlugin request)
{
var plugin = _appHost.Plugins.First(p => p.Id == request.Id);
2013-02-21 02:33:05 +01:00
_installationManager.UninstallPlugin(plugin);
2013-02-21 02:33:05 +01:00
}
}
}