RobotApp/RobotApp/Controllers/ImagesController.cs
Đăng Nguyễn c5686e4ecf update
2025-10-03 11:31:14 +07:00

62 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace RobotApp.Controllers;
[Route("api/[controller]")]
[ApiController]
[AllowAnonymous]
public class ImagesController(Services.Logger<ImagesController> Logger) : ControllerBase
{
[HttpGet]
[Route("mapping")]
public async Task<IActionResult> GetMapImage()
{
try
{
await Task.Delay(1);
string fileName = "gara20250309.png";
string filePath = Path.Combine("maps", fileName);
if (System.IO.File.Exists(filePath))
{
byte[] imageBytes = await System.IO.File.ReadAllBytesAsync(filePath);
return File(imageBytes, "image/png");
}
else
{
string mapsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "maps");
if (Directory.Exists(mapsDir))
{
var pngFiles = Directory.GetFiles(mapsDir, "*.png");
return NotFound(new
{
error = "Map image not found",
searchPath = filePath,
mapsDirectory = mapsDir,
availableFiles = pngFiles.Select(Path.GetFileName).ToArray(),
baseDirectory = AppDomain.CurrentDomain.BaseDirectory
});
}
else
{
Logger.Warning($"GetMapImage: Maps directory does not exist: {mapsDir}");
return NotFound(new
{
error = "Maps directory not found",
searchPath = mapsDir,
baseDirectory = AppDomain.CurrentDomain.BaseDirectory
});
}
}
}
catch (Exception ex)
{
Logger.Warning($"GetMapImage: Exception occurred - {ex.Message}");
return StatusCode(500, new { error = "Internal server error", message = ex.Message, stackTrace = ex.StackTrace });
}
}
}