From f32f73b402e9bfd5aca47cb94a245fccbd21482f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 2 Oct 2026 18:40:32 -0500 Subject: tweak controller --- src/slskd/Core/API/Controllers/LogsController.cs | 33 ++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index 6ef4a7b6..a83e608c 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -38,6 +38,7 @@ namespace slskd.Core.API using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Serilog; using slskd.Files; @@ -58,7 +59,7 @@ namespace slskd.Core.API } private FileService Files { get; } - private ILogger Log { get; } = Serilog.Log.ForContext(); + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// Gets the last few application logs. @@ -79,6 +80,11 @@ namespace slskd.Core.API [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] public async Task List() { + if (!Directory.Exists(Path.GetFullPath(Program.LogDirectory))) + { + return NotFound(); + } + var directory = await Files.ListDirectoryContentsAsync(Path.GetFullPath(Program.LogDirectory), enumerationOptions: new EnumerationOptions { IgnoreInaccessible = true, @@ -95,11 +101,12 @@ namespace slskd.Core.API /// Retrieves the requested log file from disk as plain text. /// /// The name of the log file. - /// A value indicating whether the file should be sent as an attachment. /// [HttpGet("files/{filename}")] + [Produces("text/plain")] + [ProducesResponseType(typeof(string), StatusCodes.Status200OK, contentType: "text/plain")] [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.AdministratorOnly)] - public IActionResult Get(string filename, [FromQuery] bool download = false) + public IActionResult Get([FromRoute] string filename) { if (string.IsNullOrWhiteSpace(filename)) { @@ -121,29 +128,23 @@ namespace slskd.Core.API if (!sanitizedFilename.EndsWith(".log", StringComparison.OrdinalIgnoreCase)) { - return BadRequest("Only .log files may be retrieved"); + return StatusCode(StatusCodes.Status403Forbidden, "Only .log files may be retrieved"); } try { var stream = Files.GetFileContents(FileSafety.CombineSafely(Program.LogDirectory, sanitizedFilename)); - const string contentType = "text/plain; charset=utf-8"; - - if (download) - { - return File(stream, contentType, filename); - } - - return File(stream, contentType); - } - catch (UnauthorizedException) - { - return Unauthorized(); + return File(stream, "text/plain; charset=utf-8", enableRangeProcessing: true); } catch (NotFoundException) { return NotFound(); } + catch (Exception ex) + { + Log.Error(ex, "Failed to retrieve log file {Filename}: {Message}", filename, ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); + } } } } -- cgit v1.2.3