Skip to content

Commit

Permalink
feat: support binary transport
Browse files Browse the repository at this point in the history
  • Loading branch information
timia2109 committed Sep 14, 2024
1 parent cdc875a commit 6d450a5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions DisplayUtil/EspUtilities/EspUtilitiesInitExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";

Expand All @@ -17,21 +19,35 @@ 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")
.WithOpenApi();

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")
Expand Down

0 comments on commit 6d450a5

Please sign in to comment.