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>
|
</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("/{imageId}", (string imageId) =>
|
Directory.CreateDirectory(imagesDir);
|
||||||
{
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.MapGet("/", (HttpContext context) =>
|
app.MapGet("/{imageName}", async (
|
||||||
|
HttpContext context,
|
||||||
|
string imageName
|
||||||
|
) =>
|
||||||
{
|
{
|
||||||
var imageId = context.Request.Query["imageId"].FirstOrDefault();
|
byte[] imageBytes;
|
||||||
var sig = context.Request.Query["sig"].FirstOrDefault();
|
|
||||||
|
var filePath = Path.Combine(imagesDir, imageName);
|
||||||
if (string.IsNullOrEmpty(imageId))
|
|
||||||
|
if (File.Exists(filePath))
|
||||||
{
|
{
|
||||||
return Results.BadRequest();
|
imageBytes = await File.ReadAllBytesAsync(filePath);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
if (!Directory.Exists(imagesDir))
|
|
||||||
{
|
{
|
||||||
return Results.NotFound();
|
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"
|
}));
|
||||||
};
|
|
||||||
|
await using var output = new MemoryStream();
|
||||||
var fileStream = File.OpenRead(filePath);
|
|
||||||
return Results.File(fileStream, contentType);
|
await image.SaveAsync(output, new PngEncoder());
|
||||||
|
|
||||||
|
return Results.File(
|
||||||
|
output.ToArray(),
|
||||||
|
MediaTypeNames.Image.Png
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user