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 /widgets | |
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 'widgets')
-rw-r--r-- | widgets/compose.go | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index 28ce18d3..c3f396bd 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -305,13 +305,17 @@ func (c *Composer) SetAttachKey(attach bool) error { return err } + newPart, err := lib.NewPart( + "application/pgp-keys", + map[string]string{"charset": "UTF-8"}, + r, + ) + if err != nil { + return err + } c.attachments = append(c.attachments, lib.NewPartAttachment( - lib.NewPart( - "application/pgp-keys", - map[string]string{"charset": "UTF-8"}, - r, - ), + newPart, c.crypto.signKey+".asc", ), ) @@ -459,7 +463,11 @@ func (c *Composer) AppendPart(mimetype string, params map[string]string, body io if !strings.HasPrefix(mimetype, "text") { return fmt.Errorf("can only append text mimetypes") } - c.textParts = append(c.textParts, lib.NewPart(mimetype, params, body)) + newPart, err := lib.NewPart(mimetype, params, body) + if err != nil { + return err + } + c.textParts = append(c.textParts, newPart) c.resetReview() return nil } @@ -814,13 +822,15 @@ func writeMsgImpl(c *Composer, header *mail.Header, writer io.Writer) error { if err != nil { return errors.Wrap(err, "CreateWriter") } - parts := []*lib.Part{ - lib.NewPart( - "text/plain", - map[string]string{"Charset": "UTF-8"}, - c.email, - ), + newPart, err := lib.NewPart( + "text/plain", + map[string]string{"Charset": "UTF-8"}, + c.email, + ) + if err != nil { + return err } + parts := []*lib.Part{newPart} if err := writeMultipartBody(append(parts, c.textParts...), w); err != nil { return errors.Wrap(err, "writeMultipartBody") } @@ -863,7 +873,7 @@ func writeMultipartBody(parts []*lib.Part, w *mail.Writer) error { return errors.Wrap(err, "CreatePart") } defer bw.Close() - if _, err := io.Copy(bw, part.Body); err != nil { + if _, err := io.Copy(bw, part.NewReader()); err != nil { return errors.Wrap(err, "io.Copy") } } @@ -884,11 +894,18 @@ func (c *Composer) AddAttachment(path string) { c.resetReview() } -func (c *Composer) AddPartAttachment(name string, mimetype string, params map[string]string, body io.Reader) { +func (c *Composer) AddPartAttachment(name string, mimetype string, + params map[string]string, body io.Reader, +) error { + p, err := lib.NewPart(mimetype, params, body) + if err != nil { + return err + } c.attachments = append(c.attachments, lib.NewPartAttachment( - lib.NewPart(mimetype, params, body), name, + p, name, )) c.resetReview() + return nil } func (c *Composer) DeleteAttachment(name string) error { |