Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
sub resources work
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Nov 19, 2023
1 parent a00d68a commit c4db540
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 23 additions & 13 deletions Web2Gateway/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,24 @@ public static WebApplication ConfigureApi(this WebApplication app, ILogger logge
var referer = httpContext.Request.Headers["referer"].ToString();
if (!string.IsNullOrEmpty(referer) && !referer.Contains(storeId))
{
if (key.StartsWith("/"))
{
key = key[1..];
}
key = key.TrimStart('/');
httpContext.Response.Headers["Location"] = $"{referer}/{storeId}/{key}";
return Results.Redirect($"{referer}/{storeId}/{key}", true);
}
var hexKey = HexUtils.ToHex(key);
var g223 = app.Services.GetRequiredService<G2To3Service>();
var dataLayerResponse = await g223.GetValue(storeId, hexKey, cancellationToken);
if (dataLayerResponse is null)
var rawValue = await g223.GetValue(storeId, hexKey, cancellationToken);
if (rawValue is null)
{
Console.WriteLine($"couldn't find: {key}");
return Results.NotFound();
}
var decodedValue = HexUtils.FromHex(dataLayerResponse);
var fileExtension = Path.GetExtension(decodedValue);
Console.WriteLine($"found: {key}");
var decodedValue = HexUtils.FromHex(rawValue);
var fileExtension = Path.GetExtension(key);
if (Utils.TryParseJson(decodedValue, out var json) && json?.type == "multipart")
{
Expand All @@ -106,27 +105,38 @@ public static WebApplication ConfigureApi(this WebApplication app, ILogger logge
httpContext.Response.ContentType = mimeType;
await httpContext.Response.Body.WriteAsync(bytes, cancellationToken);
await httpContext.Response.CompleteAsync();
return Results.StatusCode(StatusCodes.Status200OK);
}
else if (!string.IsNullOrEmpty(fileExtension))
{
string mimeType = Utils.GetMimeType(fileExtension) ?? "application/octet-stream";
return Results.Content(decodedValue, mimeType);
httpContext.Response.ContentType = mimeType;
// this should work for text and images just all as bytes
await httpContext.Response.Body.WriteAsync(Convert.FromHexString(rawValue), cancellationToken);
await httpContext.Response.CompleteAsync();
return Results.StatusCode(StatusCodes.Status200OK);
}
else if (json is not null)
{
return Results.Ok(json);
}
else if (Utils.IsBase64Image(decodedValue))
{
// figure out the mime type
var regex = new Regex(@"[^:]\w+\/[\w-+\d.]+(?=;|,)");
var match = regex.Match(decodedValue);
// convert the base64 string to a byte array
string base64Image = decodedValue.Split(";base64,")[^1];
byte[] imageBuffer = Convert.FromBase64String(base64Image);
var regex = new Regex(@"[^:]\w+\/[\w-+\d.]+(?=;|,)");
var match = regex.Match(decodedValue);
httpContext.Response.ContentType = match.Value;
await httpContext.Response.Body.WriteAsync(imageBuffer, cancellationToken);
await httpContext.Response.CompleteAsync();
return Results.StatusCode(StatusCodes.Status200OK);
}
else
Expand Down
4 changes: 2 additions & 2 deletions Web2Gateway/HexUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class HexUtils
/// </summary>
/// <param name="input">The input string to convert.</param>
/// <returns>The hexadecimal representation of the input string.</returns>
public static string ToHex(this string input) => BitConverter.ToString(Encoding.UTF8.GetBytes(input)).Replace("-", "");
public static string ToHex(this string input) => BitConverter.ToString(Encoding.UTF8.GetBytes(input)).Replace("-", "").ToLowerInvariant();

/// <summary>
/// Converts a hexadecimal string to its corresponding string representation.
Expand All @@ -29,4 +29,4 @@ public static string FromHex(this string hex)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray());
}
}
}

0 comments on commit c4db540

Please sign in to comment.