Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the Part's buffer after reading its content #37

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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