Skip to content

Commit

Permalink
Merge pull request #161 from rami-dabain/main
Browse files Browse the repository at this point in the history
Added return error to `fileFromReader`
  • Loading branch information
wneessen authored Dec 25, 2023
2 parents d5b410d + f445a55 commit 6cb8290
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
28 changes: 17 additions & 11 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,13 @@ func (m *Msg) AttachFile(n string, o ...FileOption) {
// into memory first, so it can seek through it. Using larger amounts of
// data on the io.Reader should be avoided. For such, it is recommended to
// either use AttachFile or AttachReadSeeker instead
func (m *Msg) AttachReader(n string, r io.Reader, o ...FileOption) {
f := fileFromReader(n, r)
func (m *Msg) AttachReader(n string, r io.Reader, o ...FileOption) error {
f, err := fileFromReader(n, r)
if err != nil {
return err
}
m.attachments = m.appendFile(m.attachments, f, o...)
return nil
}

// AttachReadSeeker adds an attachment File via io.ReadSeeker to the Msg
Expand Down Expand Up @@ -851,9 +855,13 @@ func (m *Msg) EmbedFile(n string, o ...FileOption) {
// into memory first, so it can seek through it. Using larger amounts of
// data on the io.Reader should be avoided. For such, it is recommended to
// either use EmbedFile or EmbedReadSeeker instead
func (m *Msg) EmbedReader(n string, r io.Reader, o ...FileOption) {
f := fileFromReader(n, r)
func (m *Msg) EmbedReader(n string, r io.Reader, o ...FileOption) error {
f, err := fileFromReader(n, r)
if err != nil {
return err
}
m.embeds = m.appendFile(m.embeds, f, o...)
return nil
}

// EmbedReadSeeker adds an embedded File from an io.ReadSeeker to the Msg
Expand Down Expand Up @@ -1216,10 +1224,10 @@ func fileFromFS(n string) *File {
}

// fileFromReader returns a File pointer from a given io.Reader
func fileFromReader(n string, r io.Reader) *File {
func fileFromReader(n string, r io.Reader) (*File, error) {
d, err := io.ReadAll(r)
if err != nil {
return &File{}
return &File{}, err
}
br := bytes.NewReader(d)
return &File{
Expand All @@ -1233,7 +1241,7 @@ func fileFromReader(n string, r io.Reader) *File {
_, cerr = br.Seek(0, io.SeekStart)
return rb, cerr
},
}
}, nil
}

// fileFromReadSeeker returns a File pointer from a given io.ReadSeeker
Expand Down Expand Up @@ -1261,8 +1269,7 @@ func fileFromHTMLTemplate(n string, t *ht.Template, d interface{}) (*File, error
if err := t.Execute(&buf, d); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
f := fileFromReader(n, &buf)
return f, nil
return fileFromReader(n, &buf)
}

// fileFromTextTemplate returns a File pointer form a given text/template.Template
Expand All @@ -1274,8 +1281,7 @@ func fileFromTextTemplate(n string, t *tt.Template, d interface{}) (*File, error
if err := t.Execute(&buf, d); err != nil {
return nil, fmt.Errorf(errTplExecuteFailed, err)
}
f := fileFromReader(n, &buf)
return f, nil
return fileFromReader(n, &buf)
}

// getEncoder creates a new mime.WordEncoder based on the encoding setting of the message
Expand Down
20 changes: 16 additions & 4 deletions msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,10 @@ func TestMsg_AttachReader(t *testing.T) {
rbuf := bytes.Buffer{}
rbuf.WriteString(ts)
r := bufio.NewReader(&rbuf)
m.AttachReader("testfile.txt", r)
if err := m.AttachReader("testfile.txt", r); err != nil {
t.Errorf("AttachReader() failed. Expected no error, got: %s", err.Error())
return
}
if len(m.attachments) != 1 {
t.Errorf("AttachReader() failed. Number of attachments expected: %d, got: %d", 1,
len(m.attachments))
Expand Down Expand Up @@ -1845,7 +1848,10 @@ func TestMsg_EmbedReader(t *testing.T) {
rbuf := bytes.Buffer{}
rbuf.WriteString(ts)
r := bufio.NewReader(&rbuf)
m.EmbedReader("testfile.txt", r)
if err := m.EmbedReader("testfile.txt", r); err != nil {
t.Errorf("EmbedReader() failed. Expected no error, got: %s", err.Error())
return
}
if len(m.embeds) != 1 {
t.Errorf("EmbedReader() failed. Number of embeds expected: %d, got: %d", 1,
len(m.embeds))
Expand Down Expand Up @@ -2847,8 +2853,14 @@ func TestMsg_AttachEmbedReader_consecutive(t *testing.T) {
ts1 := "This is a test string"
ts2 := "Another test string"
m := NewMsg()
m.AttachReader("attachment.txt", bytes.NewBufferString(ts1))
m.EmbedReader("embedded.txt", bytes.NewBufferString(ts2))
if err := m.AttachReader("attachment.txt", bytes.NewBufferString(ts1)); err != nil {
t.Errorf("AttachReader() failed. Expected no error, got: %s", err.Error())
return
}
if err := m.EmbedReader("embedded.txt", bytes.NewBufferString(ts2)); err != nil {
t.Errorf("EmbedReader() failed. Expected no error, got: %s", err.Error())
return
}
obuf1 := &bytes.Buffer{}
obuf2 := &bytes.Buffer{}
_, err := m.WriteTo(obuf1)
Expand Down

0 comments on commit 6cb8290

Please sign in to comment.