Add button to download current log
This commit is contained in:
parent
2faa334461
commit
19a63491fc
3 changed files with 67 additions and 0 deletions
43
LANCommander.Server/Endpoints/LogEndpoints.cs
Normal file
43
LANCommander.Server/Endpoints/LogEndpoints.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using LANCommander.Server.Settings.Enums;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net.Mime;
|
||||
|
||||
namespace LANCommander.Server.Endpoints;
|
||||
|
||||
public static class LogEndpoints
|
||||
{
|
||||
public static void MapLogEndpoints(this IEndpointRouteBuilder routes)
|
||||
{
|
||||
var group = routes.MapGroup("/api/Logs")
|
||||
.RequireAuthorization();
|
||||
|
||||
group.MapGet("/Download/{fileName}", DownloadAsync);
|
||||
}
|
||||
|
||||
private static IResult DownloadAsync(
|
||||
string fileName,
|
||||
[FromServices] IOptions<Settings.Settings> settings)
|
||||
{
|
||||
var fileProvider = settings.Value.Server.Logs.Providers
|
||||
.FirstOrDefault(p => p.Type == LoggingProviderType.File && p.Enabled);
|
||||
|
||||
if (fileProvider == null)
|
||||
return TypedResults.NotFound("No file logging provider is configured.");
|
||||
|
||||
// Sanitize the file name to prevent directory traversal
|
||||
fileName = Path.GetFileName(fileName);
|
||||
|
||||
if (!fileName.StartsWith("log-") || !fileName.EndsWith(".txt"))
|
||||
return TypedResults.BadRequest("Invalid log file name.");
|
||||
|
||||
var logFilePath = Path.Combine(fileProvider.ConnectionString, fileName);
|
||||
|
||||
if (!File.Exists(logFilePath))
|
||||
return TypedResults.NotFound("Log file not found.");
|
||||
|
||||
var stream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
|
||||
return TypedResults.File(stream, MediaTypeNames.Text.Plain, fileName);
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ public static class Endpoints
|
|||
endpoints.MapSaveEndpoints();
|
||||
endpoints.MapServerEndpoints();
|
||||
endpoints.MapSettingsEndpoints();
|
||||
endpoints.MapLogEndpoints();
|
||||
endpoints.MapHqEndpoints();
|
||||
endpoints.MapTagEndpoints();
|
||||
endpoints.MapControllers();
|
||||
|
|
|
|||
|
|
@ -7,10 +7,15 @@
|
|||
@inject IOptions<Settings> Settings
|
||||
@inject SettingsProvider<Settings> SettingsProvider
|
||||
@inject IMessageService MessageService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject ILogger<Index> Logger
|
||||
|
||||
<PageHeader Title="Logs">
|
||||
<PageHeaderExtra>
|
||||
@if (FileProvider != null)
|
||||
{
|
||||
<Button OnClick="DownloadLog" Icon="@IconType.Outline.Download">Download Log</Button>
|
||||
}
|
||||
<Button OnClick="Save" Type="@ButtonType.Primary">Save</Button>
|
||||
</PageHeaderExtra>
|
||||
</PageHeader>
|
||||
|
|
@ -52,6 +57,7 @@
|
|||
|
||||
@code {
|
||||
List<LoggingProvider> Providers = new();
|
||||
LoggingProvider? FileProvider => Providers.FirstOrDefault(p => p.Type == LoggingProviderType.File && p.Enabled);
|
||||
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
|
|
@ -92,4 +98,21 @@
|
|||
{
|
||||
Providers.Remove(provider);
|
||||
}
|
||||
|
||||
void DownloadLog()
|
||||
{
|
||||
if (FileProvider == null)
|
||||
return;
|
||||
|
||||
var logFileName = $"log-{DateTime.Now:yyyy-MM-dd}.txt";
|
||||
var logFilePath = Path.Combine(FileProvider.ConnectionString, logFileName);
|
||||
|
||||
if (!File.Exists(logFilePath))
|
||||
{
|
||||
MessageService.Warning("No log file exists for today.");
|
||||
return;
|
||||
}
|
||||
|
||||
NavigationManager.NavigateTo($"/api/Logs/Download/{logFileName}", true);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue