jellyfin-server/MediaBrowser.Api/PluginService.cs

260 lines
8.7 KiB
C#
Raw Normal View History

2015-01-10 05:53:35 +00:00
using System.Threading;
using MediaBrowser.Common;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Security;
using MediaBrowser.Common.Updates;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Plugins;
2013-02-24 21:53:54 +00:00
using MediaBrowser.Model.Serialization;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2014-08-31 02:08:59 +00:00
using ServiceStack.Text.Controller;
using ServiceStack.Web;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2015-01-10 05:53:35 +00:00
using System.Threading.Tasks;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class Plugins
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins", "GET", Summary = "Gets a list of currently installed plugins")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2013-02-21 01:33:05 +00:00
public class GetPlugins : IReturn<List<PluginInfo>>
{
}
/// <summary>
/// Class UninstallPlugin
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins/{Id}", "DELETE", Summary = "Uninstalls a plugin")]
2014-11-15 02:31:03 +00:00
[Authenticated(Roles = "Admin")]
2013-02-21 01:33:05 +00:00
public class UninstallPlugin : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2013-02-21 01:33:05 +00:00
public Guid Id { get; set; }
}
/// <summary>
/// Class GetPluginConfiguration
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins/{Id}/Configuration", "GET", Summary = "Gets a plugin's configuration")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2013-02-21 01:33:05 +00:00
public class GetPluginConfiguration
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-21 01:33:05 +00:00
public Guid Id { get; set; }
}
/// <summary>
/// Class UpdatePluginConfiguration
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins/{Id}/Configuration", "POST", Summary = "Updates a plugin's configuration")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2013-02-21 01:33:05 +00:00
public class UpdatePluginConfiguration : IRequiresRequestStream, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 19:14:09 +00:00
[ApiMember(Name = "Id", Description = "Plugin Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-02-21 01:33:05 +00: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 GetPluginSecurityInfo
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins/SecurityInfo", "GET", Summary = "Gets plugin registration information")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2013-02-21 01:33:05 +00:00
public class GetPluginSecurityInfo : IReturn<PluginSecurityInfo>
{
}
/// <summary>
/// Class UpdatePluginSecurityInfo
/// </summary>
2014-03-23 20:07:02 +00:00
[Route("/Plugins/SecurityInfo", "POST", Summary = "Updates plugin registration information")]
2014-11-15 02:31:03 +00:00
[Authenticated(Roles = "Admin")]
2013-03-05 02:05:59 +00:00
public class UpdatePluginSecurityInfo : PluginSecurityInfo, IReturnVoid
2013-02-21 01:33:05 +00:00
{
}
2014-03-23 20:07:02 +00:00
[Route("/Plugins/RegistrationRecords/{Name}", "GET", Summary = "Gets registration status for a feature")]
2014-11-15 02:31:03 +00:00
[Authenticated]
2013-04-08 00:26:31 +00:00
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; }
}
2014-11-15 02:31:03 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Class PluginsService
/// </summary>
2013-03-16 05:52:33 +00:00
public class PluginService : BaseApiService
2013-02-21 01:33:05 +00:00
{
2013-02-24 21:53:54 +00:00
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
/// The _app host
/// </summary>
private readonly IApplicationHost _appHost;
2013-03-04 05:43:06 +00:00
private readonly ISecurityManager _securityManager;
private readonly IInstallationManager _installationManager;
public PluginService(IJsonSerializer jsonSerializer, IApplicationHost appHost, ISecurityManager securityManager, IInstallationManager installationManager)
2013-02-24 21:53:54 +00:00
: base()
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
_appHost = appHost;
2013-03-04 05:43:06 +00:00
_securityManager = securityManager;
_installationManager = installationManager;
2013-02-24 21:53:54 +00:00
_jsonSerializer = jsonSerializer;
}
2013-04-08 00:26:31 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetRegistrationStatus request)
{
var result = _securityManager.GetRegistrationStatus(request.Name, request.Mb2Equivalent).Result;
return ToOptimizedResult(result);
}
2014-11-15 02:31:03 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2015-01-10 05:53:35 +00:00
public async Task<object> Get(GetPlugins request)
2013-02-21 01:33:05 +00:00
{
var result = _appHost.Plugins.OrderBy(p => p.Name).Select(p => p.GetPluginInfo()).ToList();
2015-01-10 05:53:35 +00:00
// Don't fail just on account of image url's
try
{
var packages = (await _installationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None))
.ToList();
foreach (var plugin in result)
{
var pkg = packages.FirstOrDefault(i => !string.IsNullOrWhiteSpace(i.guid) && new Guid(plugin.Id).Equals(new Guid(i.guid)));
if (pkg != null)
{
plugin.ImageUrl = pkg.thumbImage;
}
}
}
catch
{
}
return ToOptimizedSerializedResultUsingCache(result);
2013-02-21 01:33:05 +00:00
}
/// <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 01:33:05 +00: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(GetPluginSecurityInfo request)
{
var result = new PluginSecurityInfo
{
2013-03-04 05:43:06 +00:00
IsMBSupporter = _securityManager.IsMBSupporter,
2014-08-31 02:08:59 +00:00
SupporterKey = _securityManager.SupporterKey
2013-02-21 01:33:05 +00:00
};
return ToOptimizedSerializedResultUsingCache(result);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdatePluginSecurityInfo request)
{
2013-03-05 02:05:59 +00:00
var info = request;
2013-02-21 01:33:05 +00:00
2013-03-04 05:43:06 +00:00
_securityManager.SupporterKey = info.SupporterKey;
2013-02-21 01:33:05 +00: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
2015-01-22 16:41:34 +00:00
var id = new Guid(GetPathValue(1));
2013-02-21 01:33:05 +00:00
var plugin = _appHost.Plugins.First(p => p.Id == id);
2013-02-21 01:33:05 +00:00
2013-02-24 21:53:54 +00:00
var configuration = _jsonSerializer.DeserializeFromStream(request.RequestStream, plugin.ConfigurationType) as BasePluginConfiguration;
2013-02-21 01:33:05 +00: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 01:33:05 +00:00
_installationManager.UninstallPlugin(plugin);
2013-02-21 01:33:05 +00:00
}
}
}