SystemService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  65. public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem)
  66. {
  67. _appHost = appHost;
  68. _appPaths = appPaths;
  69. _fileSystem = fileSystem;
  70. }
  71. public object Get(GetServerLogs request)
  72. {
  73. List<FileInfo> files;
  74. try
  75. {
  76. files = new DirectoryInfo(_appPaths.LogDirectoryPath)
  77. .EnumerateFiles("*", SearchOption.AllDirectories)
  78. .Where(i => string.Equals(i.Extension, ".txt", System.StringComparison.OrdinalIgnoreCase))
  79. .ToList();
  80. }
  81. catch (DirectoryNotFoundException)
  82. {
  83. files = new List<FileInfo>();
  84. }
  85. var result = files.Select(i => new LogFile
  86. {
  87. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  88. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  89. Name = i.Name,
  90. Size = i.Length
  91. }).OrderByDescending(i => i.DateModified)
  92. .ThenByDescending(i => i.DateCreated)
  93. .ThenBy(i => i.Name)
  94. .ToList();
  95. return ToOptimizedResult(result);
  96. }
  97. public object Get(GetLogFile request)
  98. {
  99. var file = new DirectoryInfo(_appPaths.LogDirectoryPath)
  100. .EnumerateFiles("*", SearchOption.AllDirectories)
  101. .First(i => string.Equals(i.Name, request.Name, System.StringComparison.OrdinalIgnoreCase));
  102. return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
  103. }
  104. /// <summary>
  105. /// Gets the specified request.
  106. /// </summary>
  107. /// <param name="request">The request.</param>
  108. /// <returns>System.Object.</returns>
  109. public object Get(GetSystemInfo request)
  110. {
  111. var result = _appHost.GetSystemInfo();
  112. return ToOptimizedResult(result);
  113. }
  114. public object Get(GetPublicSystemInfo request)
  115. {
  116. var result = _appHost.GetSystemInfo();
  117. var publicInfo = new PublicSystemInfo
  118. {
  119. Id = result.Id,
  120. ServerName = result.ServerName,
  121. Version = result.Version
  122. };
  123. return ToOptimizedResult(publicInfo);
  124. }
  125. /// <summary>
  126. /// Posts the specified request.
  127. /// </summary>
  128. /// <param name="request">The request.</param>
  129. public void Post(RestartApplication request)
  130. {
  131. Task.Run(async () =>
  132. {
  133. await Task.Delay(100).ConfigureAwait(false);
  134. await _appHost.Restart().ConfigureAwait(false);
  135. });
  136. }
  137. /// <summary>
  138. /// Posts the specified request.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. public void Post(ShutdownApplication request)
  142. {
  143. Task.Run(async () =>
  144. {
  145. await Task.Delay(100).ConfigureAwait(false);
  146. await _appHost.Shutdown().ConfigureAwait(false);
  147. });
  148. }
  149. }
  150. }