Add complete random to image generation
This commit is contained in:
@@ -86,6 +86,35 @@ public class ImageGenerationController : BaseController<ImageGenerationControlle
|
||||
{
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Immutable;
|
||||
using Exsersewo.Common.Extensions;
|
||||
using Exsersewo.Common.Utilities;
|
||||
using ImageMagick;
|
||||
using Kynareth.Models;
|
||||
@@ -19,6 +20,30 @@ public static partial class ImageManager
|
||||
GenerationTemplateBaseFolder = Path.Combine(_appEnvironment.WebRootPath, configuration.GetValue<string>("ShitpostBot:Folder"));
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user