reordering for better structure

This commit is contained in:
Alexander Weinhold 2017-08-16 18:17:26 +02:00
parent 036990f946
commit 89642c258e

View File

@ -89,6 +89,15 @@ func decodeSubject(subject string) (string, error) {
return dec.DecodeHeader(subject) return dec.DecodeHeader(subject)
} }
func attachmentName(contentDisposition string) string {
_, params, err := mime.ParseMediaType(contentDisposition)
if filename, ok := params["filename"]; err == nil && ok {
return filename
} else {
return ""
}
}
func getPlainParts(mail *mail.Message) ([]string, []error) { func getPlainParts(mail *mail.Message) ([]string, []error) {
parts := make([]string, 0) parts := make([]string, 0)
errs := make([]error, 0) errs := make([]error, 0)
@ -104,8 +113,13 @@ func getPlainParts(mail *mail.Message) ([]string, []error) {
if err != nil { if err != nil {
errs = append(errs, err) errs = append(errs, err)
} }
if contentType := p.Header.Get("Content-Type"); strings.HasPrefix(contentType, "text/plain") { if contentType := p.Header.Get("Content-Type"); !strings.HasPrefix(contentType, "text/plain") {
if contentDisposition := p.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") { parts = append(parts, "Non-plain part: "+contentType)
} else if contentDisposition := p.Header.Get("Content-Disposition"); strings.HasPrefix(contentDisposition, "attachment") {
if filename := attachmentName(contentDisposition); filename != "" {
parts = append(parts, "Plain attachment: "+filename)
}
} else {
body, _ := ioutil.ReadAll(p) body, _ := ioutil.ReadAll(p)
encoding := p.Header.Get("Content-Transfer-Encoding") encoding := p.Header.Get("Content-Transfer-Encoding")
if encoding != "" { if encoding != "" {
@ -116,14 +130,6 @@ func getPlainParts(mail *mail.Message) ([]string, []error) {
} }
} }
parts = append(parts, string(body)) parts = append(parts, string(body))
} else {
_, params, errDispo := mime.ParseMediaType(contentDisposition)
if filename, ok := params["filename"]; errDispo == nil && ok {
parts = append(parts, "Plain attachment: "+filename)
}
}
} else {
parts = append(parts, "Non-plain part: "+contentType)
} }
} }
} else { } else {