javalin / javalin

A simple and modern Java and Kotlin web framework

Home Page:https://javalin.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How to check the size of a file that's uploading and reject it if it exceeds a specifc size on a per URL basis?

CorgiTaco opened this issue · comments

Hi there!

I am trying to figure out the best way to reject overly large files on a per URL basis instead of utilizing the global config.jetty.multipartConfig values. Ideally, I'd like to reject the connection before the upload begins to avoid exhausting the connection.

For example, I have a designated link for uploading profile pictures and another for uploading profile banners where the max file size is 5 MB. But other URLs may expect 10+ mb files.

Thank you for your time and for making this amazing framework!

Hi @CorgiTaco - you can check the content length:

ctx.req().contentLength > YOUR_MAX_SIZE

Then throw a ContentTooLargeResponse :)

Would I call that in .before or .post?

And one other question too, is it possible to check the file extension of the uploading file?

Yes, UploadedFile#extension will give you that.