using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace RobotNet10.RobotApp.Controllers; [Route("api/[controller]")] [ApiController] [Authorize] public class LogsManagerController(Services.Logger Logger) : ControllerBase { private readonly string LoggerDirectory = Path.Combine(AppContext.BaseDirectory, "logs"); [HttpGet] public async Task> GetLogs([FromQuery(Name = "date")] DateTime date) { string temp = ""; try { string fileName = $"{date:yyyy-MM-dd}.log"; string path = Path.Combine(LoggerDirectory, fileName); if (!Path.GetFullPath(path).StartsWith(Path.GetFullPath(LoggerDirectory))) { Logger.Warning($"GetLogs: Invalid path detected."); return []; } if (!System.IO.File.Exists(path)) { Logger.Warning($"GetLogs: Log file not found for date {date:d} - {path}."); return []; } temp = Path.Combine(LoggerDirectory, $"{Guid.NewGuid()}.log"); System.IO.File.Copy(path, temp); return await System.IO.File.ReadAllLinesAsync(temp); } catch (Exception ex) { Logger.Warning($"GetLogs: System error occurred - {ex.Message}"); return []; } finally { if (System.IO.File.Exists(temp)) System.IO.File.Delete(temp); } } }