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.
59 lines
2.0 KiB
59 lines
2.0 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")]
|
|
[ProducesResponseType(typeof(IEnumerable<ImageGenerationEndpointRead>), StatusCodes.Status200OK)]
|
|
public Task<object> GetTemplates()
|
|
{
|
|
string serialized = JsonSerializer.Serialize(ImageManager.GetGenerationEndpoints());
|
|
|
|
return Task.FromResult(HttpContext.Send(EventResult.FromSuccess(serialized)));
|
|
}
|
|
|
|
[HttpGet("{template}")]
|
|
[EndpointMetrics("image.generate.generate")]
|
|
[ProducesResponseType(typeof(Stream), StatusCodes.Status200OK, "image/png")]
|
|
[ProducesResponseType(typeof(Stream), StatusCodes.Status200OK, "image/gif")]
|
|
[ProducesResponseType(typeof(EventResult), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(EventResult), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(EventResult), StatusCodes.Status500InternalServerError)]
|
|
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
|
|
);
|
|
}
|
|
}
|
|
|
|
} |