diff --git a/README.md b/README.md index e8b733c..d269023 100644 --- a/README.md +++ b/README.md @@ -2713,7 +2713,30 @@ the corresponding index is made available as JSON: }, "idx": 1, "isMultipart": true, - "receivedAt": "2024-07-02T11:23:31.212523916+02:00", + "multiparts": [ + { + "bodySize": 15, + "headers": { + "Content-Type": [ + "text/plain; charset=utf-8" + ] + }, + "idx": 0, + "isMultipart": false + }, + { + "bodySize": 39, + "headers": { + "Content-Type": [ + "text/html; charset=utf-8" + ] + }, + "idx": 1, + "isMultipart": false + } + ], + "multipartsCount": 2, + "receivedAt": "2024-07-02T11:44:32.767921301+02:00", "smtpFrom": "testsender2@programmfabrik.de", "smtpRcptTo": [ "testreceiver2@programmfabrik.de" @@ -2745,7 +2768,6 @@ following schema: ```json { - "count": 2, "multiparts": [ { "bodySize": 15, @@ -2754,6 +2776,7 @@ following schema: "text/plain; charset=utf-8" ] }, + "idx": 0, "isMultipart": false }, { @@ -2763,9 +2786,11 @@ following schema: "text/html; charset=utf-8" ] }, + "idx": 1, "isMultipart": false } - ] + ], + "multipartsCount": 2 } ``` diff --git a/internal/smtp/http.go b/internal/smtp/http.go index 0c37777..95b3c4f 100644 --- a/internal/smtp/http.go +++ b/internal/smtp/http.go @@ -223,33 +223,13 @@ func (h *smtpHTTPHandler) handleMultipartIndex(w http.ResponseWriter, r *http.Re multiparts = SearchByHeader(multiparts, headerSearchRgx) } - multipartsOut := make([]any, 0) - - for _, part := range multiparts { - multipartsOut = append(multipartsOut, buildContentMeta(part.Content())) - } - - out := make(map[string]any) - out["count"] = len(multiparts) - out["multiparts"] = multipartsOut - - handlerutil.RespondWithJSON(w, http.StatusOK, out) + handlerutil.RespondWithJSON(w, http.StatusOK, buildMultipartIndex(multiparts)) } func (h *smtpHTTPHandler) handleMultipartMeta( w http.ResponseWriter, r *http.Request, part *ReceivedPart, ) { - out := map[string]any{ - "idx": part.Index(), - } - - contentMeta := buildContentMeta(part.Content()) - - for k, v := range contentMeta { - out[k] = v - } - - handlerutil.RespondWithJSON(w, http.StatusOK, out) + handlerutil.RespondWithJSON(w, http.StatusOK, buildMultipartMeta(part)) } func buildContentMeta(c *ReceivedContent) map[string]any { @@ -264,6 +244,13 @@ func buildContentMeta(c *ReceivedContent) map[string]any { } out["headers"] = headers + if c.IsMultipart() { + multipartIndex := buildMultipartIndex(c.Multiparts()) + for k, v := range multipartIndex { + out[k] = v + } + } + return out } @@ -296,6 +283,34 @@ func buildMessageBasicMeta(msg *ReceivedMessage) map[string]any { return out } +func buildMultipartIndex(parts []*ReceivedPart) map[string]any { + multipartsOut := make([]any, len(parts)) + + for i, part := range parts { + multipartsOut[i] = buildMultipartMeta(part) + } + + out := make(map[string]any) + out["multipartsCount"] = len(parts) + out["multiparts"] = multipartsOut + + return out +} + +func buildMultipartMeta(part *ReceivedPart) map[string]any { + out := map[string]any{ + "idx": part.Index(), + } + + contentMeta := buildContentMeta(part.Content()) + + for k, v := range contentMeta { + out[k] = v + } + + return out +} + // extractSearchRegex tries to extract a regular expression from the referenced // query parameter. If no query parameter is given and otherwise no error has // occurred, this function returns (nil, nil).