SystemService.cs 3.8 KB

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