Trying to use form field and file upload directive together in Akka-Http?

Table of contents
Reading Time: 2 minutes

If you are reading this then probably you have encountered the issue that comes while accessing fields with uploading a file, and if not then let me first tell you what’s the situation that I’m talking about here,

Let’s see the code where we try to use fileUpload directive and formFields together

So, the above code seems fine right? and you are wondering what is the problem here?
The problem is that if you’ll try to use it, it will throw an exception –
Substream Source cannot be materialized more than once, as we are trying to unmarshal the entity two times.

Note:  It can work to upload a small file (<1mb) if the entity is small enough that it fits in the internal buffer.

I encountered this issue while working on some project as fortunately I tested my code with a large file and found the issue before it became big.

Some workarounds over this that I found are –

Use toStrict
We can use toStrict to make sure that entire entity is buffered in memory, as otherwise it will be drained by the first unmarshalling process and not available for the second.

Custom Implementation to extract file and form fields

In above code, I’m explicitly storing the file in memory, but here we can do any processing according to our use case like If I want to upload that stream directly to S3 rather than storing it to memory.
Using the above mechanism we are directly accessing fields, instead of using any directive.

So, Both of the above solutions can be used depending on the use case that you encountered.

I hope this was helpful to those who were stuck in the similar situation, and I was able to provide them with some input so that they can proceed further.