using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.System;
using ServiceStack;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
///
/// Class GetSystemInfo
///
[Route("/System/Info", "GET", Summary = "Gets information about the server")]
public class GetSystemInfo : IReturn
{
}
///
/// Class RestartApplication
///
[Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
[Authenticated]
public class RestartApplication
{
}
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
[Authenticated]
public class ShutdownApplication
{
}
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
[Authenticated]
public class GetServerLogs : IReturn>
{
}
[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; }
}
///
/// Class SystemInfoService
///
public class SystemService : BaseApiService
{
///
/// The _app host
///
private readonly IServerApplicationHost _appHost;
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
///
/// Initializes a new instance of the class.
///
/// The app host.
/// jsonSerializer
public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem)
{
_appHost = appHost;
_appPaths = appPaths;
_fileSystem = fileSystem;
}
public object Get(GetServerLogs request)
{
List 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();
}
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);
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetSystemInfo request)
{
var result = _appHost.GetSystemInfo();
return ToOptimizedResult(result);
}
///
/// Posts the specified request.
///
/// The request.
public void Post(RestartApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Restart().ConfigureAwait(false);
});
}
///
/// Posts the specified request.
///
/// The request.
public void Post(ShutdownApplication request)
{
Task.Run(async () =>
{
await Task.Delay(100).ConfigureAwait(false);
await _appHost.Shutdown().ConfigureAwait(false);
});
}
}
}