webapi: implement adding "alternative files" to messages sent with the Send method

with new field "AlternativeFiles" in the JSON body, or with "alternativefile" form file uploads.

can be used if there is a (full) alternative representation (alternative to
text and/or html part), like a calendar item, or PDF file.

for issue #188 by morki
This commit is contained in:
Mechiel Lukkien
2024-08-23 12:00:25 +02:00
parent 62bd2f4427
commit 6c488ead0b
4 changed files with 40 additions and 11 deletions

View File

@ -759,8 +759,9 @@ func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.S
xc.Header("User-Agent", "mox/"+moxvar.Version)
}
// Whether we have additional separately inline/attached file(s).
// Whether we have additional separately alternative/inline/attached file(s).
mpf := reqInfo.Request.MultipartForm
formAlternative := mpf != nil && len(mpf.File["alternativefile"]) > 0
formInline := mpf != nil && len(mpf.File["inlinefile"]) > 0
formAttachment := mpf != nil && len(mpf.File["attachedfile"]) > 0
@ -770,6 +771,7 @@ func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.S
// - multipart/alternative (in case we have both text and html bodies)
// - text/plain (optional)
// - text/html (optional)
// - alternative file, ...
// - inline file, ...
// - attached file, ...
@ -811,7 +813,7 @@ func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.S
related = xcreateMultipart("related")
cur = related
}
if m.Text != "" && m.HTML != "" {
if m.Text != "" && m.HTML != "" || len(req.AlternativeFiles) > 0 || formAlternative {
alternative = xcreateMultipart("alternative")
cur = alternative
}
@ -827,10 +829,6 @@ func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.S
_, err := tp.Write([]byte(htmlBody))
xcheckf(err, "write html part")
}
if alternative != nil {
alternative.Close()
alternative = nil
}
xaddFileBase64 := func(ct string, inline bool, filename string, cid string, base64Data string) {
h := textproto.MIMEHeader{}
@ -923,6 +921,18 @@ func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.S
xcheckf(err, "flushing uploaded file")
}
cur = alternative
xaddJSONFiles(req.AlternativeFiles, true)
if mpf != nil {
for _, fh := range mpf.File["alternativefile"] {
xaddFile(fh, true)
}
}
if alternative != nil {
alternative.Close()
alternative = nil
}
cur = related
xaddJSONFiles(req.InlineFiles, true)
if mpf != nil {