jellyfin/MediaBrowser.Api/SystemService.cs

178 lines
5.5 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Net;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Model.System;
2013-12-07 16:52:38 +01:00
using ServiceStack;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2013-02-21 02:33:05 +01:00
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
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")]
2014-07-08 03:41:03 +02:00
[Authenticated]
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>
{
}
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")]
[Authenticated]
2013-02-21 02:33:05 +01:00
public class RestartApplication
{
}
2014-03-23 20:36:25 +01:00
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
[Authenticated]
public class ShutdownApplication
{
}
2014-03-23 20:36:25 +01:00
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
[Authenticated]
public class GetServerLogs : IReturn<List<LogFile>>
{
}
[Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
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;
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>
2013-02-24 22:53:54 +01:00
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem)
2013-02-24 22:53:54 +01:00
{
_appHost = appHost;
_appPaths = appPaths;
_fileSystem = fileSystem;
}
public object Get(GetServerLogs request)
{
List<FileInfo> files;
try
{
files = new DirectoryInfo(_appPaths.LogDirectoryPath)
.EnumerateFiles("*", SearchOption.AllDirectories)
.Where(i => string.Equals(i.Extension, ".txt", System.StringComparison.OrdinalIgnoreCase))
.ToList();
}
catch (DirectoryNotFoundException)
{
files = new List<FileInfo>();
}
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);
}
public object Get(GetLogFile request)
{
var file = new DirectoryInfo(_appPaths.LogDirectoryPath)
.EnumerateFiles("*", SearchOption.AllDirectories)
.First(i => string.Equals(i.Name, request.Name, System.StringComparison.OrdinalIgnoreCase));
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.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>
public object Get(GetSystemInfo request)
{
2013-03-07 06:34:00 +01:00
var result = _appHost.GetSystemInfo();
2013-02-21 02:33:05 +01:00
return ToOptimizedResult(result);
}
2014-07-28 00:01:29 +02:00
public object Get(GetPublicSystemInfo request)
{
var result = _appHost.GetSystemInfo();
var publicInfo = new PublicSystemInfo
{
Id = result.Id,
ServerName = result.ServerName,
Version = result.Version
};
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);
});
}
2013-02-21 02:33:05 +01:00
}
}