Skip to content

Commit

Permalink
textproto: add multipart implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed May 14, 2019
1 parent 39c160d commit 604a737
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 30 deletions.
3 changes: 1 addition & 2 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package message
import (
"bufio"
"io"
"mime/multipart"
"strings"

"github.com/emersion/go-message/textproto"
Expand Down Expand Up @@ -93,7 +92,7 @@ func (e *Entity) MultipartReader() MultipartReader {
if mb, ok := e.Body.(*multipartBody); ok {
return mb
}
return &multipartReader{multipart.NewReader(e.Body, e.mediaParams["boundary"])}
return &multipartReader{textproto.NewMultipartReader(e.Body, e.mediaParams["boundary"])}
}

// writeBodyTo writes this entity's body to w (without the header).
Expand Down
21 changes: 0 additions & 21 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package message

import (
"mime"
stdtextproto "net/textproto"

"github.com/emersion/go-message/textproto"
)
Expand All @@ -26,26 +25,6 @@ func formatHeaderWithParams(f string, params map[string]string) string {
return mime.FormatMediaType(f, encParams)
}

func mapToHeader(m stdtextproto.MIMEHeader) textproto.Header {
var h textproto.Header
for k, vs := range m {
for i := len(vs) - 1; i >= 0; i-- {
h.Add(k, vs[i])
}
}
return h
}

// headerToMap converts a textproto.Header to a map. It looses information.
func headerToMap(h textproto.Header) stdtextproto.MIMEHeader {
m := make(stdtextproto.MIMEHeader)
fields := h.Fields()
for fields.Next() {
m.Add(fields.Key(), fields.Value())
}
return m
}

// A Header represents the key-value pairs in a message header.
type Header struct {
textproto.Header
Expand Down
7 changes: 4 additions & 3 deletions multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package message

import (
"io"
"mime/multipart"

"github.com/emersion/go-message/textproto"
)

// MultipartReader is an iterator over parts in a MIME multipart body.
Expand All @@ -18,7 +19,7 @@ type MultipartReader interface {
}

type multipartReader struct {
r *multipart.Reader
r *textproto.MultipartReader
}

// NextPart implements MultipartReader.
Expand All @@ -27,7 +28,7 @@ func (r *multipartReader) NextPart() (*Entity, error) {
if err != nil {
return nil, err
}
return New(Header{mapToHeader(p.Header)}, p)
return New(Header{p.Header}, p)
}

// Close implements io.Closer.
Expand Down
Loading

0 comments on commit 604a737

Please sign in to comment.