diff --git a/Jellyfin.Api/Controllers/SystemController.cs b/Jellyfin.Api/Controllers/SystemController.cs index 3d4df03869..6c5ce47158 100644 --- a/Jellyfin.Api/Controllers/SystemController.cs +++ b/Jellyfin.Api/Controllers/SystemController.cs @@ -188,16 +188,24 @@ public class SystemController : BaseJellyfinApiController /// The name of the log file to get. /// Log file retrieved. /// User does not have permission to get log files. + /// Could not find a log file with the name. /// The log file. [HttpGet("Logs/Log")] [Authorize(Policy = Policies.RequiresElevation)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesFile(MediaTypeNames.Text.Plain)] public ActionResult GetLogFile([FromQuery, Required] string name) { - var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath) - .First(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase)); + var file = _fileSystem + .GetFiles(_appPaths.LogDirectoryPath) + .FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase)); + + if (file is null) + { + return NotFound("Log file not found."); + } // For older files, assume fully static var fileShare = file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1) ? FileShare.Read : FileShare.ReadWrite; diff --git a/tests/Jellyfin.Api.Tests/Controllers/SystemControllerTests.cs b/tests/Jellyfin.Api.Tests/Controllers/SystemControllerTests.cs new file mode 100644 index 0000000000..dd84c1a186 --- /dev/null +++ b/tests/Jellyfin.Api.Tests/Controllers/SystemControllerTests.cs @@ -0,0 +1,35 @@ +using Jellyfin.Api.Controllers; +using MediaBrowser.Common.Net; +using MediaBrowser.Controller; +using MediaBrowser.Model.IO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using Moq; +using Xunit; + +namespace Jellyfin.Api.Tests.Controllers +{ + public class SystemControllerTests + { + [Fact] + public void GetLogFile_FileDoesNotExist_ReturnsNotFound() + { + var mockFileSystem = new Mock(); + mockFileSystem + .Setup(fs => fs.GetFiles(It.IsAny(), It.IsAny())) + .Returns([new() { Name = "file1.txt" }, new() { Name = "file2.txt" }]); + + var controller = new SystemController( + Mock.Of>(), + Mock.Of(), + Mock.Of(), + mockFileSystem.Object, + Mock.Of(), + Mock.Of()); + + var result = controller.GetLogFile("DOES_NOT_EXIST.txt"); + + Assert.IsType(result); + } + } +}