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 logger) { _imgService = imgServ; _logger = logger; } /// /// Chiamata GET: riceve Json in formato JwdDto, restituisce svg file /// GET: api/Jwd/svg/00000000-0000-0000-0000-000000000000 /// /// id univoco img /// [HttpGet("svg/{id}")] public async Task 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"); } /// /// Chiamata GET: restituisce file PNG (da file o da cache) /// PUT: api/image/png/00000000-0000-0000-0000-000000000000 /// /// id oggetto /// [HttpGet("png/{id}")] public async Task 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 _logger; private static Logger Log = LogManager.GetCurrentClassLogger(); } }