SystemService.cs 5.5 KB

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