Improved image endpoints, added size to ImageController
This commit is contained in:
parent
0491678055
commit
21afbf5a39
|
@ -1,5 +1,6 @@
|
||||||
using Microsoft.AspNetCore.Http.HttpResults;
|
using Microsoft.AspNetCore.Http.HttpResults;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using Wave.Services;
|
using Wave.Services;
|
||||||
|
|
||||||
namespace Wave.Controllers;
|
namespace Wave.Controllers;
|
||||||
|
@ -11,10 +12,11 @@ public class ImageController(ImageService imageService) : ControllerBase {
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("{imageId:guid}")]
|
[Route("{imageId:guid}")]
|
||||||
public IActionResult Get(Guid imageId) {
|
public async Task<IActionResult> Get(Guid imageId, [FromQuery, Range(16, 800)] int size = 800) {
|
||||||
string? path = ImageService.GetPath(imageId);
|
string? path = ImageService.GetPath(imageId);
|
||||||
|
|
||||||
if (path is null) return NotFound();
|
if (path is null) return NotFound();
|
||||||
|
if (size < 800) return File(await ImageService.GetResized(path, size), ImageService.ImageMimeType);
|
||||||
return File(System.IO.File.OpenRead(path), ImageService.ImageMimeType);
|
return File(System.IO.File.OpenRead(path), ImageService.ImageMimeType);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using ImageMagick;
|
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.OutputCaching;
|
using Microsoft.AspNetCore.OutputCaching;
|
||||||
|
@ -31,15 +30,7 @@ public class UserController(ImageService imageService, IDbContextFactory<Applica
|
||||||
string? path = ImageService.GetPath(user.ProfilePicture.ImageId);
|
string? path = ImageService.GetPath(user.ProfilePicture.ImageId);
|
||||||
if (path is null) return NotFound();
|
if (path is null) return NotFound();
|
||||||
|
|
||||||
if (size < 800) {
|
if (size < 800) return File(await ImageService.GetResized(path, size), ImageService.ImageMimeType);
|
||||||
var image = new MagickImage(path);
|
|
||||||
image.Resize(new MagickGeometry(size));
|
|
||||||
using var memory = new MemoryStream();
|
|
||||||
await image.WriteAsync(memory);
|
|
||||||
|
|
||||||
return File(memory.GetBuffer(), ImageService.ImageMimeType);
|
|
||||||
}
|
|
||||||
|
|
||||||
return File(System.IO.File.OpenRead(path), ImageService.ImageMimeType);
|
return File(System.IO.File.OpenRead(path), ImageService.ImageMimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using ImageMagick;
|
using ImageMagick;
|
||||||
using static System.Net.Mime.MediaTypeNames;
|
|
||||||
|
|
||||||
namespace Wave.Services;
|
namespace Wave.Services;
|
||||||
|
|
||||||
|
@ -14,7 +13,16 @@ public class ImageService(ILogger<ImageService> logger) {
|
||||||
return File.Exists(path) ? path : null;
|
return File.Exists(path) ? path : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask<Guid?> StoreImageAsync(string temporaryPath, int size = 800, CancellationToken cancellation = default) {
|
public async Task<byte[]> GetResized(string path, int size) {
|
||||||
|
var image = new MagickImage(path);
|
||||||
|
image.Resize(new MagickGeometry(size));
|
||||||
|
using var memory = new MemoryStream();
|
||||||
|
await image.WriteAsync(memory);
|
||||||
|
return memory.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask<Guid?> StoreImageAsync(string temporaryPath, int size = 800,
|
||||||
|
CancellationToken cancellation = default) {
|
||||||
if (File.Exists(temporaryPath) is not true) return null;
|
if (File.Exists(temporaryPath) is not true) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -28,9 +36,7 @@ public class ImageService(ILogger<ImageService> logger) {
|
||||||
image.Format = MagickFormat.Jpeg;
|
image.Format = MagickFormat.Jpeg;
|
||||||
image.Quality = 90;
|
image.Quality = 90;
|
||||||
|
|
||||||
if (image.GetExifProfile() is not null) {
|
if (image.GetExifProfile() is { } exifProfile) image.RemoveProfile(exifProfile);
|
||||||
image.RemoveProfile(image.GetExifProfile());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overwrite exif for privacy reasons
|
// Overwrite exif for privacy reasons
|
||||||
var exif = new ExifProfile {
|
var exif = new ExifProfile {
|
||||||
|
|
Loading…
Reference in a new issue