Allow tokens to be ignored, add example random generation, tweak return for sources

main
exsersewo 2 years ago
parent 4ee98fa551
commit b92e0e5ec1
Signed by: exsersewo
GPG Key ID: 9C6DDF9AC9BA14A0
  1. 12
      src/Attributes/RequireTokenAttribute.cs
  2. 5
      src/Controllers/AmogusController.cs
  3. 35
      src/Controllers/ImageGenerationController.cs
  4. 1
      src/Controllers/ImageManipulationController.cs
  5. 22
      src/Extensions.cs
  6. 46
      src/Extensions/CollectionExtensions.cs
  7. 7
      src/Extensions/EnumHelper.cs
  8. 15
      src/Helpers/AmogusHelper.cs
  9. 36
      src/Managers/ImageManager.Generation.cs
  10. BIN
      src/wwwroot/imagegen/random/00uc8hweoec11.png
  11. BIN
      src/wwwroot/imagegen/random/08f4c5f1uhk11.jpg
  12. BIN
      src/wwwroot/imagegen/random/0dvynk93sqh11.jpg
  13. BIN
      src/wwwroot/imagegen/random/0m0x2ygrdph11.jpg
  14. BIN
      src/wwwroot/imagegen/random/122kk4janjh11.jpg
  15. BIN
      src/wwwroot/imagegen/random/13-5.jpg
  16. BIN
      src/wwwroot/imagegen/random/189jlz9sd6h11.jpg
  17. BIN
      src/wwwroot/imagegen/random/1aiiyfa16qj11.jpg
  18. BIN
      src/wwwroot/imagegen/random/1hh7hesnwmg11.jpg
  19. BIN
      src/wwwroot/imagegen/random/2gkcmtvsknk11.jpg
  20. BIN
      src/wwwroot/imagegen/random/2xc8ta241ef11.jpg
  21. BIN
      src/wwwroot/imagegen/random/3vdcuwqyvwj11.jpg
  22. BIN
      src/wwwroot/imagegen/random/54wr08npgwf11.gif
  23. BIN
      src/wwwroot/imagegen/random/56gpa81abpf11.png
  24. BIN
      src/wwwroot/imagegen/random/5aeb56nppuk11.png
  25. BIN
      src/wwwroot/imagegen/random/5spetics5kj11.jpg
  26. BIN
      src/wwwroot/imagegen/random/5vkjjdro5vf11.gif
  27. BIN
      src/wwwroot/imagegen/random/5vlcwan2lmh11.jpg
  28. BIN
      src/wwwroot/imagegen/random/6sfo1cna2pj11.jpg
  29. BIN
      src/wwwroot/imagegen/random/77x7ii00zvi11.jpg
  30. BIN
      src/wwwroot/imagegen/random/7l5n5ujzepi11.png
  31. BIN
      src/wwwroot/imagegen/random/857p7046ccf11.jpg
  32. BIN
      src/wwwroot/imagegen/random/8qa5fcz37gf11.jpg
  33. BIN
      src/wwwroot/imagegen/random/8qmocvg1m9j11.jpg
  34. BIN
      src/wwwroot/imagegen/random/8suq2wcsymf11.jpg
  35. BIN
      src/wwwroot/imagegen/random/93b0rp4612h11.jpg
  36. BIN
      src/wwwroot/imagegen/random/94w4fj9a1ai11.jpg
  37. BIN
      src/wwwroot/imagegen/random/9djtnidkcwf11.jpg
  38. BIN
      src/wwwroot/imagegen/random/9q16us8ysij11.jpg
  39. BIN
      src/wwwroot/imagegen/random/9sfe8wq12sh11.jpg
  40. BIN
      src/wwwroot/imagegen/random/a19wz17opyh11.jpg
  41. BIN
      src/wwwroot/imagegen/random/aUdeAY5.jpg
  42. BIN
      src/wwwroot/imagegen/random/aqnlxkgflvi11.jpg
  43. BIN
      src/wwwroot/imagegen/random/aqx6idxqekg11.jpg
  44. BIN
      src/wwwroot/imagegen/random/b04p5ymhr6h11.png
  45. BIN
      src/wwwroot/imagegen/random/bzj2nywuc8j11.png
  46. BIN
      src/wwwroot/imagegen/random/c429dw6y7ak11.jpg
  47. BIN
      src/wwwroot/imagegen/random/c7jy592gnsg11.jpg
  48. BIN
      src/wwwroot/imagegen/random/cx1hOwz.jpg
  49. BIN
      src/wwwroot/imagegen/random/cy200joshji11.jpg
  50. BIN
      src/wwwroot/imagegen/random/da1ponyybhf11.jpg

