Files
lux/Lux.API/Controllers/ImageController.cs
T
2025-11-03 18:42:04 +01:00

72 lines
2.6 KiB
C#

using EgwCoreLib.Lux.Data.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System.Diagnostics;
using System.Text;
namespace Lux.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImageController : ControllerBase
{
public ImageController(ImageCacheService imgServ, ILogger<ImageController> logger)
{
_imgService = imgServ;
_logger = logger;
}
/// <summary>
/// Chiamata GET: riceve Json in formato JwdDto, restituisce svg file
/// GET: api/Jwd/svg/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id univoco img</param>
/// <returns></returns>
[HttpGet("svg/{id}")]
public async Task<IActionResult> svgFileGet(string id)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string filePath = Path.Combine("DemoImg", "AntaDoppia.svg");
var svgContent = await System.IO.File.ReadAllTextAsync(filePath);
var bytes = System.Text.Encoding.UTF8.GetBytes(svgContent);
sw.Stop();
_logger.LogInformation($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return File(bytes, "image/svg+xml");
}
/// <summary>
/// Chiamata GET: restituisce file PNG (da file o da cache)
/// PUT: api/image/png/00000000-0000-0000-0000-000000000000
/// </summary>
/// <param name="id">id oggetto</param>
/// <returns></returns>
[HttpGet("png/{id}")]
public async Task<IActionResult> png(string id)
{
Stopwatch sw = new Stopwatch();
sw.Start();
string base64Encoded = "";
byte[] decodedBytes = new byte[0];
// ...se ricevo percorso --> leggo jwd/svg cablato
if (!string.IsNullOrEmpty(id))
{
// bonifica nome svg da
base64Encoded = _imgService.LoadPng(id, EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.BEAM);
// converto base64
decodedBytes = Convert.FromBase64String(base64Encoded);
}
sw.Stop();
Log.Info($"pngString | {sw.Elapsed.TotalMilliseconds:N3} ms");
//return Ok(decodedString);
return File(decodedBytes, "image/png");
}
private ImageCacheService _imgService { get; set; }
private readonly ILogger<ImageController> _logger;
private static Logger Log = LogManager.GetCurrentClassLogger();
}
}