SystemService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.System;
  7. using ServiceStack;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.System
  14. {
  15. /// <summary>
  16. /// Class GetSystemInfo
  17. /// </summary>
  18. [Route("/System/Info", "GET", Summary = "Gets information about the server")]
  19. [Authenticated]
  20. public class GetSystemInfo : IReturn<SystemInfo>
  21. {
  22. }
  23. [Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
  24. public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
  25. {
  26. }
  27. /// <summary>
  28. /// Class RestartApplication
  29. /// </summary>
  30. [Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
  31. [Authenticated]
  32. public class RestartApplication
  33. {
  34. }
  35. /// <summary>
  36. /// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
  37. /// </summary>
  38. [Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
  39. public class ShutdownApplication
  40. {
  41. }
  42. [Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
  43. [Authenticated]
  44. public class GetServerLogs : IReturn<List<LogFile>>
  45. {
  46. }
  47. [Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
  48. [Authenticated]
  49. public class GetEndpointInfo : IReturn<EndpointInfo>
  50. {
  51. public string Endpoint { get; set; }
  52. }
  53. [Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
  54. public class GetLogFile
  55. {
  56. [ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
  57. public string Name { get; set; }
  58. }
  59. /// <summary>
  60. /// Class SystemInfoService
  61. /// </summary>
  62. public class SystemService : BaseApiService
  63. {
  64. /// <summary>
  65. /// The _app host
  66. /// </summary>
  67. private readonly IServerApplicationHost _appHost;
  68. private readonly IApplicationPaths _appPaths;
  69. private readonly IFileSystem _fileSystem;
  70. private readonly INetworkManager _network;
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="SystemService" /> class.
  73. /// </summary>
  74. /// <param name="appHost">The app host.</param>
  75. /// <param name="appPaths">The application paths.</param>
  76. /// <param name="fileSystem">The file system.</param>
  77. /// <exception cref="ArgumentNullException">jsonSerializer</exception>
  78. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network)
  79. {
  80. _appHost = appHost;
  81. _appPaths = appPaths;
  82. _fileSystem = fileSystem;
  83. _network = network;
  84. }
  85. public object Get(GetServerLogs request)
  86. {
  87. List<FileInfo> files;
  88. try
  89. {
  90. files = new DirectoryInfo(_appPaths.LogDirectoryPath)
  91. .EnumerateFiles("*", SearchOption.AllDirectories)
  92. .Where(i => string.Equals(i.Extension, ".txt", global::System.StringComparison.OrdinalIgnoreCase))
  93. .ToList();
  94. }
  95. catch (DirectoryNotFoundException)
  96. {
  97. files = new List<FileInfo>();
  98. }
  99. var result = files.Select(i => new LogFile
  100. {
  101. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  102. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  103. Name = i.Name,
  104. Size = i.Length
  105. }).OrderByDescending(i => i.DateModified)
  106. .ThenByDescending(i => i.DateCreated)
  107. .ThenBy(i => i.Name)
  108. .ToList();
  109. return ToOptimizedResult(result);
  110. }
  111. public object Get(GetLogFile request)
  112. {
  113. var file = new DirectoryInfo(_appPaths.LogDirectoryPath)
  114. .EnumerateFiles("*", SearchOption.AllDirectories)
  115. .First(i => string.Equals(i.Name, request.Name, global::System.StringComparison.OrdinalIgnoreCase));
  116. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
  117. }
  118. /// <summary>
  119. /// Gets the specified request.
  120. /// </summary>
  121. /// <param name="request">The request.</param>
  122. /// <returns>System.Object.</returns>
  123. public object Get(GetSystemInfo request)
  124. {
  125. var result = _appHost.GetSystemInfo();
  126. return ToOptimizedResult(result);
  127. }
  128. public object Get(GetPublicSystemInfo request)
  129. {
  130. var result = _appHost.GetSystemInfo();
  131. var publicInfo = new PublicSystemInfo
  132. {
  133. Id = result.Id,
  134. ServerName = result.ServerName,
  135. Version = result.Version
  136. };
  137. return ToOptimizedResult(publicInfo);
  138. }
  139. /// <summary>
  140. /// Posts the specified request.
  141. /// </summary>
  142. /// <param name="request">The request.</param>
  143. public void Post(RestartApplication request)
  144. {
  145. Task.Run(async () =>
  146. {
  147. await Task.Delay(100).ConfigureAwait(false);
  148. await _appHost.Restart().ConfigureAwait(false);
  149. });
  150. }
  151. /// <summary>
  152. /// Posts the specified request.
  153. /// </summary>
  154. /// <param name="request">The request.</param>
  155. public void Post(ShutdownApplication request)
  156. {
  157. Task.Run(async () =>
  158. {
  159. await Task.Delay(100).ConfigureAwait(false);
  160. await _appHost.Shutdown().ConfigureAwait(false);
  161. });
  162. }
  163. public object Get(GetEndpointInfo request)
  164. {
  165. return ToOptimizedResult(new EndpointInfo
  166. {
  167. IsLocal = Request.IsLocal,
  168. IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
  169. });
  170. }
  171. }
  172. public class EndpointInfo
  173. {
  174. public bool IsLocal { get; set; }
  175. public bool IsInNetwork { get; set; }
  176. }
  177. }