From 6d450a503abc0c5d4a07f3e5ead80b7875f76df1 Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Sat, 14 Sep 2024 11:42:36 +0200 Subject: [PATCH] feat: support binary transport --- .../EspUtilities/EspUtilitiesInitExtension.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/DisplayUtil/EspUtilities/EspUtilitiesInitExtension.cs b/DisplayUtil/EspUtilities/EspUtilitiesInitExtension.cs index da41380..09e4241 100644 --- a/DisplayUtil/EspUtilities/EspUtilitiesInitExtension.cs +++ b/DisplayUtil/EspUtilities/EspUtilitiesInitExtension.cs @@ -4,6 +4,8 @@ namespace DisplayUtil.EspUtilities; public static class EspUtilitiesInitExtension { + private const string BinaryMediaType = "application/octet-stream"; + public const string CompressedImageRoute = "/esp/{providerId}", PlainImageRoute = "/esp/bits/{providerId}"; @@ -17,10 +19,17 @@ public static WebApplication UseEspUtilities(this WebApplication app) { app.MapGet(CompressedImageRoute, async (string providerId, HttpContext ctx, EspImageProvider espProvider) => { + var accept = ctx.Request.Headers.Accept; var (data, size) = await espProvider.GetAsRunLengthAsync(providerId); - var base64 = Convert.ToBase64String(data); ctx.Response.Headers.Append("X-Width", size.Width.ToString()); ctx.Response.Headers.Append("X-Height", size.Height.ToString()); + + if (accept.Contains(BinaryMediaType)) + { + return Results.Bytes(data, BinaryMediaType); + } + + var base64 = Convert.ToBase64String(data); return Results.Text(base64, "text/plain", Encoding.ASCII); }) .WithName("Get ESP Image") @@ -28,10 +37,17 @@ public static WebApplication UseEspUtilities(this WebApplication app) app.MapGet(PlainImageRoute, async (string providerId, HttpContext ctx, EspImageProvider espProvider) => { + var accept = ctx.Request.Headers.Accept; var (data, size) = await espProvider.GetAsPlainBytesAsync(providerId); - var base64 = Convert.ToBase64String(data); ctx.Response.Headers.Append("X-Width", size.Width.ToString()); ctx.Response.Headers.Append("X-Height", size.Height.ToString()); + + if (accept.Contains(BinaryMediaType)) + { + return Results.Bytes(data, BinaryMediaType); + } + + var base64 = Convert.ToBase64String(data); return Results.Text(base64, "text/plain", Encoding.ASCII); }) .WithName("Get ESP Bit Image")