jellyfin/MediaBrowser.Api/System/SystemService.cs

227 lines
7.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
2013-02-20 20:33:05 -05:00
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
2016-10-25 15:02:04 -04:00
using MediaBrowser.Model.IO;
2016-01-09 16:27:30 -05:00
using MediaBrowser.Model.Net;
2016-10-25 15:02:04 -04:00
using MediaBrowser.Model.Services;
using MediaBrowser.Model.System;
2019-01-01 15:34:12 -05:00
using Microsoft.Extensions.Logging;
2013-02-20 20:33:05 -05:00
2014-08-10 18:13:17 -04:00
namespace MediaBrowser.Api.System
2013-02-20 20:33:05 -05:00
{
2013-02-24 16:53:54 -05:00
/// <summary>
/// Class GetSystemInfo
/// </summary>
2014-03-23 15:36:25 -04:00
[Route("/System/Info", "GET", Summary = "Gets information about the server")]
2016-06-23 13:04:18 -04:00
[Authenticated(EscapeParentalControl = true, AllowBeforeStartupWizard = true)]
2013-02-20 20:33:05 -05:00
public class GetSystemInfo : IReturn<SystemInfo>
{
}
2014-07-27 18:01:29 -04:00
[Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
{
}
2016-01-02 18:45:21 -05:00
[Route("/System/Ping", "POST")]
2018-09-12 13:26:21 -04:00
[Route("/System/Ping", "GET")]
2016-01-02 18:45:21 -05:00
public class PingSystem : IReturnVoid
{
}
2013-02-20 20:33:05 -05:00
/// <summary>
/// Class RestartApplication
/// </summary>
2014-03-23 15:36:25 -04:00
[Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
2017-09-03 14:38:26 -04:00
[Authenticated(Roles = "Admin", AllowLocal = true)]
2013-02-20 20:33:05 -05:00
public class RestartApplication
{
}
2014-08-24 23:54:45 -04:00
/// <summary>
/// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
/// </summary>
2014-03-23 15:36:25 -04:00
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
2017-09-03 14:38:26 -04:00
[Authenticated(Roles = "Admin", AllowLocal = true)]
public class ShutdownApplication
{
}
2014-03-23 15:36:25 -04:00
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
2014-11-14 21:31:03 -05:00
[Authenticated(Roles = "Admin")]
2017-08-19 15:43:35 -04:00
public class GetServerLogs : IReturn<LogFile[]>
{
}
2014-08-19 18:28:35 -04:00
[Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
[Authenticated]
2016-01-09 16:27:30 -05:00
public class GetEndpointInfo : IReturn<EndPointInfo>
2014-08-19 18:28:35 -04:00
{
public string Endpoint { get; set; }
}
[Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
2014-11-14 21:31:03 -05:00
[Authenticated(Roles = "Admin")]
public class GetLogFile
{
[ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string Name { get; set; }
}
2018-09-12 13:26:21 -04:00
[Route("/System/WakeOnLanInfo", "GET", Summary = "Gets wake on lan information")]
[Authenticated]
public class GetWakeOnLanInfo : IReturn<WakeOnLanInfo[]>
{
}
2013-02-20 20:33:05 -05:00
/// <summary>
/// Class SystemInfoService
/// </summary>
2013-03-16 01:52:33 -04:00
public class SystemService : BaseApiService
2013-02-20 20:33:05 -05:00
{
/// <summary>
/// The _app host
/// </summary>
2013-03-07 00:34:00 -05:00
private readonly IServerApplicationHost _appHost;
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
2014-08-19 18:28:35 -04:00
private readonly INetworkManager _network;
2013-02-24 16:53:54 -05:00
/// <summary>
/// Initializes a new instance of the <see cref="SystemService" /> class.
/// </summary>
/// <param name="appHost">The app host.</param>
/// <param name="fileSystem">The file system.</param>
2014-08-10 18:13:17 -04:00
/// <exception cref="ArgumentNullException">jsonSerializer</exception>
public SystemService(
ILogger<SystemService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
IServerApplicationHost appHost,
IFileSystem fileSystem,
INetworkManager network)
: base(logger, serverConfigurationManager, httpResultFactory)
2013-02-24 16:53:54 -05:00
{
_appPaths = serverConfigurationManager.ApplicationPaths;
_appHost = appHost;
_fileSystem = fileSystem;
2014-08-19 18:28:35 -04:00
_network = network;
2014-08-31 15:15:33 -04:00
}
2016-01-02 18:45:21 -05:00
public object Post(PingSystem request)
{
return _appHost.Name;
}
2018-09-12 13:26:21 -04:00
public object Get(GetWakeOnLanInfo request)
{
var result = _appHost.GetWakeOnLanInfo();
return ToOptimizedResult(result);
}
public object Get(GetServerLogs request)
{
IEnumerable<FileSystemMetadata> files;
try
{
2019-01-01 15:34:12 -05:00
files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt", ".log" }, true, false);
}
2019-01-01 15:34:12 -05:00
catch (IOException ex)
{
2019-01-01 15:34:12 -05:00
Logger.LogError(ex, "Error getting logs");
files = Enumerable.Empty<FileSystemMetadata>();
}
var result = files.Select(i => new LogFile
{
DateCreated = _fileSystem.GetCreationTimeUtc(i),
DateModified = _fileSystem.GetLastWriteTimeUtc(i),
Name = i.Name,
Size = i.Length
}).OrderByDescending(i => i.DateModified)
.ThenByDescending(i => i.DateCreated)
.ThenBy(i => i.Name)
2017-08-19 15:43:35 -04:00
.ToArray();
return ToOptimizedResult(result);
}
2016-06-19 02:18:29 -04:00
public Task<object> Get(GetLogFile request)
{
2016-06-23 13:04:18 -04:00
var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
2014-11-05 14:28:41 -05:00
.First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
2017-09-03 21:24:20 -04:00
// For older files, assume fully static
2020-04-05 23:12:25 -04:00
var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite;
return ResultFactory.GetStaticFileResult(Request, file.FullName, fileShare);
2013-02-24 16:53:54 -05:00
}
2013-02-20 20:33:05 -05:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-06-19 12:53:43 -04:00
public async Task<object> Get(GetSystemInfo request)
2013-02-20 20:33:05 -05:00
{
2017-11-23 10:46:16 -05:00
var result = await _appHost.GetSystemInfo(CancellationToken.None).ConfigureAwait(false);
2013-02-20 20:33:05 -05:00
return ToOptimizedResult(result);
}
2016-06-19 12:53:43 -04:00
public async Task<object> Get(GetPublicSystemInfo request)
2014-07-27 18:01:29 -04:00
{
var result = await _appHost.GetPublicSystemInfo(CancellationToken.None).ConfigureAwait(false);
2014-07-27 18:01:29 -04:00
return ToOptimizedResult(result);
2014-07-27 18:01:29 -04:00
}
2013-02-20 20:33:05 -05:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(RestartApplication request)
{
2017-09-09 14:51:24 -04:00
_appHost.Restart();
2013-02-20 20:33:05 -05:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(ShutdownApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Shutdown().ConfigureAwait(false);
});
}
2014-08-19 18:28:35 -04:00
public object Get(GetEndpointInfo request)
{
2016-01-09 16:27:30 -05:00
return ToOptimizedResult(new EndPointInfo
2014-08-19 18:28:35 -04:00
{
IsLocal = Request.IsLocal,
IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
});
}
}
2013-02-20 20:33:05 -05:00
}