diff --git a/KfChatDotNetBot/Commands/ImageCommands.cs b/KfChatDotNetBot/Commands/ImageCommands.cs index f5e7ed3..9bee3bb 100644 --- a/KfChatDotNetBot/Commands/ImageCommands.cs +++ b/KfChatDotNetBot/Commands/ImageCommands.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Net.Http.Headers; +using System.Text.RegularExpressions; using KfChatDotNetBot.Extensions; using KfChatDotNetBot.Models; using KfChatDotNetBot.Models.DbModels; @@ -104,11 +105,13 @@ public class ListImageCommand : ICommand public UserRight RequiredRight => UserRight.TrueAndHonest; public TimeSpan Timeout => TimeSpan.FromSeconds(10); public RateLimitOptionsModel? RateLimitOptions => null; + public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) { await using var db = new ApplicationDbContext(); - var imageKeys = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.BotImageAcceptableKeys)).JsonDeserialize>(); + var imageKeys = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.BotImageAcceptableKeys)) + .JsonDeserialize>(); if (imageKeys == null) throw new InvalidOperationException($"{BuiltIn.Keys.BotImageAcceptableKeys} was null"); var key = arguments["key"].Value; if (!imageKeys.Contains(key)) @@ -119,6 +122,18 @@ public class ListImageCommand : ICommand } var images = db.Images.Where(i => i.Key == key); + if (await images.CountAsync(cancellationToken: ctx) > 20 && await Zipline.IsZiplineEnabled()) + { + var content = string.Empty; + foreach (var image in images) + { + content += image.Url + Environment.NewLine; + } + + var paste = await Zipline.Upload(content, new MediaTypeHeaderValue("text/plain"), "1d", ctx); + await botInstance.SendChatMessageAsync($"List of images for {key}: {paste}", true); + } + var i = 0; var result = $"List of images for {key}:"; foreach (var image in images) diff --git a/KfChatDotNetBot/Services/Zipline.cs b/KfChatDotNetBot/Services/Zipline.cs index 3e85875..a999f53 100644 --- a/KfChatDotNetBot/Services/Zipline.cs +++ b/KfChatDotNetBot/Services/Zipline.cs @@ -10,6 +10,26 @@ namespace KfChatDotNetBot.Services; public static class Zipline { public static async Task Upload(Stream content, MediaTypeHeaderValue mimeType, string? expiration = null, CancellationToken ct = default) + { + using var formContent = new MultipartFormDataContent(); + var fileContent = new StreamContent(content); + fileContent.Headers.ContentType = mimeType; + formContent.Add(fileContent, "upload", Money.GenerateEventId()); + var url = await DoUpload(formContent, expiration, ct); + return url; + } + + public static async Task Upload(string content, MediaTypeHeaderValue mimeType, string? expiration = null, CancellationToken ct = default) + { + using var formContent = new MultipartFormDataContent(); + var fileContent = new StringContent(content); + fileContent.Headers.ContentType = mimeType; + formContent.Add(fileContent, "upload", Money.GenerateEventId()); + var url = await DoUpload(formContent, expiration, ct); + return url; + } + + private static async Task DoUpload(MultipartFormDataContent content, string? expiration = null, CancellationToken ct = default) { var logger = LogManager.GetCurrentClassLogger(); var settings = @@ -30,17 +50,13 @@ public static class Zipline } using var client = new HttpClient(handler); - using var formContent = new MultipartFormDataContent(); - var fileContent = new StreamContent(content); - fileContent.Headers.ContentType = mimeType; - formContent.Add(fileContent, "upload", Money.GenerateEventId()); client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", settings[BuiltIn.Keys.ZiplineKey].Value); if (expiration != null) { client.DefaultRequestHeaders.Add("x-zipline-expiration", expiration); } - var response = await client.PostAsync($"{settings[BuiltIn.Keys.ZiplineUrl].Value}/api/upload", formContent, ct); + var response = await client.PostAsync($"{settings[BuiltIn.Keys.ZiplineUrl].Value}/api/upload", content, ct); var json = await response.Content.ReadFromJsonAsync(cancellationToken: ct); string url; try @@ -58,4 +74,10 @@ public static class Zipline return url; } + + public static async Task IsZiplineEnabled() + { + var key = await SettingsProvider.GetValueAsync(BuiltIn.Keys.ZiplineKey); + return !string.IsNullOrEmpty(key.Value); + } } \ No newline at end of file