Skip to content

Commit

Permalink
Restore the Part's buffer after reading its content
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Yazdani committed Nov 2, 2017
1 parent b6ea523 commit aaadc3d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions envelope.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package enmime

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -147,6 +148,7 @@ func parseTextOnlyBody(root *Part, e *Envelope) error {

// Read transcoded text
bodyBytes, err := ioutil.ReadAll(root)
root.Utf8Reader = bytes.NewReader(bodyBytes)
if err != nil {
return err
}
Expand Down Expand Up @@ -225,6 +227,7 @@ func parseMultiPartBody(root *Part, e *Envelope) error {
if ioerr != nil {
return ioerr
}
p.Utf8Reader = bytes.NewReader(allBytes)
e.Text = string(allBytes)
}
} else {
Expand All @@ -240,6 +243,7 @@ func parseMultiPartBody(root *Part, e *Envelope) error {
if ioerr != nil {
return ioerr
}
p.Utf8Reader = bytes.NewReader(allBytes)
e.Text += string(allBytes)
}
}
Expand All @@ -251,6 +255,7 @@ func parseMultiPartBody(root *Part, e *Envelope) error {
if ioerr != nil {
return ioerr
}
p.Utf8Reader = bytes.NewReader(allBytes)
e.HTML += string(allBytes)
}

Expand Down
8 changes: 4 additions & 4 deletions part.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ type Part struct {
FileName string // The file-name from disposition or type header
Charset string // The content charset encoding label
Errors []Error // Errors encountered while parsing this part
Utf8Reader io.Reader // The decoded content converted to UTF-8

boundary string // Boundary marker used within this part
rawReader io.Reader // The raw Part content, no decoding or charset conversion
decodedReader io.Reader // The content decoded from quoted-printable or base64
utf8Reader io.Reader // The decoded content converted to UTF-8
}

// NewPart creates a new Part object. It does not update the parents FirstChild attribute.
Expand All @@ -38,10 +38,10 @@ func NewPart(parent *Part, contentType string) *Part {

// Read returns the decoded & UTF-8 converted content; implements io.Reader.
func (p *Part) Read(b []byte) (n int, err error) {
if p.utf8Reader == nil {
if p.Utf8Reader == nil {
return 0, io.EOF
}
return p.utf8Reader.Read(b)
return p.Utf8Reader.Read(b)
}

// setupContentHeaders uses Content-Type media params and Content-Disposition headers to populate
Expand Down Expand Up @@ -127,7 +127,7 @@ func (p *Part) buildContentReaders(r io.Reader) error {
}
}
}
p.utf8Reader = contentReader
p.Utf8Reader = contentReader
return nil
}

Expand Down

0 comments on commit aaadc3d

Please sign in to comment.