jellyfin/MediaBrowser.Api/SystemService.cs

170 lines
5.2 KiB
C#
Raw Normal View History

2013-03-08 19:14:09 +00:00
using MediaBrowser.Common.Extensions;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Configuration;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Configuration;
2013-02-24 21:53:54 +00:00
using MediaBrowser.Model.Serialization;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.System;
using ServiceStack.ServiceHost;
2013-02-24 21:53:54 +00:00
using System;
2013-02-21 01:33:05 +00:00
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
2013-02-24 21:53:54 +00:00
/// <summary>
/// Class GetSystemInfo
/// </summary>
2013-02-21 01:33:05 +00:00
[Route("/System/Info", "GET")]
2013-04-10 15:32:09 +00:00
[Api(Description = "Gets information about the server")]
2013-02-21 01:33:05 +00:00
public class GetSystemInfo : IReturn<SystemInfo>
{
}
/// <summary>
/// Class RestartApplication
/// </summary>
[Route("/System/Restart", "POST")]
2013-04-10 15:32:09 +00:00
[Api(("Restarts the application, if needed"))]
2013-02-21 01:33:05 +00:00
public class RestartApplication
{
}
[Route("/System/Shutdown", "POST")]
2013-04-10 15:32:09 +00:00
[Api(("Shuts down the application"))]
public class ShutdownApplication
{
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Class GetConfiguration
/// </summary>
[Route("/System/Configuration", "GET")]
2013-04-10 15:32:09 +00:00
[Api(("Gets application configuration"))]
2013-02-21 01:33:05 +00:00
public class GetConfiguration : IReturn<ServerConfiguration>
{
}
/// <summary>
/// Class UpdateConfiguration
/// </summary>
[Route("/System/Configuration", "POST")]
2013-04-10 15:32:09 +00:00
[Api(("Updates application configuration"))]
2013-03-05 02:05:59 +00:00
public class UpdateConfiguration : ServerConfiguration, IReturnVoid
2013-02-21 01:33:05 +00:00
{
}
/// <summary>
/// Class SystemInfoService
/// </summary>
2013-03-16 05:52:33 +00:00
public class SystemService : 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>
2013-03-07 05:34:00 +00:00
private readonly IServerApplicationHost _appHost;
2013-03-04 05:43:06 +00:00
/// <summary>
/// The _configuration manager
/// </summary>
private readonly IServerConfigurationManager _configurationManager;
2013-02-24 21:53:54 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="SystemService" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="appHost">The app host.</param>
2013-03-04 05:43:06 +00:00
/// <param name="configurationManager">The configuration manager.</param>
2013-02-24 21:53:54 +00:00
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
2013-03-07 05:34:00 +00:00
public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appHost, IServerConfigurationManager configurationManager)
2013-02-24 21:53:54 +00:00
: base()
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (appHost == null)
{
throw new ArgumentNullException("appHost");
}
2013-02-24 21:53:54 +00:00
_appHost = appHost;
2013-03-04 05:43:06 +00:00
_configurationManager = configurationManager;
2013-02-24 21:53:54 +00:00
_jsonSerializer = jsonSerializer;
}
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(GetSystemInfo request)
{
2013-03-07 05:34:00 +00:00
var result = _appHost.GetSystemInfo();
2013-02-21 01:33:05 +00:00
return ToOptimizedResult(result);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetConfiguration request)
{
2013-03-04 05:43:06 +00:00
var dateModified = File.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);
2013-02-21 01:33:05 +00:00
2013-03-04 05:43:06 +00:00
var cacheKey = (_configurationManager.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();
2013-02-21 01:33:05 +00:00
2013-03-04 05:43:06 +00:00
return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(RestartApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Restart().ConfigureAwait(false);
2013-02-21 01:33:05 +00:00
});
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(ShutdownApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Shutdown().ConfigureAwait(false);
});
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Posts the specified configuraiton.
/// </summary>
/// <param name="request">The request.</param>
public void Post(UpdateConfiguration request)
{
2013-03-05 02:05:59 +00:00
// Silly, but we need to serialize and deserialize or the XmlSerializer will write the xml with an element name of UpdateConfiguration
var json = _jsonSerializer.SerializeToString(request);
var config = _jsonSerializer.DeserializeFromString<ServerConfiguration>(json);
2013-02-24 21:53:54 +00:00
2013-03-05 02:05:59 +00:00
_configurationManager.ReplaceConfiguration(config);
2013-02-21 01:33:05 +00:00
}
}
}