SystemService.cs 4.8 KB

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