< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.SystemController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/SystemController.cs
Line coverage
36%
Covered lines: 19
Uncovered lines: 33
Coverable lines: 52
Total lines: 231
Line coverage: 36.5%
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

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetSystemInfo()100%210%
GetPublicSystemInfo()100%210%
PingSystem()100%210%
RestartApplication()100%210%
ShutdownApplication()100%210%
GetServerLogs()100%210%
GetEndpointInfo()100%210%
GetLogFile(...)25%4.84462.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.Constants;
 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 public information about the server.
 76    /// </summary>
 77    /// <response code="200">Information retrieved.</response>
 78    /// <returns>A <see cref="PublicSystemInfo"/> with public info about the system.</returns>
 79    [HttpGet("Info/Public")]
 80    [ProducesResponseType(StatusCodes.Status200OK)]
 81    public ActionResult<PublicSystemInfo> GetPublicSystemInfo()
 082        => _systemManager.GetPublicSystemInfo(Request);
 83
 84    /// <summary>
 85    /// Pings the system.
 86    /// </summary>
 87    /// <response code="200">Information retrieved.</response>
 88    /// <returns>The server name.</returns>
 89    [HttpGet("Ping", Name = "GetPingSystem")]
 90    [HttpPost("Ping", Name = "PostPingSystem")]
 91    [ProducesResponseType(StatusCodes.Status200OK)]
 92    public ActionResult<string> PingSystem()
 093        => _appHost.Name;
 94
 95    /// <summary>
 96    /// Restarts the application.
 97    /// </summary>
 98    /// <response code="204">Server restarted.</response>
 99    /// <response code="403">User does not have permission to restart server.</response>
 100    /// <returns>No content. Server restarted.</returns>
 101    [HttpPost("Restart")]
 102    [Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
 103    [ProducesResponseType(StatusCodes.Status204NoContent)]
 104    [ProducesResponseType(StatusCodes.Status403Forbidden)]
 105    public ActionResult RestartApplication()
 106    {
 0107        _systemManager.Restart();
 0108        return NoContent();
 109    }
 110
 111    /// <summary>
 112    /// Shuts down the application.
 113    /// </summary>
 114    /// <response code="204">Server shut down.</response>
 115    /// <response code="403">User does not have permission to shutdown server.</response>
 116    /// <returns>No content. Server shut down.</returns>
 117    [HttpPost("Shutdown")]
 118    [Authorize(Policy = Policies.RequiresElevation)]
 119    [ProducesResponseType(StatusCodes.Status204NoContent)]
 120    [ProducesResponseType(StatusCodes.Status403Forbidden)]
 121    public ActionResult ShutdownApplication()
 122    {
 0123        _systemManager.Shutdown();
 0124        return NoContent();
 125    }
 126
 127    /// <summary>
 128    /// Gets a list of available server log files.
 129    /// </summary>
 130    /// <response code="200">Information retrieved.</response>
 131    /// <response code="403">User does not have permission to get server logs.</response>
 132    /// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
 133    [HttpGet("Logs")]
 134    [Authorize(Policy = Policies.RequiresElevation)]
 135    [ProducesResponseType(StatusCodes.Status200OK)]
 136    [ProducesResponseType(StatusCodes.Status403Forbidden)]
 137    public ActionResult<LogFile[]> GetServerLogs()
 138    {
 139        IEnumerable<FileSystemMetadata> files;
 140
 141        try
 142        {
 0143            files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
 0144        }
 0145        catch (IOException ex)
 146        {
 0147            _logger.LogError(ex, "Error getting logs");
 0148            files = Enumerable.Empty<FileSystemMetadata>();
 0149        }
 150
 0151        var result = files.Select(i => new LogFile
 0152        {
 0153            DateCreated = _fileSystem.GetCreationTimeUtc(i),
 0154            DateModified = _fileSystem.GetLastWriteTimeUtc(i),
 0155            Name = i.Name,
 0156            Size = i.Length
 0157        })
 0158            .OrderByDescending(i => i.DateModified)
 0159            .ThenByDescending(i => i.DateCreated)
 0160            .ThenBy(i => i.Name)
 0161            .ToArray();
 162
 0163        return result;
 164    }
 165
 166    /// <summary>
 167    /// Gets information about the request endpoint.
 168    /// </summary>
 169    /// <response code="200">Information retrieved.</response>
 170    /// <response code="403">User does not have permission to get endpoint information.</response>
 171    /// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
 172    [HttpGet("Endpoint")]
 173    [Authorize]
 174    [ProducesResponseType(StatusCodes.Status200OK)]
 175    [ProducesResponseType(StatusCodes.Status403Forbidden)]
 176    public ActionResult<EndPointInfo> GetEndpointInfo()
 177    {
 0178        return new EndPointInfo
 0179        {
 0180            IsLocal = HttpContext.IsLocal(),
 0181            IsInNetwork = _networkManager.IsInLocalNetwork(HttpContext.GetNormalizedRemoteIP())
 0182        };
 183    }
 184
 185    /// <summary>
 186    /// Gets a log file.
 187    /// </summary>
 188    /// <param name="name">The name of the log file to get.</param>
 189    /// <response code="200">Log file retrieved.</response>
 190    /// <response code="403">User does not have permission to get log files.</response>
 191    /// <response code="404">Could not find a log file with the name.</response>
 192    /// <returns>The log file.</returns>
 193    [HttpGet("Logs/Log")]
 194    [Authorize(Policy = Policies.RequiresElevation)]
 195    [ProducesResponseType(StatusCodes.Status200OK)]
 196    [ProducesResponseType(StatusCodes.Status403Forbidden)]
 197    [ProducesResponseType(StatusCodes.Status404NotFound)]
 198    [ProducesFile(MediaTypeNames.Text.Plain)]
 199    public ActionResult GetLogFile([FromQuery, Required] string name)
 200    {
 1201        var file = _fileSystem
 1202            .GetFiles(_appPaths.LogDirectoryPath)
 1203            .FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
 204
 1205        if (file is null)
 206        {
 1207            return NotFound("Log file not found.");
 208        }
 209
 210        // For older files, assume fully static
 0211        var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
 0212        FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, fileShare, IODefaults.FileStre
 0213        return File(stream, "text/plain; charset=utf-8");
 214    }
 215
 216    /// <summary>
 217    /// Gets wake on lan information.
 218    /// </summary>
 219    /// <response code="200">Information retrieved.</response>
 220    /// <returns>An <see cref="IEnumerable{WakeOnLanInfo}"/> with the WakeOnLan infos.</returns>
 221    [HttpGet("WakeOnLanInfo")]
 222    [Authorize]
 223    [Obsolete("This endpoint is obsolete.")]
 224    [ProducesResponseType(StatusCodes.Status200OK)]
 225    public ActionResult<IEnumerable<WakeOnLanInfo>> GetWakeOnLanInfo()
 226    {
 227        var result = _networkManager.GetMacAddresses()
 228            .Select(i => new WakeOnLanInfo(i));
 229        return Ok(result);
 230    }
 231}