Meme Generation API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kynareth/src/Controllers/ImageGenerationController.cs

53 lines
1.5 KiB

using System.Text.Json;
using Exsersewo.Common.Utilities;
using ImageMagick;
using Kynareth.Attributes;
using Kynareth.Helpers;
using Kynareth.Managers;
using Kynareth.Models;
using Microsoft.AspNetCore.Mvc;
namespace Kynareth.Controllers;
[Route("image/generate")]
public class ImageGenerationController : BaseController<ImageGenerationController>
{
public ImageGenerationController(IConfiguration configuration, ILoggerFactory loggerFactory, IWebHostEnvironment environment) : base(configuration, loggerFactory, environment)
{ }
[HttpGet]
[EndpointMetrics("image.generate.get.templates")]
public Task<object> GetTemplates()
{
string serialized = JsonSerializer.Serialize(ImageManager.GetGenerationEndpoints());
return Task.FromResult(HttpContext.Send(EventResult.FromSuccess()));
}
[HttpGet("{template}")]
[EndpointMetrics("image.generate.generate")]
public async Task<object> GetImageAsync(string template, [FromQuery(Name = "source")] string[] sources, [FromQuery] string variant = null)
{
try
{
var result = await ImageManager.GenerateImageAsync(template, variant, sources);
if (result is byte[] data)
{
var image = new MemoryStream(data);
return HttpContext.SendStream(image, "image/png");
}
return result as IResult;
}
catch (ArgumentException ex)
{
return HttpContext.Send(
EventResult.FromFailureException($"Can't parse result. Reason:\"{ex.Message}\"", ex),
System.Net.HttpStatusCode.InternalServerError
);
}
}
}