Skip to content

Commit

Permalink
Only call replaceCID if we have anything to replace
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Apr 3, 2024
1 parent 21c1eba commit 40d3dce
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ func (msg *message) getCID(text string) (cid string) {

// replaceCIDs replaces the CIDs found in a text string
// with generated ones
func (msg *message) replaceCIDs(text string) string {
func (msg *message) replaceCIDs(input []byte) []byte {
if len(msg.cids) == 0 {
// One process replaceCIDs if we have anything to replace
return input
}

text := string(input)

// regular expression to find cids
re := regexp.MustCompile(`(src|href)="cid:(.*?)"`)
// replace all of the found cids with generated ones
Expand All @@ -92,7 +99,7 @@ func (msg *message) replaceCIDs(text string) string {
text = strings.Replace(text, "cid:"+matches[2], "cid:"+cid, -1)
}

return text
return []byte(text)
}

// openMultipart creates a new part of a multipart message
Expand Down Expand Up @@ -211,7 +218,7 @@ func (msg *message) writeBody(body []byte, encoding encoding) {
}

func (msg *message) addBody(contentType string, body []byte) {
body = []byte(msg.replaceCIDs(string(body)))
body = msg.replaceCIDs(body)

header := make(textproto.MIMEHeader)
header.Set("Content-Type", contentType+"; charset="+msg.charset)
Expand Down

0 comments on commit 40d3dce

Please sign in to comment.