diff options
-rw-r--r-- | commands/msg/forward.go | 6 | ||||
-rw-r--r-- | commands/msg/recall.go | 6 | ||||
-rw-r--r-- | lib/attachment.go | 20 | ||||
-rw-r--r-- | widgets/compose.go | 47 |
4 files changed, 57 insertions, 22 deletions
diff --git a/commands/msg/forward.go b/commands/msg/forward.go index 78c24380..ccfaa086 100644 --- a/commands/msg/forward.go +++ b/commands/msg/forward.go @@ -205,8 +205,12 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error { name = fmt.Sprintf("%s_%s_%d", bs.MIMEType, bs.MIMESubType, rand.Uint64()) } mu.Lock() - composer.AddPartAttachment(name, mime, params, reader) + err := composer.AddPartAttachment(name, mime, params, reader) mu.Unlock() + if err != nil { + logging.Errorf(err.Error()) + aerc.PushError(err.Error()) + } }) } } diff --git a/commands/msg/recall.go b/commands/msg/recall.go index 866266f5..b5c92f21 100644 --- a/commands/msg/recall.go +++ b/commands/msg/recall.go @@ -211,8 +211,12 @@ func (Recall) Execute(aerc *widgets.Aerc, args []string) error { name = fmt.Sprintf("%s_%s_%d", bs.MIMEType, bs.MIMESubType, rand.Uint64()) } mu.Lock() - composer.AddPartAttachment(name, mime, params, reader) + err := composer.AddPartAttachment(name, mime, params, reader) mu.Unlock() + if err != nil { + logging.Errorf(err.Error()) + aerc.PushError(err.Error()) + } }) } }) 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 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 { |