jellyfin/MediaBrowser.Api/PluginService.cs

259 lines
7.9 KiB
C#
Raw Normal View History

2013-02-20 18:33:05 -07:00
using MediaBrowser.Common.Extensions;
2013-02-26 14:05:52 -07:00
using MediaBrowser.Common.Implementations.HttpServer;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Plugins;
2013-02-24 14:53:54 -07:00
using MediaBrowser.Model.Serialization;
2013-02-20 18:33:05 -07:00
using ServiceStack.ServiceHost;
2013-02-24 14:53:54 -07:00
using ServiceStack.Text.Controller;
2013-02-20 18:33:05 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Api
{
/// <summary>
/// Class Plugins
/// </summary>
[Route("/Plugins", "GET")]
public class GetPlugins : IReturn<List<PluginInfo>>
{
}
/// <summary>
/// Class GetPluginAssembly
/// </summary>
[Route("/Plugins/{Id}/Assembly", "GET")]
public class GetPluginAssembly
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public Guid Id { get; set; }
}
/// <summary>
/// Class UninstallPlugin
/// </summary>
[Route("/Plugins/{Id}", "DELETE")]
public class UninstallPlugin : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public Guid Id { get; set; }
}
/// <summary>
/// Class GetPluginConfiguration
/// </summary>
[Route("/Plugins/{Id}/Configuration", "GET")]
public class GetPluginConfiguration
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public Guid Id { get; set; }
}
/// <summary>
/// Class UpdatePluginConfiguration
/// </summary>
[Route("/Plugins/{Id}/Configuration", "POST")]
public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
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")]
public class GetPluginConfigurationFile
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public Guid Id { get; set; }
}
/// <summary>
/// Class GetPluginSecurityInfo
/// </summary>
[Route("/Plugins/SecurityInfo", "GET")]
public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
{
}
/// <summary>
/// Class UpdatePluginSecurityInfo
/// </summary>
[Route("/Plugins/SecurityInfo", "GET")]
public class UpdatePluginSecurityInfo : IReturnVoid, IRequiresRequestStream
{
/// <summary>
/// The raw Http Request Input Stream
/// </summary>
/// <value>The request stream.</value>
public Stream RequestStream { get; set; }
}
/// <summary>
/// Class PluginsService
/// </summary>
public class PluginService : BaseRestService
{
2013-02-24 14:53:54 -07:00
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="PluginService" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
public PluginService(IJsonSerializer jsonSerializer)
: base()
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
_jsonSerializer = jsonSerializer;
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetPlugins request)
{
var result = Kernel.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
2013-02-20 18:33:05 -07: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)
{
2013-02-21 23:28:57 -07:00
var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
2013-02-20 18:33:05 -07: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)
{
2013-02-21 23:28:57 -07:00
var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
2013-02-20 18:33:05 -07: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)
{
2013-02-21 23:28:57 -07:00
var plugin = Kernel.Plugins.First(p => p.Id == request.Id);
2013-02-20 18:33:05 -07: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 kernel = (Kernel)Kernel;
var result = new PluginSecurityInfo
{
IsMBSupporter = kernel.SecurityManager.IsMBSupporter,
SupporterKey = kernel.SecurityManager.SupporterKey,
LegacyKey = kernel.SecurityManager.LegacyKey
2013-02-20 18:33:05 -07:00
};
return ToOptimizedResult(result);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdatePluginSecurityInfo request)
{
var kernel = (Kernel)Kernel;
2013-02-24 14:53:54 -07:00
var info = _jsonSerializer.DeserializeFromStream<PluginSecurityInfo>(request.RequestStream);
2013-02-20 18:33:05 -07:00
kernel.SecurityManager.SupporterKey = info.SupporterKey;
kernel.SecurityManager.LegacyKey = info.LegacyKey;
2013-02-20 18:33:05 -07: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));
2013-02-21 23:28:57 -07:00
var plugin = Kernel.Plugins.First(p => p.Id == id);
2013-02-20 18:33:05 -07:00
2013-02-24 14:53:54 -07:00
var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
2013-02-20 18:33:05 -07:00
plugin.UpdateConfiguration(configuration);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Delete(UninstallPlugin request)
{
var kernel = (Kernel)Kernel;
2013-02-21 23:28:57 -07:00
var plugin = kernel.Plugins.First(p => p.Id == request.Id);
2013-02-20 18:33:05 -07:00
kernel.InstallationManager.UninstallPlugin(plugin);
}
}
}