SystemService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Implementations.HttpServer;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Serialization;
  8. using MediaBrowser.Model.System;
  9. using ServiceStack.ServiceHost;
  10. using System;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api
  14. {
  15. /// <summary>
  16. /// Class GetSystemInfo
  17. /// </summary>
  18. [Route("/System/Info", "GET")]
  19. public class GetSystemInfo : IReturn<SystemInfo>
  20. {
  21. }
  22. /// <summary>
  23. /// Class RestartApplication
  24. /// </summary>
  25. [Route("/System/Restart", "POST")]
  26. [ServiceStack.ServiceHost.Api(("Restarts the application, if needed"))]
  27. public class RestartApplication
  28. {
  29. }
  30. [Route("/System/Shutdown", "POST")]
  31. public class ShutdownApplication
  32. {
  33. }
  34. /// <summary>
  35. /// Class GetConfiguration
  36. /// </summary>
  37. [Route("/System/Configuration", "GET")]
  38. public class GetConfiguration : IReturn<ServerConfiguration>
  39. {
  40. }
  41. /// <summary>
  42. /// Class UpdateConfiguration
  43. /// </summary>
  44. [Route("/System/Configuration", "POST")]
  45. public class UpdateConfiguration : IRequiresRequestStream
  46. {
  47. /// <summary>
  48. /// The raw Http Request Input Stream
  49. /// </summary>
  50. /// <value>The request stream.</value>
  51. public Stream RequestStream { get; set; }
  52. }
  53. /// <summary>
  54. /// Class SystemInfoService
  55. /// </summary>
  56. public class SystemService : BaseRestService
  57. {
  58. /// <summary>
  59. /// The _json serializer
  60. /// </summary>
  61. private readonly IJsonSerializer _jsonSerializer;
  62. /// <summary>
  63. /// The _app host
  64. /// </summary>
  65. private readonly IApplicationHost _appHost;
  66. /// <summary>
  67. /// The _configuration manager
  68. /// </summary>
  69. private readonly IServerConfigurationManager _configurationManager;
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="SystemService" /> class.
  72. /// </summary>
  73. /// <param name="jsonSerializer">The json serializer.</param>
  74. /// <param name="appHost">The app host.</param>
  75. /// <param name="configurationManager">The configuration manager.</param>
  76. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  77. public SystemService(IJsonSerializer jsonSerializer, IApplicationHost appHost, IServerConfigurationManager configurationManager)
  78. : base()
  79. {
  80. if (jsonSerializer == null)
  81. {
  82. throw new ArgumentNullException("jsonSerializer");
  83. }
  84. if (appHost == null)
  85. {
  86. throw new ArgumentNullException("appHost");
  87. }
  88. _appHost = appHost;
  89. _configurationManager = configurationManager;
  90. _jsonSerializer = jsonSerializer;
  91. }
  92. /// <summary>
  93. /// Gets the specified request.
  94. /// </summary>
  95. /// <param name="request">The request.</param>
  96. /// <returns>System.Object.</returns>
  97. public object Get(GetSystemInfo request)
  98. {
  99. var result = Kernel.Instance.GetSystemInfo();
  100. return ToOptimizedResult(result);
  101. }
  102. /// <summary>
  103. /// Gets the specified request.
  104. /// </summary>
  105. /// <param name="request">The request.</param>
  106. /// <returns>System.Object.</returns>
  107. public object Get(GetConfiguration request)
  108. {
  109. var dateModified = File.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);
  110. var cacheKey = (_configurationManager.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();
  111. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration);
  112. }
  113. /// <summary>
  114. /// Posts the specified request.
  115. /// </summary>
  116. /// <param name="request">The request.</param>
  117. public void Post(RestartApplication request)
  118. {
  119. Task.Run(async () =>
  120. {
  121. await Task.Delay(100);
  122. Kernel.Instance.PerformPendingRestart();
  123. });
  124. }
  125. /// <summary>
  126. /// Posts the specified request.
  127. /// </summary>
  128. /// <param name="request">The request.</param>
  129. public void Post(ShutdownApplication request)
  130. {
  131. Task.Run(async () =>
  132. {
  133. await Task.Delay(100);
  134. _appHost.Shutdown();
  135. });
  136. }
  137. /// <summary>
  138. /// Posts the specified configuraiton.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. public void Post(UpdateConfiguration request)
  142. {
  143. var serverConfig = _jsonSerializer.DeserializeFromStream<ServerConfiguration>(request.RequestStream);
  144. _configurationManager.ReplaceConfiguration(serverConfig);
  145. }
  146. }
  147. }