resizing images ondemand

This commit is contained in:
ju09279
2024-08-16 00:28:14 +02:00
parent c3c4de3c44
commit b4ac10c006
9 changed files with 119 additions and 69 deletions

View File

@@ -2,20 +2,23 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMemoryCache();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
@@ -28,14 +31,14 @@ app.MapGet("/ping", () => "pong");
app.MapGet("/api/v1/songs/{hash}", (string hash) =>
{
return Results.Ok(new { hash });
return Results.Ok(new { hash });
});
app.MapGet("/api/v1/songs/recent", (int? limit, int? offset) =>
{
var limitValue = limit ?? 100; // default to 10 if not provided
var offsetValue = offset ?? 0; // default to 0 if not provided
return Results.Json(Osudb.Instance.GetRecent(limitValue, offsetValue));
});
@@ -54,22 +57,22 @@ app.MapGet("/api/v1/songs/{hash}", (string hash) =>
app.MapGet("/api/v1/collections/", async (int? limit, int? offset, [FromServices] IMemoryCache cache) =>
{
const string cacheKey = "collections";
if (!cache.TryGetValue(cacheKey, out var collections))
{
collections = Osudb.Instance.GetCollections();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromDays(1))
.SetAbsoluteExpiration(TimeSpan.FromDays(3));
cache.Set(cacheKey, collections, cacheEntryOptions);
const string cacheKey = "collections";
if (!cache.TryGetValue(cacheKey, out var collections))
{
collections = Osudb.Instance.GetCollections();
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromDays(1))
.SetAbsoluteExpiration(TimeSpan.FromDays(3));
cache.Set(cacheKey, collections, cacheEntryOptions);
}
return Results.Json(collections);
return Results.Json(collections);
});
app.MapGet("/api/v1/collection/{index}", (int index) =>
@@ -103,7 +106,7 @@ app.MapGet("/api/v1/audio/{*fileName}", async (string fileName, HttpContext cont
});
app.MapGet("/api/v1/images/{*filename}", async (string filename) =>
app.MapGet("/api/v1/images/{*filename}", async (string filename, int? h, int? w) =>
{
var decodedFileName = Uri.UnescapeDataString(filename);
var filePath = Path.Combine(Osudb.osufolder, "Songs", decodedFileName);
@@ -124,12 +127,72 @@ app.MapGet("/api/v1/images/{*filename}", async (string filename) =>
".webp" => "image/webp",
_ => "application/octet-stream",
};
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true);
if (w == null || h == null)
{
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true);
return Results.Stream(fileStream, contentType, filename);
}
using var originalImage = new Bitmap(filePath);
// If resizing is requested, resize the image
Bitmap resizedImage;
if (w.HasValue || h.HasValue)
{
resizedImage = ResizeImage(originalImage, w, h);
}
else
{
resizedImage = new Bitmap(originalImage); // Keep original size
}
// Convert the resized image to a memory stream
var memoryStream = new MemoryStream();
resizedImage.Save(memoryStream, GetImageFormat(fileExtension));
memoryStream.Position = 0; // Reset stream position
return Results.File(memoryStream, contentType);
return Results.Stream(fileStream, contentType, filename);
});
static Bitmap ResizeImage(Image originalImage, int? width, int? height)
{
int newWidth = width ?? originalImage.Width;
int newHeight = height ?? originalImage.Height;
if (width == null)
{
newWidth = originalImage.Width * newHeight / originalImage.Height;
}
else if (height == null)
{
newHeight = originalImage.Height * newWidth / originalImage.Width;
}
var resizedImage = new Bitmap(newWidth, newHeight);
using (var graphics = Graphics.FromImage(resizedImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(originalImage, 0, 0, newWidth, newHeight);
}
return resizedImage;
}
static ImageFormat GetImageFormat(string extension)
{
return extension switch
{
".jpg" or ".jpeg" => ImageFormat.Jpeg,
".png" => ImageFormat.Png,
".gif" => ImageFormat.Gif,
".bmp" => ImageFormat.Bmp,
".webp" => ImageFormat.Webp,
_ => ImageFormat.Png,
};
}
app.Run();