using EgwCoreLib.Lux.Data.Services.Sales;
using System.Text;
namespace Lux.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FileController : ControllerBase
{
#region Public Constructors
///
/// Controller per gestione file (es download JWD)
///
///
///
///
///
public FileController(IDataLayerServices DLService, IOfferRowService iORService, IImageCacheService imgServ)
{
_DSService = DLService;
_OffRService = iORService;
_imgService = imgServ;
}
#endregion Public Constructors
#if false
///
/// 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");
}
///
/// 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();
Log.Info($"svgString | {sw.Elapsed.TotalMilliseconds:N3} ms");
return File(bytes, "image/svg+xml");
}
#endif
#region Public Methods
///
/// 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
///
/// uid oggetto
/// tipologia oggetto (SOR/POR)
/// environment oggetto
///
[HttpGet("{id}")]
public async Task 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 _OrdRService.GetByUidAsync(id);
jsonContent = currPOR?.SerStruct ?? "";
}
else if (objType == "SOR")
{
var currSOR = await _OffRService.GetByUidAsync(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);
}
}
[HttpGet("version")]
public string version()
{
var version = Assembly
.GetExecutingAssembly()
.GetName()
.Version?
.ToString() ?? "unknown";
return version;
}
#endregion Public Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Properties
private readonly IDataLayerServices _DSService;
private readonly IOfferRowService _OffRService;
private readonly IOrderRowService _OrdRService;
private readonly IImageCacheService _imgService;
#endregion Private Properties
}
}