SystemService.cs 4.8 KB

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