SystemService.cs 4.7 KB

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