mailparser icon indicating copy to clipboard operation
mailparser copied to clipboard

Attachment size undefined

Open zw3428 opened this issue 4 years ago • 2 comments

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.

2191607930277_ pic

zw3428 avatar Dec 14 '20 07:12 zw3428

Also same with checksum

pyummm avatar Jan 20 '21 23:01 pyummm

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.

nickzelei avatar Mar 04 '21 17:03 nickzelei