From b1f4253efb6728f625a3845545d45b1ee76186f7 Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 19 Nov 2021 10:22:11 -0500 Subject: [PATCH] fix: show correct file size in 'file too large' error message --- PluralKit.Bot/Services/WebhookExecutorService.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index 27990e6e..0f3e2e01 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -123,7 +123,7 @@ namespace PluralKit.Bot }; MultipartFile[] files = null; - var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit * 1024 * 1024); + var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit); if (attachmentChunks.Count > 0) { _logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks", @@ -214,11 +214,14 @@ namespace PluralKit.Bot var chunks = new List>(); var list = new List(); + // sizeThreshold is in MB (user-readable) + var bytesThreshold = sizeThreshold * 1024 * 1024; + foreach (var attachment in attachments) { - if (attachment.Size >= sizeThreshold) throw Errors.AttachmentTooLarge(sizeThreshold); + if (attachment.Size >= bytesThreshold) throw Errors.AttachmentTooLarge(sizeThreshold); - if (list.Sum(a => a.Size) + attachment.Size >= sizeThreshold) + if (list.Sum(a => a.Size) + attachment.Size >= bytesThreshold) { chunks.Add(list); list = new List();