< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.SystemController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/SystemController.cs
Line coverage
35%
Covered lines: 19
Uncovered lines: 34
Coverable lines: 53
Total lines: 228
Line coverage: 35.8%
Branch coverage
25%
Covered branches: 1
Total branches: 4
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

0255075100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetSystemInfo()100%210%
GetSystemStorage()100%210%
GetPublicSystemInfo()100%210%
PingSystem()100%210%
RestartApplication()100%210%
ShutdownApplication()100%210%
GetServerLogs()100%210%
GetEndpointInfo()100%210%
GetLogFile(...)25%5462.5%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/SystemController.cs

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