chore: resize images based on query

This commit is contained in:
Lapis
2026-05-21 19:08:12 -04:00
parent 76b7c2ca88
commit 01277dfcbe
3 changed files with 62 additions and 64 deletions
+1
View File
@@ -9,6 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.8" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.2" /> <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

@@ -1,85 +1,82 @@
using CannedNet.Services.Infrastructure; using System.Net.Mime;
using CannedNet.Services.Infrastructure;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;
namespace CannedNet.Services.Controllers; namespace CannedNet.Services.Controllers;
public class ImageController public class ImageController
{ {
public WebApplicationBuilder Initialize(string[]? args = null) => ServiceExtensions.CreateRecNetBuilder(args); private static readonly HttpClient HttpClient = new();
public WebApplicationBuilder Initialize(string[]? args = null)
=> ServiceExtensions.CreateRecNetBuilder(args);
public void MapEndpoints(WebApplication app) public void MapEndpoints(WebApplication app)
{ {
var imagesDir = "Images"; var imagesDir = Path.Combine("Images");
if (!Directory.Exists(imagesDir))
Directory.CreateDirectory(imagesDir);
app.MapGet("/{imageName}", async (
HttpContext context,
string imageName
) =>
{ {
Directory.CreateDirectory(imagesDir); byte[] imageBytes;
}
app.MapGet("/{imageId}", (string imageId) => var filePath = Path.Combine(imagesDir, imageName);
{
if (!Directory.Exists(imagesDir)) if (File.Exists(filePath))
{ {
return Results.NotFound(); imageBytes = await File.ReadAllBytesAsync(filePath);
}
else
{
var response = await HttpClient.GetAsync(
$"https://cdn.rec.net/img/{imageName}"
);
if (!response.IsSuccessStatusCode)
return Results.NotFound();
imageBytes = await response.Content.ReadAsByteArrayAsync();
} }
var files = Directory.GetFiles(imagesDir, $"{imageId}.*"); int? width = null;
var filePath = files.FirstOrDefault(); int? height = null;
if (filePath == null) if (int.TryParse(context.Request.Query["width"], out var w))
width = w;
if (int.TryParse(context.Request.Query["height"], out var h))
height = h;
if (width == null && height == null)
{ {
return Results.NotFound(); return Results.File(imageBytes, MediaTypeNames.Image.Png);
} }
var extension = Path.GetExtension(filePath).ToLowerInvariant(); using var image = Image.Load(imageBytes);
var contentType = extension switch
image.Mutate(x => x.Resize(new ResizeOptions
{ {
".png" => "image/png", Size = new Size(
".jpg" or ".jpeg" => "image/jpeg", width ?? 0,
".gif" => "image/gif", height ?? 0
".webp" => "image/webp", ),
".bmp" => "image/bmp", Mode = ResizeMode.Max
_ => "application/octet-stream" }));
};
var fileStream = File.OpenRead(filePath); await using var output = new MemoryStream();
return Results.File(fileStream, contentType);
});
app.MapGet("/", (HttpContext context) => await image.SaveAsync(output, new PngEncoder());
{
var imageId = context.Request.Query["imageId"].FirstOrDefault();
var sig = context.Request.Query["sig"].FirstOrDefault();
if (string.IsNullOrEmpty(imageId)) return Results.File(
{ output.ToArray(),
return Results.BadRequest(); MediaTypeNames.Image.Png
} );
if (!Directory.Exists(imagesDir))
{
return Results.NotFound();
}
var files = Directory.GetFiles(imagesDir, $"{imageId}.*");
var filePath = files.FirstOrDefault();
if (filePath == null)
{
return Results.NotFound();
}
var extension = Path.GetExtension(filePath).ToLowerInvariant();
var contentType = extension switch
{
".png" => "image/png",
".jpg" or ".jpeg" => "image/jpeg",
".gif" => "image/gif",
".webp" => "image/webp",
".bmp" => "image/bmp",
_ => "application/octet-stream"
};
var fileStream = File.OpenRead(filePath);
return Results.File(fileStream, contentType);
}); });
} }
} }