Add complete random to image generation

main
exsersewo 2 years ago
parent b92e0e5ec1
commit c91d3975b7
Signed by: exsersewo
GPG Key ID: 9C6DDF9AC9BA14A0
  1. 29
      src/Controllers/ImageGenerationController.cs
  2. 25
      src/Managers/ImageManager.Generation.cs

@ -86,6 +86,35 @@ public class ImageGenerationController : BaseController<ImageGenerationControlle
{ {
_logger.LogCritical(ex.Message, ex); _logger.LogCritical(ex.Message, ex);
return HttpContext.Send(EventResult.FromFailure("Can't parse result"), System.Net.HttpStatusCode.InternalServerError);
}
}
[HttpGet("random")]
[EndpointMetrics("image.generate.generate.random")]
[ProducesResponseType(typeof(Stream), StatusCodes.Status200OK, "image/png")]
[ProducesResponseType(typeof(EventResult), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(EventResult), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(EventResult), StatusCodes.Status500InternalServerError)]
[IgnoreToken]
public async Task<object> GenerateMemeAsync()
{
try
{
var result = await ImageManager.GenerateImageAsync(Request.HttpContext.RequestAborted);
if (result is byte[] data)
{
var image = new MemoryStream(data);
return HttpContext.SendStream(image, "image/png");
}
return result as IResult;
}
catch (ArgumentException ex)
{
_logger.LogCritical(ex.Message, ex);
return HttpContext.Send(EventResult.FromFailure("Can't parse result"), System.Net.HttpStatusCode.InternalServerError); return HttpContext.Send(EventResult.FromFailure("Can't parse result"), System.Net.HttpStatusCode.InternalServerError);
} }
} }

@ -1,4 +1,5 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using Exsersewo.Common.Extensions;
using Exsersewo.Common.Utilities; using Exsersewo.Common.Utilities;
using ImageMagick; using ImageMagick;
using Kynareth.Models; using Kynareth.Models;
@ -20,6 +21,30 @@ public static partial class ImageManager
GenerationEndpoints = configuration.GetSection("ShitpostBot:Templates").Get<List<ImageGenerationEndpoint>>(); GenerationEndpoints = configuration.GetSection("ShitpostBot:Templates").Get<List<ImageGenerationEndpoint>>();
} }
public static async Task<Object> GenerateImageAsync(CancellationToken cancellationToken)
{
ImageGenerationImage imageTemplate = null;
var endpoint = GenerationEndpoints.Random();
if (endpoint is null)
{
var ex = new ArgumentException($"Couldn't find endpoint");
return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex));
}
string variant = null;
if (endpoint.Variants.Any() && TrueRandom.Next(0, 50) > 25)
{
variant = endpoint.Variants.Random().Name;
}
var sources = sourceFiles.GetRandomAmount(endpoint.SourcesRequired) as string[];
return await GenerateImageAsync(endpoint.Name, variant, sources, cancellationToken);
}
public static async Task<Object> GenerateImageAsync(string template, string variant, CancellationToken cancellationToken) public static async Task<Object> GenerateImageAsync(string template, string variant, CancellationToken cancellationToken)
{ {
ImageGenerationImage imageTemplate = null; ImageGenerationImage imageTemplate = null;

Loading…
Cancel
Save