mailparser
mailparser copied to clipboard
Attachment size undefined
I use mailparser to fetch attachments in emails, but the size is 'undefined', how can I get the correct size of the attachment?
Here is the code, I can get the 'filename' and 'contentType', but the 'size' is undefined.

Also same with checksum
I took a peek at the code. I believe this is because the attachment.content is a stream.
It is an instance of the StreamHash class, which is a Transform stream type. As the content stream is read, the _transform method incrementally calculates the hash as well as accumulates the byteCount. The checksum and size are not calculated until the _flush method is called.
So effectively, these properties are calculated and in their resting states once the content stream has been exhausted.
The tests don't catch this because they don't verify the length until after the test has exhausted the stream.
This is problematic for various reasons. In my case: I need the content length ahead of time in order to pipe the stream to a remote storage location, which requires the content length header is set. This isn't possible right now without either buffering the contents in memory, or reading it to /tmp, which also isn't ideal.