2
0

SystemService.cs 6.3 KB

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