mirror of
https://github.com/CannedNet/CannedNet.git
synced 2026-10-25 15:09:23 -07:00
chore: resize images based on query
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.8" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" 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;
|
||||
|
||||
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)
|
||||
{
|
||||
var imagesDir = "Images";
|
||||
if (!Directory.Exists(imagesDir))
|
||||
var imagesDir = Path.Combine("Images");
|
||||
|
||||
Directory.CreateDirectory(imagesDir);
|
||||
|
||||
app.MapGet("/{imageName}", async (
|
||||
HttpContext context,
|
||||
string imageName
|
||||
) =>
|
||||
{
|
||||
Directory.CreateDirectory(imagesDir);
|
||||
}
|
||||
byte[] imageBytes;
|
||||
|
||||
app.MapGet("/{imageId}", (string imageId) =>
|
||||
{
|
||||
if (!Directory.Exists(imagesDir))
|
||||
var filePath = Path.Combine(imagesDir, imageName);
|
||||
|
||||
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}.*");
|
||||
var filePath = files.FirstOrDefault();
|
||||
int? width = null;
|
||||
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();
|
||||
var contentType = extension switch
|
||||
using var image = Image.Load(imageBytes);
|
||||
|
||||
image.Mutate(x => x.Resize(new ResizeOptions
|
||||
{
|
||||
".png" => "image/png",
|
||||
".jpg" or ".jpeg" => "image/jpeg",
|
||||
".gif" => "image/gif",
|
||||
".webp" => "image/webp",
|
||||
".bmp" => "image/bmp",
|
||||
_ => "application/octet-stream"
|
||||
};
|
||||
Size = new Size(
|
||||
width ?? 0,
|
||||
height ?? 0
|
||||
),
|
||||
Mode = ResizeMode.Max
|
||||
}));
|
||||
|
||||
var fileStream = File.OpenRead(filePath);
|
||||
return Results.File(fileStream, contentType);
|
||||
});
|
||||
await using var output = new MemoryStream();
|
||||
|
||||
app.MapGet("/", (HttpContext context) =>
|
||||
{
|
||||
var imageId = context.Request.Query["imageId"].FirstOrDefault();
|
||||
var sig = context.Request.Query["sig"].FirstOrDefault();
|
||||
await image.SaveAsync(output, new PngEncoder());
|
||||
|
||||
if (string.IsNullOrEmpty(imageId))
|
||||
{
|
||||
return Results.BadRequest();
|
||||
}
|
||||
|
||||
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);
|
||||
return Results.File(
|
||||
output.ToArray(),
|
||||
MediaTypeNames.Image.Png
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user