mirror of
https://github.com/CannedNet/CannedNet.git
synced 2026-10-25 23:18:54 -07:00
45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using CannedNet.Services.Infrastructure;
|
|
|
|
namespace CannedNet.Services.Controllers;
|
|
|
|
public class ImageController
|
|
{
|
|
public WebApplicationBuilder Initialize(string[]? args = null) => ServiceExtensions.CreateRecNetBuilder(args);
|
|
|
|
public void MapEndpoints(WebApplication app)
|
|
{
|
|
app.MapGet("/{imageId}", (string imageId, HttpContext context) =>
|
|
{
|
|
var imagesDir = "Images";
|
|
|
|
if (!Directory.Exists(imagesDir))
|
|
{
|
|
context.Response.StatusCode = 404;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
var files = Directory.GetFiles(imagesDir, $"{imageId}.*");
|
|
var filePath = files.FirstOrDefault();
|
|
|
|
if (filePath == null)
|
|
{
|
|
context.Response.StatusCode = 404;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
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"
|
|
};
|
|
|
|
context.Response.ContentType = contentType;
|
|
return File.OpenRead(filePath).CopyToAsync(context.Response.Body);
|
|
});
|
|
}
|
|
} |