diff --git a/src/Attributes/RequireTokenAttribute.cs b/src/Attributes/RequireTokenAttribute.cs index 39e4b18..575c252 100644 --- a/src/Attributes/RequireTokenAttribute.cs +++ b/src/Attributes/RequireTokenAttribute.cs @@ -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 +{ + } \ No newline at end of file diff --git a/src/Controllers/AmogusController.cs b/src/Controllers/AmogusController.cs index a1b80d8..3c67a7c 100644 --- a/src/Controllers/AmogusController.cs +++ b/src/Controllers/AmogusController.cs @@ -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 } [HttpGet("roles")] + [IgnoreToken] public async Task GetAmongUsRoles() => Ok(EventResult.FromSuccess(Enum.GetNames().Except("None"))); [HttpGet("colours")] + [IgnoreToken] public async Task GetAmongUsColours() => Ok(EventResult.FromSuccess(Enum.GetNames().Except("None"))); [HttpGet("skins")] + [IgnoreToken] public async Task GetAmongUsSkins() => Ok(EventResult.FromSuccess(Enum.GetNames().Except("None"))); } \ No newline at end of file diff --git a/src/Controllers/ImageGenerationController.cs b/src/Controllers/ImageGenerationController.cs index 9cbda5b..26f0dbc 100644 --- a/src/Controllers/ImageGenerationController.cs +++ b/src/Controllers/ImageGenerationController.cs @@ -17,6 +17,7 @@ public class ImageGenerationController : BaseController), StatusCodes.Status200OK)] public Task GetTemplates() { @@ -53,5 +54,39 @@ public class ImageGenerationController : BaseController 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); + } + } } \ No newline at end of file diff --git a/src/Controllers/ImageManipulationController.cs b/src/Controllers/ImageManipulationController.cs index b194303..9332a3e 100644 --- a/src/Controllers/ImageManipulationController.cs +++ b/src/Controllers/ImageManipulationController.cs @@ -49,6 +49,7 @@ public class ImageManipulationController : BaseController), StatusCodes.Status200OK)] public Task GetTemplates() diff --git a/src/Extensions.cs b/src/Extensions.cs deleted file mode 100644 index 3d32fb7..0000000 --- a/src/Extensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace Kynareth; - -public static class Extensions -{ - public static T GetRandomEnumValue() where T : Enum - => (T) Enum.GetValues(typeof(T)).OfType().OrderBy(_ => Guid.NewGuid()).FirstOrDefault(); - - public static IList Range(int start, int stop, int step) - { - var list = new List(); - - for (int i = start; i < stop; i += step) { - list.Add(i); - } - - return list; - } - - public static T[] Except(this T[] array, T specificValue) where T : IComparable { - return array.Where(val => val.CompareTo(specificValue) != 0).ToArray(); - } -} \ No newline at end of file diff --git a/src/Extensions/CollectionExtensions.cs b/src/Extensions/CollectionExtensions.cs new file mode 100644 index 0000000..6506c82 --- /dev/null +++ b/src/Extensions/CollectionExtensions.cs @@ -0,0 +1,46 @@ +using Exsersewo.Common.Extensions; + +namespace Kynareth.Extensions; + +public static class CollectionExtensions +{ + public static ICollection GetRandomAmount(this ICollection stack, int amount) + { + T[] entries = new T[amount]; + + for (int x = 0; x < amount; x++) + { + entries[x] = stack.CryptoRandom(); + } + + return entries; + } + + public static ICollection GetRandomAmount(this IEnumerable stack, int amount) + { + T[] entries = new T[amount]; + + for (int x = 0; x < amount; x++) + { + entries[x] = stack.CryptoRandom(); + } + + return entries; + } + + public static IList Range(int start, int stop, int step) + { + var list = new List(); + + for (int i = start; i < stop; i += step) { + list.Add(i); + } + + return list; + } + + public static T[] Except(this T[] array, T specificValue) where T : IComparable + { + return array.Where(val => val.CompareTo(specificValue) != 0).ToArray(); + } +} \ No newline at end of file diff --git a/src/Extensions/EnumHelper.cs b/src/Extensions/EnumHelper.cs new file mode 100644 index 0000000..d26a71a --- /dev/null +++ b/src/Extensions/EnumHelper.cs @@ -0,0 +1,7 @@ +namespace Kynareth.Extensions; + +public static class EnumHelper +{ + public static T GetRandomEnumValue() where T : Enum + => (T) Enum.GetValues(typeof(T)).OfType().OrderBy(_ => Guid.NewGuid()).FirstOrDefault(); +} \ No newline at end of file diff --git a/src/Helpers/AmogusHelper.cs b/src/Helpers/AmogusHelper.cs index d551e5e..891af10 100644 --- a/src/Helpers/AmogusHelper.cs +++ b/src/Helpers/AmogusHelper.cs @@ -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 /// A containing the Amogus Ejected Gif 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(); + if (role is null) role = EnumHelper.GetRandomEnumValue(); 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(); - if (role is null) role = Extensions.GetRandomEnumValue(); - if (skin is null) skin = Extensions.GetRandomEnumValue(); + if (colour is null) colour = EnumHelper.GetRandomEnumValue(); + if (role is null) role = EnumHelper.GetRandomEnumValue(); + if (skin is null) skin = EnumHelper.GetRandomEnumValue(); 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(); diff --git a/src/Managers/ImageManager.Generation.cs b/src/Managers/ImageManager.Generation.cs index ba3a227..f623125 100644 --- a/src/Managers/ImageManager.Generation.cs +++ b/src/Managers/ImageManager.Generation.cs @@ -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 GenerationEndpoints; + private static IReadOnlyList sourceFiles; private static string GenerationTemplateBaseFolder; static void ConfigureGeneration(IConfiguration configuration, IWebHostEnvironment _appEnvironment) { + sourceFiles = Directory.EnumerateFiles(Path.Combine(_appEnvironment.WebRootPath, configuration.GetValue("ShitpostBot:Folder"), "..", "random")).ToImmutableList(); GenerationTemplateBaseFolder = Path.Combine(_appEnvironment.WebRootPath, configuration.GetValue("ShitpostBot:Folder")); GenerationEndpoints = configuration.GetSection("ShitpostBot:Templates").Get>(); } + + public static async Task 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 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; } } diff --git a/src/wwwroot/imagegen/random/00uc8hweoec11.png b/src/wwwroot/imagegen/random/00uc8hweoec11.png new file mode 100644 index 0000000..b2c35dc Binary files /dev/null and b/src/wwwroot/imagegen/random/00uc8hweoec11.png differ diff --git a/src/wwwroot/imagegen/random/08f4c5f1uhk11.jpg b/src/wwwroot/imagegen/random/08f4c5f1uhk11.jpg new file mode 100644 index 0000000..e214707 Binary files /dev/null and b/src/wwwroot/imagegen/random/08f4c5f1uhk11.jpg differ diff --git a/src/wwwroot/imagegen/random/0dvynk93sqh11.jpg b/src/wwwroot/imagegen/random/0dvynk93sqh11.jpg new file mode 100644 index 0000000..0b12121 Binary files /dev/null and b/src/wwwroot/imagegen/random/0dvynk93sqh11.jpg differ diff --git a/src/wwwroot/imagegen/random/0m0x2ygrdph11.jpg b/src/wwwroot/imagegen/random/0m0x2ygrdph11.jpg new file mode 100644 index 0000000..554fefa Binary files /dev/null and b/src/wwwroot/imagegen/random/0m0x2ygrdph11.jpg differ diff --git a/src/wwwroot/imagegen/random/122kk4janjh11.jpg b/src/wwwroot/imagegen/random/122kk4janjh11.jpg new file mode 100644 index 0000000..7001e03 Binary files /dev/null and b/src/wwwroot/imagegen/random/122kk4janjh11.jpg differ diff --git a/src/wwwroot/imagegen/random/13-5.jpg b/src/wwwroot/imagegen/random/13-5.jpg new file mode 100644 index 0000000..4c94642 Binary files /dev/null and b/src/wwwroot/imagegen/random/13-5.jpg differ diff --git a/src/wwwroot/imagegen/random/189jlz9sd6h11.jpg b/src/wwwroot/imagegen/random/189jlz9sd6h11.jpg new file mode 100644 index 0000000..bc59cf2 Binary files /dev/null and b/src/wwwroot/imagegen/random/189jlz9sd6h11.jpg differ diff --git a/src/wwwroot/imagegen/random/1aiiyfa16qj11.jpg b/src/wwwroot/imagegen/random/1aiiyfa16qj11.jpg new file mode 100644 index 0000000..bd9f115 Binary files /dev/null and b/src/wwwroot/imagegen/random/1aiiyfa16qj11.jpg differ diff --git a/src/wwwroot/imagegen/random/1hh7hesnwmg11.jpg b/src/wwwroot/imagegen/random/1hh7hesnwmg11.jpg new file mode 100644 index 0000000..9ae7255 Binary files /dev/null and b/src/wwwroot/imagegen/random/1hh7hesnwmg11.jpg differ diff --git a/src/wwwroot/imagegen/random/2gkcmtvsknk11.jpg b/src/wwwroot/imagegen/random/2gkcmtvsknk11.jpg new file mode 100644 index 0000000..4c4a039 Binary files /dev/null and b/src/wwwroot/imagegen/random/2gkcmtvsknk11.jpg differ diff --git a/src/wwwroot/imagegen/random/2xc8ta241ef11.jpg b/src/wwwroot/imagegen/random/2xc8ta241ef11.jpg new file mode 100644 index 0000000..fde668e Binary files /dev/null and b/src/wwwroot/imagegen/random/2xc8ta241ef11.jpg differ diff --git a/src/wwwroot/imagegen/random/3vdcuwqyvwj11.jpg b/src/wwwroot/imagegen/random/3vdcuwqyvwj11.jpg new file mode 100644 index 0000000..c9177fa Binary files /dev/null and b/src/wwwroot/imagegen/random/3vdcuwqyvwj11.jpg differ diff --git a/src/wwwroot/imagegen/random/54wr08npgwf11.gif b/src/wwwroot/imagegen/random/54wr08npgwf11.gif new file mode 100644 index 0000000..38e3584 Binary files /dev/null and b/src/wwwroot/imagegen/random/54wr08npgwf11.gif differ diff --git a/src/wwwroot/imagegen/random/56gpa81abpf11.png b/src/wwwroot/imagegen/random/56gpa81abpf11.png new file mode 100644 index 0000000..79d29d2 Binary files /dev/null and b/src/wwwroot/imagegen/random/56gpa81abpf11.png differ diff --git a/src/wwwroot/imagegen/random/5aeb56nppuk11.png b/src/wwwroot/imagegen/random/5aeb56nppuk11.png new file mode 100644 index 0000000..03e3fbe Binary files /dev/null and b/src/wwwroot/imagegen/random/5aeb56nppuk11.png differ diff --git a/src/wwwroot/imagegen/random/5spetics5kj11.jpg b/src/wwwroot/imagegen/random/5spetics5kj11.jpg new file mode 100644 index 0000000..7788abc Binary files /dev/null and b/src/wwwroot/imagegen/random/5spetics5kj11.jpg differ diff --git a/src/wwwroot/imagegen/random/5vkjjdro5vf11.gif b/src/wwwroot/imagegen/random/5vkjjdro5vf11.gif new file mode 100644 index 0000000..0cf68f9 Binary files /dev/null and b/src/wwwroot/imagegen/random/5vkjjdro5vf11.gif differ diff --git a/src/wwwroot/imagegen/random/5vlcwan2lmh11.jpg b/src/wwwroot/imagegen/random/5vlcwan2lmh11.jpg new file mode 100644 index 0000000..af1cd60 Binary files /dev/null and b/src/wwwroot/imagegen/random/5vlcwan2lmh11.jpg differ diff --git a/src/wwwroot/imagegen/random/6sfo1cna2pj11.jpg b/src/wwwroot/imagegen/random/6sfo1cna2pj11.jpg new file mode 100644 index 0000000..3ebdaac Binary files /dev/null and b/src/wwwroot/imagegen/random/6sfo1cna2pj11.jpg differ diff --git a/src/wwwroot/imagegen/random/77x7ii00zvi11.jpg b/src/wwwroot/imagegen/random/77x7ii00zvi11.jpg new file mode 100644 index 0000000..07ec128 Binary files /dev/null and b/src/wwwroot/imagegen/random/77x7ii00zvi11.jpg differ diff --git a/src/wwwroot/imagegen/random/7l5n5ujzepi11.png b/src/wwwroot/imagegen/random/7l5n5ujzepi11.png new file mode 100644 index 0000000..f35ae19 Binary files /dev/null and b/src/wwwroot/imagegen/random/7l5n5ujzepi11.png differ diff --git a/src/wwwroot/imagegen/random/857p7046ccf11.jpg b/src/wwwroot/imagegen/random/857p7046ccf11.jpg new file mode 100644 index 0000000..e648b8a Binary files /dev/null and b/src/wwwroot/imagegen/random/857p7046ccf11.jpg differ diff --git a/src/wwwroot/imagegen/random/8qa5fcz37gf11.jpg b/src/wwwroot/imagegen/random/8qa5fcz37gf11.jpg new file mode 100644 index 0000000..39b4f48 Binary files /dev/null and b/src/wwwroot/imagegen/random/8qa5fcz37gf11.jpg differ diff --git a/src/wwwroot/imagegen/random/8qmocvg1m9j11.jpg b/src/wwwroot/imagegen/random/8qmocvg1m9j11.jpg new file mode 100644 index 0000000..9c67c17 Binary files /dev/null and b/src/wwwroot/imagegen/random/8qmocvg1m9j11.jpg differ diff --git a/src/wwwroot/imagegen/random/8suq2wcsymf11.jpg b/src/wwwroot/imagegen/random/8suq2wcsymf11.jpg new file mode 100644 index 0000000..904fe87 Binary files /dev/null and b/src/wwwroot/imagegen/random/8suq2wcsymf11.jpg differ diff --git a/src/wwwroot/imagegen/random/93b0rp4612h11.jpg b/src/wwwroot/imagegen/random/93b0rp4612h11.jpg new file mode 100644 index 0000000..9c23ce2 Binary files /dev/null and b/src/wwwroot/imagegen/random/93b0rp4612h11.jpg differ diff --git a/src/wwwroot/imagegen/random/94w4fj9a1ai11.jpg b/src/wwwroot/imagegen/random/94w4fj9a1ai11.jpg new file mode 100644 index 0000000..2c34cec Binary files /dev/null and b/src/wwwroot/imagegen/random/94w4fj9a1ai11.jpg differ diff --git a/src/wwwroot/imagegen/random/9djtnidkcwf11.jpg b/src/wwwroot/imagegen/random/9djtnidkcwf11.jpg new file mode 100644 index 0000000..318e4f7 Binary files /dev/null and b/src/wwwroot/imagegen/random/9djtnidkcwf11.jpg differ diff --git a/src/wwwroot/imagegen/random/9q16us8ysij11.jpg b/src/wwwroot/imagegen/random/9q16us8ysij11.jpg new file mode 100644 index 0000000..83bc5ae Binary files /dev/null and b/src/wwwroot/imagegen/random/9q16us8ysij11.jpg differ diff --git a/src/wwwroot/imagegen/random/9sfe8wq12sh11.jpg b/src/wwwroot/imagegen/random/9sfe8wq12sh11.jpg new file mode 100644 index 0000000..3df9305 Binary files /dev/null and b/src/wwwroot/imagegen/random/9sfe8wq12sh11.jpg differ diff --git a/src/wwwroot/imagegen/random/a19wz17opyh11.jpg b/src/wwwroot/imagegen/random/a19wz17opyh11.jpg new file mode 100644 index 0000000..f6ce1f1 Binary files /dev/null and b/src/wwwroot/imagegen/random/a19wz17opyh11.jpg differ diff --git a/src/wwwroot/imagegen/random/aUdeAY5.jpg b/src/wwwroot/imagegen/random/aUdeAY5.jpg new file mode 100644 index 0000000..f31371e Binary files /dev/null and b/src/wwwroot/imagegen/random/aUdeAY5.jpg differ diff --git a/src/wwwroot/imagegen/random/aqnlxkgflvi11.jpg b/src/wwwroot/imagegen/random/aqnlxkgflvi11.jpg new file mode 100644 index 0000000..c512268 Binary files /dev/null and b/src/wwwroot/imagegen/random/aqnlxkgflvi11.jpg differ diff --git a/src/wwwroot/imagegen/random/aqx6idxqekg11.jpg b/src/wwwroot/imagegen/random/aqx6idxqekg11.jpg new file mode 100644 index 0000000..741b140 Binary files /dev/null and b/src/wwwroot/imagegen/random/aqx6idxqekg11.jpg differ diff --git a/src/wwwroot/imagegen/random/b04p5ymhr6h11.png b/src/wwwroot/imagegen/random/b04p5ymhr6h11.png new file mode 100644 index 0000000..666b8c8 Binary files /dev/null and b/src/wwwroot/imagegen/random/b04p5ymhr6h11.png differ diff --git a/src/wwwroot/imagegen/random/bzj2nywuc8j11.png b/src/wwwroot/imagegen/random/bzj2nywuc8j11.png new file mode 100644 index 0000000..4dc3bd9 Binary files /dev/null and b/src/wwwroot/imagegen/random/bzj2nywuc8j11.png differ diff --git a/src/wwwroot/imagegen/random/c429dw6y7ak11.jpg b/src/wwwroot/imagegen/random/c429dw6y7ak11.jpg new file mode 100644 index 0000000..f8a098d Binary files /dev/null and b/src/wwwroot/imagegen/random/c429dw6y7ak11.jpg differ diff --git a/src/wwwroot/imagegen/random/c7jy592gnsg11.jpg b/src/wwwroot/imagegen/random/c7jy592gnsg11.jpg new file mode 100644 index 0000000..69b75ec Binary files /dev/null and b/src/wwwroot/imagegen/random/c7jy592gnsg11.jpg differ diff --git a/src/wwwroot/imagegen/random/cx1hOwz.jpg b/src/wwwroot/imagegen/random/cx1hOwz.jpg new file mode 100644 index 0000000..4837559 Binary files /dev/null and b/src/wwwroot/imagegen/random/cx1hOwz.jpg differ diff --git a/src/wwwroot/imagegen/random/cy200joshji11.jpg b/src/wwwroot/imagegen/random/cy200joshji11.jpg new file mode 100644 index 0000000..8c82bd1 Binary files /dev/null and b/src/wwwroot/imagegen/random/cy200joshji11.jpg differ diff --git a/src/wwwroot/imagegen/random/da1ponyybhf11.jpg b/src/wwwroot/imagegen/random/da1ponyybhf11.jpg new file mode 100644 index 0000000..9b83abb Binary files /dev/null and b/src/wwwroot/imagegen/random/da1ponyybhf11.jpg differ