Refactor EML parsing into a single function

The previous separate parsing of EML headers and body parts has been refactored into a single function, parseEML. This change simplifies the operations in the readEML and makes the code cleaner by reducing repetition.
This commit is contained in:
Winni Neessen 2024-06-24 14:30:07 +02:00
parent 9fa8644a9a
commit 0cec8a28f7
Signed by: wneessen
GPG key ID: 385AC9889632126E

26
eml.go
View file

@ -39,11 +39,8 @@ func EMLToMsgFromReader(reader io.Reader) (*Msg, error) {
return msg, fmt.Errorf("failed to parse EML from reader: %w", err) return msg, fmt.Errorf("failed to parse EML from reader: %w", err)
} }
if err = parseEMLHeaders(&parsedMsg.Header, msg); err != nil { if err := parseEML(parsedMsg, bodybuf, msg); err != nil {
return msg, fmt.Errorf("failed to parse EML headers: %w", err) return msg, fmt.Errorf("failed to parse EML contents: %w", err)
}
if err = parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
return msg, fmt.Errorf("failed to parse EML body parts: %w", err)
} }
return msg, nil return msg, nil
@ -64,17 +61,24 @@ func EMLToMsgFromFile(filePath string) (*Msg, error) {
return msg, fmt.Errorf("failed to parse EML file: %w", err) return msg, fmt.Errorf("failed to parse EML file: %w", err)
} }
if err = parseEMLHeaders(&parsedMsg.Header, msg); err != nil { if err := parseEML(parsedMsg, bodybuf, msg); err != nil {
return msg, fmt.Errorf("failed to parse EML headers: %w", err) return msg, fmt.Errorf("failed to parse EML contents: %w", err)
} }
if err = parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
return msg, fmt.Errorf("failed to parse EML body parts: %w", err)
}
//fmt.Printf("FOO: %+v\n", msg)
return msg, nil return msg, nil
} }
// parseEML parses the EML's headers and body and inserts the parsed values into the Msg
func parseEML(parsedMsg *netmail.Message, bodybuf *bytes.Buffer, msg *Msg) error {
if err := parseEMLHeaders(&parsedMsg.Header, msg); err != nil {
return fmt.Errorf("failed to parse EML headers: %w", err)
}
if err := parseEMLBodyParts(parsedMsg, bodybuf, msg); err != nil {
return fmt.Errorf("failed to parse EML body parts: %w", err)
}
return nil
}
// readEML opens an EML file and uses net/mail to parse the header and body // readEML opens an EML file and uses net/mail to parse the header and body
func readEML(filePath string) (*netmail.Message, *bytes.Buffer, error) { func readEML(filePath string) (*netmail.Message, *bytes.Buffer, error) {
fileHandle, err := os.Open(filePath) fileHandle, err := os.Open(filePath)