229 lines
8.3 KiB
C#
229 lines
8.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using RestSharp;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EgwCoreLib.Lux.Data.Services
|
|
{
|
|
public class ImageCacheService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ImageCacheService(IConfiguration config, IConfiguration configuration, IRedisService redisService)
|
|
{
|
|
_config = config;
|
|
_redisService = redisService;
|
|
svgChannel = _config.GetValue<string>("ServerConf:SvgChannel") ?? "svg:img";
|
|
liveTag = _config.GetValue<string>("ServerConf:ImageLiveTag") ?? "svg";
|
|
cacheTag = _config.GetValue<string>("ServerConf:ImageFileTag") ?? "svgfile";
|
|
calcTag = _config.GetValue<string>("ServerConf:ImageCalcTag") ?? "svg-preview";
|
|
// verifico la url base
|
|
apiUrl = _config.GetValue<string>("ServerConf:Prog.ApiUrl") ?? "https://iis01.egalware.com/lux/srv/api/window";
|
|
// fix opzioni base del RestClient
|
|
restOptStd = new RestClientOptions
|
|
{
|
|
Timeout = TimeSpan.FromSeconds(60),
|
|
BaseUrl = new Uri(apiUrl)
|
|
};
|
|
Log.Info("ImageCache Service Started");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Effettua una generica chiamata ApiRest
|
|
/// </summary>
|
|
/// <param name="ApiUrl">URL Api di base</param>
|
|
/// <param name="ApiRequest">Richiesta completa</param>
|
|
/// <returns></returns>
|
|
public async Task<RestResponse> CallRestGet(string ApiUrl, string ApiRequest)
|
|
{
|
|
RestResponse response;
|
|
// cerco online
|
|
using (RestClient client = new RestClient(ApiUrl))
|
|
{
|
|
var request = new RestRequest(ApiRequest, Method.Get);
|
|
response = await client.GetAsync(request);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua una generica chiamata ApiRest
|
|
/// </summary>
|
|
/// <param name="ApiUrl">URL Api di base</param>
|
|
/// <param name="ApiRequest">Richiesta completa</param>
|
|
/// <param name="JsonBody">Corpo Json trasmesso con richiesta</param>
|
|
/// <returns></returns>
|
|
public async Task<RestResponse> CallRestPost(string ApiUrl, string ApiRequest, string JsonBody)
|
|
{
|
|
RestResponse response;
|
|
// cerco online
|
|
using (RestClient client = new RestClient(ApiUrl))
|
|
{
|
|
var request = new RestRequest(ApiRequest, Method.Post);
|
|
string rawData = JsonConvert.SerializeObject(JsonBody);
|
|
request.AddJsonBody(rawData);
|
|
response = await client.ExecutePostAsync(request);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola URL immagine dato
|
|
/// </summary>
|
|
/// <param name="baseUrl">Base url IMG server (da config)</param>
|
|
/// <param name="isLive">Live = in fase di calcolo (cache REDIS), altrimenti da filesystem</param>
|
|
/// <param name="imgUID">UID elemento/immagine</param>
|
|
/// <returns></returns>
|
|
public string ImageUrl(string baseUrl, bool isLive, string imgUID)
|
|
{
|
|
string tag = isLive ? liveTag : cacheTag;
|
|
if (!imgUID.EndsWith(".svg"))
|
|
{
|
|
imgUID += ".svg";
|
|
}
|
|
string fullUrl = $"{baseUrl}/{tag}/{imgUID}".Replace("////", "//");
|
|
return fullUrl;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera img redis SVG x uid richiesta
|
|
/// </summary>
|
|
/// <param name="imgUid"></param>
|
|
public string LoadSvg(string imgUid)
|
|
{
|
|
// recupero img da cache
|
|
string currKey = $"{redisBaseKey}:Images:{imgUid.Replace("/", ":")}";
|
|
var rawVal = _redisService.Get(currKey);
|
|
return $"{rawVal}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera img redis SVG x id porta richiesta
|
|
/// </summary>
|
|
/// <param name="imgId"></param>\
|
|
public async Task<string> LoadSvgAsync(string imgId)
|
|
{
|
|
// recupero img da cache
|
|
string currKey = $"{redisBaseKey}:Images:{imgId.Replace("/", ":")}";
|
|
var rawVal = await _redisService.GetAsync(currKey);
|
|
return $"{rawVal}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola URL immagine dato
|
|
/// </summary>
|
|
/// <param name="baseUrl">Base url IMG server (da config)</param>
|
|
/// <param name="imgUID">UID elemento/immagine</param>
|
|
/// <param name="serJwd">JWD serializzato di cui si richiede l'immagine aggiornata</param>
|
|
/// <returns></returns>
|
|
public string ReqUpdateSvg(string baseUrl, string imgUID, string serJwd)
|
|
{
|
|
string answ = "";
|
|
if (imgUID.EndsWith(".svg"))
|
|
{
|
|
imgUID.Replace(".svg", "");
|
|
}
|
|
string fullUrl = $"{baseUrl}/svg-preview/{imgUID}".Replace("////", "//");
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salva e invia su channel img redis SVG per il doc richiesto
|
|
/// </summary>
|
|
/// <param name="imgId"></param>
|
|
/// <param name="bomContent"></param>
|
|
public async Task<bool> SaveBomAsync(string imgId, string bomContent)
|
|
{
|
|
// salvo img in cache
|
|
string currKey = $"{redisBaseKey}:BOM:{imgId.Replace("/", ":")}";
|
|
var done = await _redisService.SetAsync(currKey, bomContent);
|
|
// invio notifica nuova svg generata sul canale... viene inviato solo svg con ID nel canale
|
|
string notifyChannel = $"{bomChannel}:{imgId}";
|
|
long numSent = await _redisService.PublishAsync(notifyChannel, bomContent);
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salva e invia su channel img redis SVG per il doc richiesto
|
|
/// </summary>
|
|
/// <param name="imgId"></param>
|
|
/// <param name="svgContent"></param>
|
|
/// <returns></returns>
|
|
public bool SaveSvg(string imgId, string svgContent)
|
|
{
|
|
// salvo img in cache
|
|
string currKey = $"{redisBaseKey}:Images:{imgId.Replace("/", ":")}";
|
|
var done = _redisService.Set(currKey, svgContent);
|
|
// invio notifica nuova imgsvg sul canale... viene "reimbustata"
|
|
string notifyChannel = $"svg-gen:{imgId}";
|
|
long numSent = _redisService.Publish(notifyChannel, svgContent);
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salva e invia su channel img redis SVG per il doc richiesto
|
|
/// </summary>
|
|
/// <param name="imgId"></param>
|
|
/// <param name="svgContent"></param>
|
|
public async Task<bool> SaveSvgAsync(string imgId, string svgContent)
|
|
{
|
|
// salvo img in cache
|
|
string currKey = $"{redisBaseKey}:Images:{imgId.Replace("/", ":")}";
|
|
var done = await _redisService.SetAsync(currKey, svgContent);
|
|
// invio notifica nuova svg generata sul canale... viene inviato solo svg con ID nel canale
|
|
string notifyChannel = $"{svgChannel}:{imgId}";
|
|
long numSent = await _redisService.PublishAsync(notifyChannel, svgContent);
|
|
return done;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private readonly IRedisService _redisService;
|
|
|
|
private readonly string apiUrl = "";
|
|
|
|
private readonly string bomChannel = "bom:item";
|
|
|
|
private readonly string cacheTag = "svgfile";
|
|
|
|
private readonly string calcTag = "svgpreview";
|
|
|
|
private readonly string liveTag = "svg";
|
|
|
|
private readonly string svgChannel = "svg:img";
|
|
|
|
private IConfiguration _config;
|
|
|
|
private string imageBaseUrl = "";
|
|
|
|
private string redisBaseKey = "Lux";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Conf client RestSharp standard:
|
|
/// - base URI al sito
|
|
/// - timeout 1 min
|
|
/// </summary>
|
|
private RestClientOptions restOptStd { get; set; } = new RestClientOptions();
|
|
|
|
#endregion Private Properties
|
|
}
|
|
} |