SystemService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.IO;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Serialization;
  8. using MediaBrowser.Model.System;
  9. using ServiceStack;
  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. [Api(Description = "Gets information about the server")]
  20. public class GetSystemInfo : IReturn<SystemInfo>
  21. {
  22. }
  23. /// <summary>
  24. /// Class RestartApplication
  25. /// </summary>
  26. [Route("/System/Restart", "POST")]
  27. [Api(("Restarts the application, if needed"))]
  28. public class RestartApplication
  29. {
  30. }
  31. [Route("/System/Shutdown", "POST")]
  32. [Api(("Shuts down the application"))]
  33. public class ShutdownApplication
  34. {
  35. }
  36. /// <summary>
  37. /// Class GetConfiguration
  38. /// </summary>
  39. [Route("/System/Configuration", "GET")]
  40. [Api(("Gets application configuration"))]
  41. public class GetConfiguration : IReturn<ServerConfiguration>
  42. {
  43. }
  44. /// <summary>
  45. /// Class UpdateConfiguration
  46. /// </summary>
  47. [Route("/System/Configuration", "POST")]
  48. [Api(("Updates application configuration"))]
  49. public class UpdateConfiguration : ServerConfiguration, IReturnVoid
  50. {
  51. }
  52. /// <summary>
  53. /// Class SystemInfoService
  54. /// </summary>
  55. public class SystemService : BaseApiService
  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 IServerApplicationHost _appHost;
  65. /// <summary>
  66. /// The _configuration manager
  67. /// </summary>
  68. private readonly IServerConfigurationManager _configurationManager;
  69. private readonly IFileSystem _fileSystem;
  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, IServerApplicationHost appHost, IServerConfigurationManager configurationManager, IFileSystem fileSystem)
  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. _fileSystem = fileSystem;
  91. _jsonSerializer = jsonSerializer;
  92. }
  93. /// <summary>
  94. /// Gets the specified request.
  95. /// </summary>
  96. /// <param name="request">The request.</param>
  97. /// <returns>System.Object.</returns>
  98. public object Get(GetSystemInfo request)
  99. {
  100. var result = _appHost.GetSystemInfo();
  101. return ToOptimizedResult(result);
  102. }
  103. /// <summary>
  104. /// Gets the specified request.
  105. /// </summary>
  106. /// <param name="request">The request.</param>
  107. /// <returns>System.Object.</returns>
  108. public object Get(GetConfiguration request)
  109. {
  110. var configPath = _configurationManager.ApplicationPaths.SystemConfigurationFilePath;
  111. var dateModified = _fileSystem.GetLastWriteTimeUtc(configPath);
  112. var cacheKey = (configPath + dateModified.Ticks).GetMD5();
  113. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration);
  114. }
  115. /// <summary>
  116. /// Posts the specified request.
  117. /// </summary>
  118. /// <param name="request">The request.</param>
  119. public void Post(RestartApplication request)
  120. {
  121. Task.Run(async () =>
  122. {
  123. await Task.Delay(100).ConfigureAwait(false);
  124. await _appHost.Restart().ConfigureAwait(false);
  125. });
  126. }
  127. /// <summary>
  128. /// Posts the specified request.
  129. /// </summary>
  130. /// <param name="request">The request.</param>
  131. public void Post(ShutdownApplication request)
  132. {
  133. Task.Run(async () =>
  134. {
  135. await Task.Delay(100).ConfigureAwait(false);
  136. await _appHost.Shutdown().ConfigureAwait(false);
  137. });
  138. }
  139. /// <summary>
  140. /// Posts the specified configuraiton.
  141. /// </summary>
  142. /// <param name="request">The request.</param>
  143. public void Post(UpdateConfiguration request)
  144. {
  145. // Silly, but we need to serialize and deserialize or the XmlSerializer will write the xml with an element name of UpdateConfiguration
  146. var json = _jsonSerializer.SerializeToString(request);
  147. var config = _jsonSerializer.DeserializeFromString<ServerConfiguration>(json);
  148. _configurationManager.ReplaceConfiguration(config);
  149. }
  150. }
  151. }