Bozza modifica img unificata SVG/PNG
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using EgwCoreLib.Lux.Data.Services;
|
||||
using EgwMultiEngineManager.Data;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
@@ -11,11 +13,45 @@ namespace Lux.API.Controllers
|
||||
[ApiController]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
public ImageController(ImageCacheService imgServ, ILogger<ImageController> logger)
|
||||
{
|
||||
_imgService = imgServ;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <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");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chiamata GET: riceve Json in formato JwdDto, restituisce svg file
|
||||
/// GET: api/Jwd/svg/00000000-0000-0000-0000-000000000000
|
||||
@@ -35,37 +71,98 @@ namespace Lux.API.Controllers
|
||||
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
|
||||
/// Chiamata GET: restituisce file SVG/PNG (da file o cache REDIS), eliminando nome rand (x force refresh)
|
||||
/// GET: api/image/file/OFF0000001.001.svg?env=WINDOW
|
||||
/// GET: api/image/cache/OFF0000001.001.svg?env=WINDOW
|
||||
/// GET: api/image/file/OFF0000002.001.png?env=WINDOW
|
||||
/// GET: api/image/cache/OFF0000002.001.png?env=WINDOW
|
||||
/// GET: api/image/file/OFF0000002.002-123456.png?env=WINDOW
|
||||
/// GET: api/image/cache/OFF0000002.002-123456.png?env=WINDOW
|
||||
/// </summary>
|
||||
/// <param name="id">id oggetto</param>
|
||||
/// <param name="id">uid oggetto</param>
|
||||
/// <param name="env">environment oggetto</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("png/{id}")]
|
||||
public async Task<IActionResult> png(string id)
|
||||
[HttpGet("{id}")]
|
||||
//[HttpGet("file/{id}")]
|
||||
//[HttpGet("cache/{id}")]
|
||||
public async Task<IActionResult> cacheFile(string id, EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS env = EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.WINDOW)
|
||||
{
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
string base64Encoded = "";
|
||||
byte[] decodedBytes = new byte[0];
|
||||
|
||||
if (string.IsNullOrEmpty(id))
|
||||
return NotFound();
|
||||
|
||||
string mimeType = "txt";
|
||||
byte[] bytes = 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);
|
||||
// se contiene ".svg" lo levo...
|
||||
if (id.EndsWith(".svg"))
|
||||
{
|
||||
mimeType = "image/svg+xml";
|
||||
//id = id.Replace(".svg", "");
|
||||
}
|
||||
else if (id.EndsWith(".png"))
|
||||
{
|
||||
mimeType = "image/png";
|
||||
//id.Replace(".png", "");
|
||||
}
|
||||
|
||||
// se contiene i caratteri casuali x forzare reload --> li levo
|
||||
if (id.Contains("-"))
|
||||
{
|
||||
id = id.Substring(0, id.IndexOf("-"));
|
||||
}
|
||||
|
||||
// secondo del tipo + envr decodifico valore corretto
|
||||
switch (env)
|
||||
{
|
||||
case Constants.EXECENVIRONMENTS.NULL:
|
||||
break;
|
||||
case Constants.EXECENVIRONMENTS.WINDOW:
|
||||
mimeType = "image/svg+xml";
|
||||
string svgContent = await _imgService.LoadSvgAsync(id, env);
|
||||
// se vuoto --> leggo img logo...
|
||||
if (string.IsNullOrEmpty(svgContent))
|
||||
{
|
||||
string filePath = Path.Combine("DemoImg", "LogoEgalware.svg");
|
||||
svgContent = await System.IO.File.ReadAllTextAsync(filePath);
|
||||
}
|
||||
bytes = Encoding.UTF8.GetBytes(svgContent);
|
||||
break;
|
||||
case Constants.EXECENVIRONMENTS.BEAM:
|
||||
case Constants.EXECENVIRONMENTS.WALL:
|
||||
case Constants.EXECENVIRONMENTS.CABINET:
|
||||
default:
|
||||
mimeType = "image/png";
|
||||
string base64Encoded = _imgService.LoadPng(id, EgwMultiEngineManager.Data.Constants.EXECENVIRONMENTS.BEAM);
|
||||
// converto base64
|
||||
bytes = Convert.FromBase64String(base64Encoded);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sw.Stop();
|
||||
Log.Info($"pngString | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
//return Ok(decodedString);
|
||||
return File(decodedBytes, "image/png");
|
||||
Log.Info($"{mimeType} | {sw.Elapsed.TotalMilliseconds:N3} ms");
|
||||
return File(bytes, mimeType);
|
||||
}
|
||||
|
||||
private ImageCacheService _imgService { get; set; }
|
||||
private readonly ILogger<ImageController> _logger;
|
||||
|
||||
#endregion Public Methods
|
||||
|
||||
#region Private Fields
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private readonly ILogger<ImageController> _logger;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private ImageCacheService _imgService { get; set; }
|
||||
|
||||
#endregion Private Properties
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user