From 9bf76764640eb5cfbc2e3613e442924062cc5495 Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 4 Sep 2026 01:07:27 +0200 Subject: [PATCH] =?UTF-8?q?SF-74:=20Client=20=E2=80=94=20CustomImagesServi?= =?UTF-8?q?ce=20(in-memory=20cache,=20GET=20/api/CustomImages),=20DI=20reg?= =?UTF-8?q?istration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Program.cs | 3 + .../Services/CustomImagesService.cs | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Services/CustomImagesService.cs diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Program.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Program.cs index 863f0b58..c6be87e1 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Program.cs +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Program.cs @@ -38,6 +38,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +// Logo configuration (loaded once after login, in-memory cached) +builder.Services.AddScoped(); + // DevExpress WASM builder.Services.AddDevExpressWebAssemblyBlazorPdfViewer(); builder.Services.AddDevExpressWebAssemblyBlazorReportViewer(); diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Services/CustomImagesService.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Services/CustomImagesService.cs new file mode 100644 index 00000000..d75cc3f7 --- /dev/null +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server.Client/Services/CustomImagesService.cs @@ -0,0 +1,55 @@ +using System.Net.Http.Json; +using System.Text.Json; + +namespace EnvelopeGenerator.Server.Client.Services; + +/// +/// Fetches and caches the CustomImages configuration (signFLOW + company logos) +/// from GET /api/CustomImages. +/// +/// Config changes rarely (deployment-time only), so results are cached in-memory +/// for the lifetime of the service (WASM app session). No TTL needed. +/// +public class CustomImagesService(IHttpClientFactory clientFactory) +{ + private static readonly JsonSerializerOptions _json = new(JsonSerializerDefaults.Web); + private const string Endpoint = "/api/CustomImages"; + + private CustomImagesDto? _cached; + + /// + /// Returns logo configuration. Fetches once from server; all subsequent calls + /// are served from memory with zero network overhead. + /// Returns defaults (empty Src) on auth failure (401) — components must handle gracefully. + /// + public async Task GetAsync(CancellationToken ct = default) + { + if (_cached is not null) + return _cached; + + try + { + var client = clientFactory.CreateClient("EnvelopeGenerator.Server"); + var response = await client.GetAsync(Endpoint, ct); + + if (!response.IsSuccessStatusCode) + { + // Not authenticated yet (login page) — return defaults silently. + _cached = new CustomImagesDto(); + return _cached; + } + + _cached = await response.Content.ReadFromJsonAsync(_json, ct) + ?? new CustomImagesDto(); + return _cached; + } + catch (Exception) + { + // Network error or server unavailable — return defaults so UI still renders. + return new CustomImagesDto(); + } + } + + /// Call after logout to force re-fetch on next login. + public void Invalidate() => _cached = null; +}