@ -1,6 +1,7 @@
using System.Net;
using Exsersewo.Common.Utilities;
using Kynareth.Helpers;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Kynareth.Attributes;
@ -10,7 +11,11 @@ public class RequireTokenAttribute : ActionFilterAttribute
{
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (await RequestHelper.IsRequestAuthenticatedAsync(context.HttpContext.Request))
var methodInfo = (ControllerActionDescriptor)context.ActionDescriptor;
var shouldIgnoreToken = methodInfo.MethodInfo.CustomAttributes.Any(x=>x.AttributeType == typeof(IgnoreTokenAttribute));
if (shouldIgnoreToken || await RequestHelper.IsRequestAuthenticatedAsync(context.HttpContext.Request))
{
await next();
return;
@ -23,4 +28,9 @@ public class RequireTokenAttribute : ActionFilterAttribute
await context.HttpContext.Response.WriteAsync(invalidRequest.ToJson(), context.HttpContext.RequestAborted);
return;
}
}
public class IgnoreTokenAttribute : Attribute
{
}

@ -1,7 +1,9 @@
using Exsersewo.Common.Utilities;
using ImageMagick;
using Kynareth.Attributes;
using Kynareth.Helpers;
using Microsoft.AspNetCore.Mvc;
using Kynareth.Extensions;
namespace Kynareth.Controllers;
@ -31,14 +33,17 @@ public class AmogusController : BaseController<AmogusController>
}
[HttpGet("roles")]
[IgnoreToken]
public async Task<object> GetAmongUsRoles()
=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusRole>().Except("None")));
[HttpGet("colours")]
[IgnoreToken]
public async Task<object> GetAmongUsColours()
=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusColour>().Except("None")));
[HttpGet("skins")]
[IgnoreToken]
public async Task<object> GetAmongUsSkins()
=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusSkin>().Except("None")));
}

