Files
lux/Lux.API/Controllers/FileController.cs
T
2026-01-13 15:10:47 +01:00

184 lines
7.0 KiB
C#

using EgwCoreLib.Lux.Data.Services;
using EgwMultiEngineManager.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;
using System;
using System.Data;
using System.Diagnostics;
using System.Text;
namespace Lux.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FileController : ControllerBase
{
#region Public Constructors
/// <summary>
/// Controller per gestione file (es download JWD)
/// </summary>
/// <param name="imgServ"></param>
/// <param name="logger"></param>
public FileController(DataLayerServices DLService, ImageCacheService imgServ, ILogger<ImageController> logger)
{
_imgService = imgServ;
_DSService = DLService;
_logger = logger;
}
#endregion Public Constructors
#region Public Methods
#if false
/// <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
/// </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");
}
#endif
/// <summary>
/// Chiamata GET: restituisce file di un determinato objType
/// GET: api/file/POR.25.0000000E?env=WINDOW&type=POR
/// GET: api/file/SOR.25.0000000E?env=WINDOW&type=SOR
///
/// nb: al momento solo window e file jwd
/// </summary>
/// <param name="id">uid oggetto</param>
/// <param name="objType">tipologia oggetto (SOR/POR)</param>
/// <param name="env">environment oggetto</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<IActionResult> porFile(string id, string objType = "SOR", Constants.EXECENVIRONMENTS env = Constants.EXECENVIRONMENTS.WINDOW)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (string.IsNullOrEmpty(id))
return NotFound();
// accetta SOLO env window...
if (env != Constants.EXECENVIRONMENTS.WINDOW)
{
return BadRequest("Only Window env");
}
else
{
string mimeType = "txt";
string fileName = id.Replace(".", "_").Replace(" ", "_").Replace(":", "_");
byte[] bytes = new byte[0];
// ...se ricevo percorso --> leggo jwd/svg cablato
if (!string.IsNullOrEmpty(id))
{
// se contiene i caratteri casuali x forzare reload --> li levo
if (id.Contains("-"))
{
id = id.Substring(0, id.IndexOf("-"));
}
// secondo del tipo + enivr decodifico valore corretto
switch (env)
{
case Constants.EXECENVIRONMENTS.NULL:
break;
case Constants.EXECENVIRONMENTS.WINDOW:
mimeType = "application/octet-stream";
fileName = $"{fileName}.jwd";
// recupero riga..
string jsonContent = "";// await _imgService.LoadSvgAsync(id, env);
// a seconda del tipo leggo da 2 aree db diverse...
if (objType == "POR")
{
var currPOR = await _DSService.OrderRowGetByUID(id);
jsonContent = currPOR?.SerStruct ?? "";
}
else if (objType == "SOR")
{
var currSOR = await _DSService.OfferRowGetByUID(id);
jsonContent = currSOR?.SerStruct ?? "";
}
// se NON vuoto --> converto byte stream...
if (!string.IsNullOrEmpty(jsonContent))
{
bytes = Encoding.UTF8.GetBytes(jsonContent);
}
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($"{mimeType} | {sw.Elapsed.TotalMilliseconds:N3} ms");
return File(bytes, mimeType, fileName);
}
}
#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; }
private DataLayerServices _DSService { get; set; }
#endregion Private Properties
}
}