using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Implementations.HttpServer; using MediaBrowser.Common.Kernel; using MediaBrowser.Controller; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.System; 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 { } [Route("/System/Shutdown", "POST")] public class ShutdownApplication { } /// /// 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; /// /// The _app host /// private readonly IApplicationHost _appHost; /// /// Initializes a new instance of the class. /// /// The json serializer. /// The app host. /// jsonSerializer public SystemService(IJsonSerializer jsonSerializer, IApplicationHost appHost) : base() { if (jsonSerializer == null) { throw new ArgumentNullException("jsonSerializer"); } if (appHost == null) { throw new ArgumentNullException("appHost"); } _appHost = appHost; _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 request. /// /// The request. public void Post(ShutdownApplication request) { Task.Run(async () => { await Task.Delay(100); _appHost.Shutdown(); }); } /// /// Posts the specified configuraiton. /// /// The request. public void Post(UpdateConfiguration request) { var serverConfig = _jsonSerializer.DeserializeFromStream(request.RequestStream); var kernel = (Kernel)Kernel; kernel.UpdateConfiguration(serverConfig); } } }