diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-10-23 21:27:09 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-09 21:14:33 +0100 |
commit | f479ae8c6e550dade0f183da9d3d7760f406d806 (patch) | |
tree | 6e956d10ee77d9cca6b943677af9d5b7161e0560 /lib | |
parent | 3e52278e86d07192a21a2624ccbabdc1d3ea5ad6 (diff) | |
download | aerc-f479ae8c6e550dade0f183da9d3d7760f406d806.tar.gz |
lib: prepare attachments for multiple reads
Prepare attachments for multiple reads. The data for lib.PartAttachment
is stored as an io.Reader which can only be read once. This will cause
an issue when we want to call composer.WriteMessage multiple times, i.e.
for a message preview. We fix this by keeping a copy of the data and
create a new reader everytime the attachment is read.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/attachment.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/attachment.go b/lib/attachment.go index 9246315d..bc29a567 100644 --- a/lib/attachment.go +++ b/lib/attachment.go @@ -2,6 +2,7 @@ package lib import ( "bufio" + "bytes" "io" "mime" "net/http" @@ -17,15 +18,24 @@ import ( type Part struct { MimeType string Params map[string]string - Body io.Reader + Data []byte } -func NewPart(mimetype string, params map[string]string, body io.Reader) *Part { +func NewPart(mimetype string, params map[string]string, body io.Reader, +) (*Part, error) { + d, err := io.ReadAll(body) + if err != nil { + return nil, err + } return &Part{ MimeType: mimetype, Params: params, - Body: body, - } + Data: d, + }, nil +} + +func (p *Part) NewReader() io.Reader { + return bytes.NewReader(p.Data) } type Attachment interface { @@ -131,7 +141,7 @@ func (pa *PartAttachment) WriteTo(w *mail.Writer) error { } defer aw.Close() - if _, err := io.Copy(aw, pa.part.Body); err != nil { + if _, err := io.Copy(aw, pa.part.NewReader()); err != nil { return errors.Wrap(err, "io.Copy") } return nil |