@ -17,6 +17,7 @@ public class ImageGenerationController : BaseController<ImageGenerationControlle
[HttpGet]
[EndpointMetrics("image.generate.get.templates")]
[IgnoreToken]
[ProducesResponseType(typeof(IEnumerable<ImageGenerationEndpointRead>), StatusCodes.Status200OK)]
public Task<object> GetTemplates()
{
@ -53,5 +54,39 @@ public class ImageGenerationController : BaseController<ImageGenerationControlle
);
}
}
[HttpGet("{template}/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(string template, [FromQuery] string variant = null)
{
try
{
if (!string.IsNullOrWhiteSpace(template))
{
var result = await ImageManager.GenerateImageAsync(template, variant, Request.HttpContext.RequestAborted);
if (result is byte[] data)
{
var image = new MemoryStream(data);
return HttpContext.SendStream(image, "image/png");
}
return result as IResult;
}
return HttpContext.Send(EventResult.FromFailure("No template has been provided"), System.Net.HttpStatusCode.BadRequest);
}
catch (ArgumentException ex)
{
_logger.LogCritical(ex.Message, ex);
return HttpContext.Send(EventResult.FromFailure("Can't parse result"), System.Net.HttpStatusCode.InternalServerError);
}
}
}

@ -49,6 +49,7 @@ public class ImageManipulationController : BaseController<ImageManipulationContr
/// BOTTOM TEXT ///
/// ------------- ///
[HttpGet]
[IgnoreToken]
[EndpointMetrics("image.generate.get.templates")]
[ProducesResponseType(typeof(IEnumerable<ImageGenerationEndpointRead>), StatusCodes.Status200OK)]
public Task<object> GetTemplates()

@ -1,22 +0,0 @@
namespace Kynareth;
public static class Extensions
{
public static T GetRandomEnumValue<T>() where T : Enum
=> (T) Enum.GetValues(typeof(T)).OfType<Enum>().OrderBy(_ => Guid.NewGuid()).FirstOrDefault();
public static IList<int> Range(int start, int stop, int step)
{
var list = new List<int>();
for (int i = start; i < stop; i += step) {
list.Add(i);
}
return list;
}
public static T[] Except<T>(this T[] array, T specificValue) where T : IComparable {
return array.Where<T>(val => val.CompareTo(specificValue) != 0).ToArray();
}
}

@ -0,0 +1,46 @@
using Exsersewo.Common.Extensions;
namespace Kynareth.Extensions;
public static class CollectionExtensions
{
public static ICollection<T> GetRandomAmount<T>(this ICollection<T> stack, int amount)
{
T[] entries = new T[amount];
for (int x = 0; x < amount; x++)
{
entries[x] = stack.CryptoRandom();
}
return entries;
}
public static ICollection<T> GetRandomAmount<T>(this IEnumerable<T> stack, int amount)
{
T[] entries = new T[amount];
for (int x = 0; x < amount; x++)
{
entries[x] = stack.CryptoRandom();
}
return entries;
}
public static IList<int> Range(int start, int stop, int step)
{
var list = new List<int>();
for (int i = start; i < stop; i += step) {
list.Add(i);
}
return list;
}
public static T[] Except<T>(this T[] array, T specificValue) where T : IComparable
{
return array.Where<T>(val => val.CompareTo(specificValue) != 0).ToArray();
}
}

@ -0,0 +1,7 @@
namespace Kynareth.Extensions;
public static class EnumHelper
{
public static T GetRandomEnumValue<T>() where T : Enum
=> (T) Enum.GetValues(typeof(T)).OfType<Enum>().OrderBy(_ => Guid.NewGuid()).FirstOrDefault();
}

@ -3,7 +3,8 @@ using System.Numerics;
using System.Text;
using ImageMagick;
using Kynareth.Managers;
using Kynareth.Models;
using Kynareth.Extensions;
using CollectionExtensions = Kynareth.Extensions.CollectionExtensions;
namespace Kynareth.Helpers;
@ -412,7 +413,7 @@ public static class AmogusHelper
using MagickImage stars2 = new MagickImage(stars);
stars2.Distort(DistortMethod.ScaleRotateTranslate, 180);
foreach (var x in Extensions.Range(-800, width, 320))
foreach (var x in CollectionExtensions.Range(-800, width, 320))
{
baseImage.Composite(stars2, Gravity.Northwest, x, (int)(x / 60f) - 200, CompositeOperator.Over);
baseImage.Composite(stars, Gravity.Northwest, x, 100 + (int)(x / 120f), CompositeOperator.Over);
@ -458,16 +459,16 @@ public static class AmogusHelper
/// <returns>A <see cref="MemoryStream"/> containing the Amogus Ejected Gif</returns>
public static MagickImageCollection GenerateEjectionGif(string name, AmogusColour? colour = null, AmogusRole? role = null, AmogusSkin? skin = null, bool addStars = true)
{
if (role is null) role = Extensions.GetRandomEnumValue<AmogusRole>();
if (role is null) role = EnumHelper.GetRandomEnumValue<AmogusRole>();
return GenerateCustomEjectionGif(GenerateEjectionMessage(name, role.Value), colour, role, skin, addStars);
}
public static MagickImageCollection GenerateCustomEjectionGif(string text, AmogusColour? colour = null, AmogusRole? role = null, AmogusSkin? skin = null, bool addStars = true)
{
if (colour is null) colour = Extensions.GetRandomEnumValue<AmogusColour>();
if (role is null) role = Extensions.GetRandomEnumValue<AmogusRole>();
if (skin is null) skin = Extensions.GetRandomEnumValue<AmogusSkin>();
if (colour is null) colour = EnumHelper.GetRandomEnumValue<AmogusColour>();
if (role is null) role = EnumHelper.GetRandomEnumValue<AmogusRole>();
if (skin is null) skin = EnumHelper.GetRandomEnumValue<AmogusSkin>();
var crewmate = GenerateCrewmate(colour.Value, skin.Value);
crewmate = MakeSquare(crewmate);
@ -497,7 +498,7 @@ public static class AmogusHelper
stars3.BackgroundColor = MagickColors.Transparent;
}
var range = Extensions.Range(-120, background.Width * 3, 12);
var range = CollectionExtensions.Range(-120, background.Width * 3, 12);
MagickImageCollection gifFrames = new MagickImageCollection();

@ -1,20 +1,42 @@
using System.Collections.Immutable;
using Exsersewo.Common.Utilities;
using ImageMagick;
using Kynareth.Models;
using Kynareth.Extensions;
namespace Kynareth.Managers;
public static partial class ImageManager
{
private static List<ImageGenerationEndpoint> GenerationEndpoints;
private static IReadOnlyList<string> sourceFiles;
private static string GenerationTemplateBaseFolder;
static void ConfigureGeneration(IConfiguration configuration, IWebHostEnvironment _appEnvironment)
{
sourceFiles = Directory.EnumerateFiles(Path.Combine(_appEnvironment.WebRootPath, configuration.GetValue<string>("ShitpostBot:Folder"), "..", "random")).ToImmutableList();
GenerationTemplateBaseFolder = Path.Combine(_appEnvironment.WebRootPath, configuration.GetValue<string>("ShitpostBot:Folder"));
GenerationEndpoints = configuration.GetSection("ShitpostBot:Templates").Get<List<ImageGenerationEndpoint>>();
}
public static async Task<Object> GenerateImageAsync(string template, string variant, CancellationToken cancellationToken)
{
ImageGenerationImage imageTemplate = null;
var endpoint = GenerationEndpoints.FirstOrDefault(e => e.Name.ToLowerInvariant().Equals(template.ToLowerInvariant()));
if (endpoint is null)
{
var ex = new ArgumentException($"Couldn't find endpoint named \"{template}\"", nameof(template));
return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex));
}
var sources = sourceFiles.GetRandomAmount(endpoint.SourcesRequired) as string[];
return await GenerateImageAsync(template, variant, sources, cancellationToken);
}
public static async Task<object> GenerateImageAsync(string template, string variant, string[] sources, CancellationToken cancellationToken)
{
@ -102,7 +124,17 @@ public static partial class ImageManager
{
var position = template.Positions.ElementAtOrDefault(img);
using MagickImage srcImg = new MagickImage(await HttpWebClient.GetStreamAsync(new Uri(src)));
Stream imageData = null;
if (File.Exists(src))
{
imageData = new MemoryStream(await File.ReadAllBytesAsync(src));
}
else if (src.StartsWith("http"))
{
imageData = await HttpWebClient.GetStreamAsync(new Uri(src));
}
using MagickImage srcImg = new MagickImage(imageData);
srcImg.Resize(position.Width, position.Height);
@ -121,6 +153,8 @@ public static partial class ImageManager
img++;
}
return;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 110 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 422 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 303 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 272 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 685 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 753 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Loading…
Cancel
Save