SystemService.cs 5.5 KB

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