using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
using MediaBrowser.Networking.HttpServer;
using ServiceStack.ServiceHost;
using System;
using System.IO;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
///
/// Class GetSystemInfo
///
[Route("/System/Info", "GET")]
public class GetSystemInfo : IReturn
{
}
///
/// Class RestartApplication
///
[Route("/System/Restart", "POST")]
[ServiceStack.ServiceHost.Api(("Restarts the application, if needed"))]
public class RestartApplication
{
}
///
/// Class GetConfiguration
///
[Route("/System/Configuration", "GET")]
public class GetConfiguration : IReturn
{
}
///
/// Class UpdateConfiguration
///
[Route("/System/Configuration", "POST")]
public class UpdateConfiguration : IRequiresRequestStream
{
///
/// The raw Http Request Input Stream
///
/// The request stream.
public Stream RequestStream { get; set; }
}
///
/// Class SystemInfoService
///
public class SystemService : BaseRestService
{
///
/// The _json serializer
///
private readonly IJsonSerializer _jsonSerializer;
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// jsonSerializer
public SystemService(IJsonSerializer jsonSerializer)
: base()
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
_jsonSerializer = jsonSerializer;
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetSystemInfo request)
{
var result = Kernel.GetSystemInfo();
return ToOptimizedResult(result);
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetConfiguration request)
{
var kernel = (Kernel)Kernel;
var dateModified = File.GetLastWriteTimeUtc(Kernel.ApplicationPaths.SystemConfigurationFilePath);
var cacheKey = (Kernel.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();
return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => kernel.Configuration);
}
///
/// Posts the specified request.
///
/// The request.
public void Post(RestartApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100);
Kernel.PerformPendingRestart();
});
}
///
/// Posts the specified configuraiton.
///
/// The request.
public void Post(UpdateConfiguration request)
{
var serverConfig = _jsonSerializer.DeserializeFromStream(request.RequestStream);
var kernel = (Kernel)Kernel;
kernel.UpdateConfiguration(serverConfig);
}
}
}