fix: show correct file size in 'file too large' error message

This commit is contained in:
spiral 2021-11-19 10:22:11 -05:00
parent 85c095a115
commit b1f4253efb
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31

View File

@ -123,7 +123,7 @@ namespace PluralKit.Bot
}; };
MultipartFile[] files = null; MultipartFile[] files = null;
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit * 1024 * 1024); var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, req.FileSizeLimit);
if (attachmentChunks.Count > 0) if (attachmentChunks.Count > 0)
{ {
_logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks", _logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks",
@ -214,11 +214,14 @@ namespace PluralKit.Bot
var chunks = new List<List<Message.Attachment>>(); var chunks = new List<List<Message.Attachment>>();
var list = new List<Message.Attachment>(); var list = new List<Message.Attachment>();
// sizeThreshold is in MB (user-readable)
var bytesThreshold = sizeThreshold * 1024 * 1024;
foreach (var attachment in attachments) 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); chunks.Add(list);
list = new List<Message.Attachment>(); list = new List<Message.Attachment>();