build-base
This commit is contained in:
@@ -198,6 +198,40 @@ func (s *Store) GetRaw(ctx context.Context, userID, messageID int64) ([]byte, er
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
// BodyParts holds decoded body content returned by GetBodyParts.
|
||||
type BodyParts struct {
|
||||
Text string
|
||||
HTML string
|
||||
Attachments []AttachmentMeta
|
||||
}
|
||||
|
||||
// AttachmentMeta describes an attachment without loading its bytes.
|
||||
type AttachmentMeta struct {
|
||||
Filename string
|
||||
ContentType string
|
||||
ContentID string // for inline images
|
||||
Inline bool
|
||||
}
|
||||
|
||||
// GetBodyParts decrypts a message and returns the text/HTML body and attachment list.
|
||||
func (s *Store) GetBodyParts(ctx context.Context, userID, messageID int64) (*BodyParts, error) {
|
||||
raw, err := s.GetRaw(ctx, userID, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
text, html, atts := parseMIME(raw)
|
||||
bp := &BodyParts{Text: text, HTML: html}
|
||||
for _, a := range atts {
|
||||
bp.Attachments = append(bp.Attachments, AttachmentMeta{
|
||||
Filename: a.Filename,
|
||||
ContentType: a.ContentType,
|
||||
ContentID: a.ContentID,
|
||||
Inline: a.Inline,
|
||||
})
|
||||
}
|
||||
return bp, nil
|
||||
}
|
||||
|
||||
// parseMIME walks a raw RFC822 message and extracts body parts and attachments.
|
||||
func parseMIME(raw []byte) (bodyText, bodyHTML string, attachments []parsedAttachment) {
|
||||
m, err := mail.ReadMessage(bytes.NewReader(raw))
|
||||
|
||||
Reference in New Issue
Block a user