SystemController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Mime;
  7. using Jellyfin.Api.Attributes;
  8. using Jellyfin.Api.Models.SystemInfoDtos;
  9. using MediaBrowser.Common.Api;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Common.Extensions;
  12. using MediaBrowser.Common.Net;
  13. using MediaBrowser.Controller;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.Net;
  16. using MediaBrowser.Model.System;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Http;
  19. using Microsoft.AspNetCore.Mvc;
  20. using Microsoft.Extensions.Logging;
  21. namespace Jellyfin.Api.Controllers;
  22. /// <summary>
  23. /// The system controller.
  24. /// </summary>
  25. public class SystemController : BaseJellyfinApiController
  26. {
  27. private readonly ILogger<SystemController> _logger;
  28. private readonly IServerApplicationHost _appHost;
  29. private readonly IApplicationPaths _appPaths;
  30. private readonly IFileSystem _fileSystem;
  31. private readonly INetworkManager _networkManager;
  32. private readonly ISystemManager _systemManager;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="SystemController"/> class.
  35. /// </summary>
  36. /// <param name="logger">Instance of <see cref="ILogger{SystemController}"/> interface.</param>
  37. /// <param name="appPaths">Instance of <see cref="IServerApplicationPaths"/> interface.</param>
  38. /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
  39. /// <param name="fileSystem">Instance of <see cref="IFileSystem"/> interface.</param>
  40. /// <param name="networkManager">Instance of <see cref="INetworkManager"/> interface.</param>
  41. /// <param name="systemManager">Instance of <see cref="ISystemManager"/> interface.</param>
  42. public SystemController(
  43. ILogger<SystemController> logger,
  44. IServerApplicationHost appHost,
  45. IServerApplicationPaths appPaths,
  46. IFileSystem fileSystem,
  47. INetworkManager networkManager,
  48. ISystemManager systemManager)
  49. {
  50. _logger = logger;
  51. _appHost = appHost;
  52. _appPaths = appPaths;
  53. _fileSystem = fileSystem;
  54. _networkManager = networkManager;
  55. _systemManager = systemManager;
  56. }
  57. /// <summary>
  58. /// Gets information about the server.
  59. /// </summary>
  60. /// <response code="200">Information retrieved.</response>
  61. /// <response code="403">User does not have permission to retrieve information.</response>
  62. /// <returns>A <see cref="SystemInfo"/> with info about the system.</returns>
  63. [HttpGet("Info")]
  64. [Authorize(Policy = Policies.FirstTimeSetupOrIgnoreParentalControl)]
  65. [ProducesResponseType(StatusCodes.Status200OK)]
  66. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  67. public ActionResult<SystemInfo> GetSystemInfo()
  68. => _systemManager.GetSystemInfo(Request);
  69. /// <summary>
  70. /// Gets information about the server.
  71. /// </summary>
  72. /// <response code="200">Information retrieved.</response>
  73. /// <response code="403">User does not have permission to retrieve information.</response>
  74. /// <returns>A <see cref="SystemInfo"/> with info about the system.</returns>
  75. [HttpGet("Info/Storage")]
  76. [Authorize(Policy = Policies.RequiresElevation)]
  77. [ProducesResponseType(StatusCodes.Status200OK)]
  78. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  79. public ActionResult<SystemStorageDto> GetSystemStorage()
  80. => Ok(SystemStorageDto.FromSystemStorageInfo(_systemManager.GetSystemStorageInfo()));
  81. /// <summary>
  82. /// Gets public information about the server.
  83. /// </summary>
  84. /// <response code="200">Information retrieved.</response>
  85. /// <returns>A <see cref="PublicSystemInfo"/> with public info about the system.</returns>
  86. [HttpGet("Info/Public")]
  87. [ProducesResponseType(StatusCodes.Status200OK)]
  88. public ActionResult<PublicSystemInfo> GetPublicSystemInfo()
  89. => _systemManager.GetPublicSystemInfo(Request);
  90. /// <summary>
  91. /// Pings the system.
  92. /// </summary>
  93. /// <response code="200">Information retrieved.</response>
  94. /// <returns>The server name.</returns>
  95. [HttpGet("Ping", Name = "GetPingSystem")]
  96. [HttpPost("Ping", Name = "PostPingSystem")]
  97. [ProducesResponseType(StatusCodes.Status200OK)]
  98. public ActionResult<string> PingSystem()
  99. => _appHost.Name;
  100. /// <summary>
  101. /// Restarts the application.
  102. /// </summary>
  103. /// <response code="204">Server restarted.</response>
  104. /// <response code="403">User does not have permission to restart server.</response>
  105. /// <returns>No content. Server restarted.</returns>
  106. [HttpPost("Restart")]
  107. [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
  108. [ProducesResponseType(StatusCodes.Status204NoContent)]
  109. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  110. public ActionResult RestartApplication()
  111. {
  112. _systemManager.Restart();
  113. return NoContent();
  114. }
  115. /// <summary>
  116. /// Shuts down the application.
  117. /// </summary>
  118. /// <response code="204">Server shut down.</response>
  119. /// <response code="403">User does not have permission to shutdown server.</response>
  120. /// <returns>No content. Server shut down.</returns>
  121. [HttpPost("Shutdown")]
  122. [Authorize(Policy = Policies.RequiresElevation)]
  123. [ProducesResponseType(StatusCodes.Status204NoContent)]
  124. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  125. public ActionResult ShutdownApplication()
  126. {
  127. _systemManager.Shutdown();
  128. return NoContent();
  129. }
  130. /// <summary>
  131. /// Gets a list of available server log files.
  132. /// </summary>
  133. /// <response code="200">Information retrieved.</response>
  134. /// <response code="403">User does not have permission to get server logs.</response>
  135. /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
  136. [HttpGet("Logs")]
  137. [Authorize(Policy = Policies.RequiresElevation)]
  138. [ProducesResponseType(StatusCodes.Status200OK)]
  139. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  140. public ActionResult<LogFile[]> GetServerLogs()
  141. {
  142. IEnumerable<FileSystemMetadata> files;
  143. try
  144. {
  145. files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
  146. }
  147. catch (IOException ex)
  148. {
  149. _logger.LogError(ex, "Error getting logs");
  150. files = Enumerable.Empty<FileSystemMetadata>();
  151. }
  152. var result = files.Select(i => new LogFile
  153. {
  154. DateCreated = _fileSystem.GetCreationTimeUtc(i),
  155. DateModified = _fileSystem.GetLastWriteTimeUtc(i),
  156. Name = i.Name,
  157. Size = i.Length
  158. })
  159. .OrderByDescending(i => i.DateModified)
  160. .ThenByDescending(i => i.DateCreated)
  161. .ThenBy(i => i.Name)
  162. .ToArray();
  163. return result;
  164. }
  165. /// <summary>
  166. /// Gets information about the request endpoint.
  167. /// </summary>
  168. /// <response code="200">Information retrieved.</response>
  169. /// <response code="403">User does not have permission to get endpoint information.</response>
  170. /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
  171. [HttpGet("Endpoint")]
  172. [Authorize]
  173. [ProducesResponseType(StatusCodes.Status200OK)]
  174. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  175. public ActionResult<EndPointInfo> GetEndpointInfo()
  176. {
  177. return new EndPointInfo
  178. {
  179. IsLocal = HttpContext.IsLocal(),
  180. IsInNetwork = _networkManager.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIP())
  181. };
  182. }
  183. /// <summary>
  184. /// Gets a log file.
  185. /// </summary>
  186. /// <param name="name">The name of the log file to get.</param>
  187. /// <response code="200">Log file retrieved.</response>
  188. /// <response code="403">User does not have permission to get log files.</response>
  189. /// <response code="404">Could not find a log file with the name.</response>
  190. /// <returns>The log file.</returns>
  191. [HttpGet("Logs/Log")]
  192. [Authorize(Policy = Policies.RequiresElevation)]
  193. [ProducesResponseType(StatusCodes.Status200OK)]
  194. [ProducesResponseType(StatusCodes.Status403Forbidden)]
  195. [ProducesResponseType(StatusCodes.Status404NotFound)]
  196. [ProducesFile(MediaTypeNames.Text.Plain)]
  197. public ActionResult GetLogFile([FromQuery, Required] string name)
  198. {
  199. var file = _fileSystem
  200. .GetFiles(_appPaths.LogDirectoryPath)
  201. .FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
  202. if (file is null)
  203. {
  204. return NotFound("Log file not found.");
  205. }
  206. // For older files, assume fully static
  207. var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
  208. FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, fileShare, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
  209. return File(stream, "text/plain; charset=utf-8");
  210. }
  211. }