jellyfin/MediaBrowser.Api/System/SystemService.cs

222 lines
7 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
2014-08-20 00:28:35 +02:00
using MediaBrowser.Common.Net;
2014-08-31 21:15:33 +02:00
using MediaBrowser.Common.Security;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Net;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.System;
2014-08-11 00:13:17 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-02-21 02:33:05 +01:00
using System.Threading.Tasks;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2016-01-09 22:27:30 +01:00
using MediaBrowser.Model.Net;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.Services;
2013-02-21 02:33:05 +01:00
2014-08-11 00:13:17 +02:00
namespace MediaBrowser.Api.System
2013-02-21 02:33:05 +01:00
{
2013-02-24 22:53:54 +01:00
/// <summary>
/// Class GetSystemInfo
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/System/Info", "GET", Summary = "Gets information about the server")]
2016-06-23 19:04:18 +02:00
[Authenticated(EscapeParentalControl = true, AllowBeforeStartupWizard = true)]
2013-02-21 02:33:05 +01:00
public class GetSystemInfo : IReturn<SystemInfo>
{
}
2014-07-28 00:01:29 +02:00
[Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
{
}
2016-01-03 00:45:21 +01:00
[Route("/System/Ping", "POST")]
public class PingSystem : IReturnVoid
{
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Class RestartApplication
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
2014-11-15 03:31:03 +01:00
[Authenticated(Roles = "Admin")]
2013-02-21 02:33:05 +01:00
public class RestartApplication
{
}
2014-08-25 05:54:45 +02:00
/// <summary>
/// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
public class ShutdownApplication
{
// TODO: This is not currently authenticated due to uninstaller
// Improve later
}
2014-03-23 20:36:25 +01:00
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
2014-11-15 03:31:03 +01:00
[Authenticated(Roles = "Admin")]
public class GetServerLogs : IReturn<List<LogFile>>
{
}
2014-08-20 00:28:35 +02:00
[Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
[Authenticated]
2016-01-09 22:27:30 +01:00
public class GetEndpointInfo : IReturn<EndPointInfo>
2014-08-20 00:28:35 +02:00
{
public string Endpoint { get; set; }
}
[Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
2014-11-15 03:31:03 +01: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; }
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Class SystemInfoService
/// </summary>
2013-03-16 06:52:33 +01:00
public class SystemService : BaseApiService
2013-02-21 02:33:05 +01:00
{
/// <summary>
/// The _app host
/// </summary>
2013-03-07 06:34:00 +01:00
private readonly IServerApplicationHost _appHost;
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
2014-08-20 00:28:35 +02:00
private readonly INetworkManager _network;
2014-08-31 21:15:33 +02:00
private readonly ISecurityManager _security;
2013-02-24 22:53:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="SystemService" /> class.
/// </summary>
/// <param name="appHost">The app host.</param>
/// <param name="appPaths">The application paths.</param>
/// <param name="fileSystem">The file system.</param>
2014-08-11 00:13:17 +02:00
/// <exception cref="ArgumentNullException">jsonSerializer</exception>
2014-08-31 21:15:33 +02:00
public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
2013-02-24 22:53:54 +01:00
{
_appHost = appHost;
_appPaths = appPaths;
_fileSystem = fileSystem;
2014-08-20 00:28:35 +02:00
_network = network;
2014-08-31 21:15:33 +02:00
_security = security;
}
2016-01-03 00:45:21 +01:00
public object Post(PingSystem request)
{
return _appHost.Name;
}
public object Get(GetServerLogs request)
{
2015-10-04 05:38:46 +02:00
List<FileSystemMetadata> files;
try
{
2016-06-23 19:04:18 +02:00
files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
2014-11-05 20:28:41 +01:00
.Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
.ToList();
}
2016-11-01 04:07:45 +01:00
catch (IOException)
{
2015-10-04 05:38:46 +02:00
files = new List<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)
.ToList();
return ToOptimizedResult(result);
}
2016-06-19 08:18:29 +02:00
public Task<object> Get(GetLogFile request)
{
2016-06-23 19:04:18 +02:00
var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
2014-11-05 20:28:41 +01:00
.First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
2016-10-25 21:02:04 +02:00
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
2013-02-24 22:53:54 +01:00
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-06-19 18:53:43 +02:00
public async Task<object> Get(GetSystemInfo request)
2013-02-21 02:33:05 +01:00
{
2016-06-19 18:53:43 +02:00
var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
2013-02-21 02:33:05 +01:00
return ToOptimizedResult(result);
}
2016-06-19 18:53:43 +02:00
public async Task<object> Get(GetPublicSystemInfo request)
2014-07-28 00:01:29 +02:00
{
2016-06-19 18:53:43 +02:00
var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
2014-07-28 00:01:29 +02:00
var publicInfo = new PublicSystemInfo
{
Id = result.Id,
ServerName = result.ServerName,
2014-09-25 00:47:03 +02:00
Version = result.Version,
LocalAddress = result.LocalAddress,
WanAddress = result.WanAddress,
OperatingSystem = result.OperatingSystem
2014-07-28 00:01:29 +02:00
};
return ToOptimizedResult(publicInfo);
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(RestartApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Restart().ConfigureAwait(false);
2013-02-21 02:33:05 +01: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-20 00:28:35 +02:00
public object Get(GetEndpointInfo request)
{
2016-01-09 22:27:30 +01:00
return ToOptimizedResult(new EndPointInfo
2014-08-20 00:28:35 +02:00
{
IsLocal = Request.IsLocal,
IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
});
}
}
2013-02-21 02:33:05 +01:00
}