From b656124ca95d16793c31d710b60678781699b7be Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 17 Sep 2024 13:34:07 +0300 Subject: [PATCH] added logic for render multi img --- senders/common.go | 36 +++++++++++++++++++++++++++++------- tests/smtp_test.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/senders/common.go b/senders/common.go index 794af3e..0377a25 100644 --- a/senders/common.go +++ b/senders/common.go @@ -3,6 +3,7 @@ package senders import ( "bytes" _ "embed" + "encoding/base64" "encoding/json" "fmt" "golang.org/x/net/html" @@ -56,20 +57,41 @@ var tmplFuncs = template.FuncMap{ } func RenderImage(content string) template.HTML { - var res model.ImageContent - err := json.Unmarshal([]byte(content), &res) - if err != nil { - return SplitContent(content) + contents := strings.Split(content, "`,`") + var builder strings.Builder + + for i, cnt := range contents { + if i == 0 { + cnt = strings.TrimPrefix(cnt, "`") + } + if i == len(contents)-1 { + cnt = strings.TrimSuffix(cnt, "`") + } + + var res model.ImageContent + err := json.Unmarshal([]byte(cnt), &res) + if err != nil { + return SplitContent(content) + } else { + builder.WriteString(fmt.Sprintf("%s
", res.Description, res.Image)) + } } - tpl := template.HTML(fmt.Sprintf("%s
", res.Description, res.Image)) - return tpl + + return template.HTML(builder.String()) } func SplitContent(content string) template.HTML { parts := strings.Split(content, "|") if len(parts) == 2 { url := strings.TrimSpace(parts[0]) - filename := strings.TrimSpace(parts[1]) + filenameBase64 := strings.TrimSpace(parts[1]) + filenameBytes, err := base64.StdEncoding.DecodeString(filenameBase64) + if err != nil { + return template.HTML(fmt.Sprintf(`%s`, url, "invalid filename")) + } + + filename := string(filenameBytes) + return template.HTML(fmt.Sprintf(`%s`, url, filename)) } return template.HTML(content) diff --git a/tests/smtp_test.go b/tests/smtp_test.go index e776c55..34044c5 100644 --- a/tests/smtp_test.go +++ b/tests/smtp_test.go @@ -3,6 +3,7 @@ package tests import ( "context" _ "embed" + "encoding/json" "fmt" "github.com/gofiber/fiber/v2" "github.com/pioz/faker" @@ -160,9 +161,36 @@ func TestProcessMessageToClient(t *testing.T) { UserID: "64f2cd7a7047f28fdabf6d9e", } + f := model.ImageContent{ + Image: "https://letsenhance.io/static/8f5e523ee6b2479e26ecc91b9c25261e/1015f/MainAfter.jpg", + Description: "Gekon", + } + + s := model.ImageContent{ + Image: "https://otvet.imgsmail.ru/download/259979446_0cec59a5be8fdd4b6f48cb11e4d79e03_800.jpg", + Description: "Born To Trall", + } + + th := model.ImageContent{ + Image: "https://miro.medium.com/v2/resize:fit:600/1*i2skbfmDsHayHhqPfwt6pA.png", + Description: "Gopher", + } + + fo := model.ImageContent{ + Image: "https://aqua-tver.su/sites/default/files/product/terra-BrachypelmaAlbopilosum.png", + Description: "Pawuk", + } + + fj, _ := json.Marshal(f) + sj, _ := json.Marshal(s) + thj, _ := json.Marshal(th) + foj, _ := json.Marshal(fo) + + fmt.Println(quizConfig) + allAnswers := []model.ResultAnswer{ { - Content: `{"Image":"https://letsenhance.io/static/8f5e523ee6b2479e26ecc91b9c25261e/1015f/MainAfter.jpg","Description":"Gekon"}`, + Content: fmt.Sprintf("`%s`,`%s`,`%s`,`%s`", fj, sj, thj, foj), AnswerID: 1, QuestionID: 1, }, @@ -173,6 +201,8 @@ func TestProcessMessageToClient(t *testing.T) { }, } + println(allAnswers[0].Content) + answerContent := model.ResultContent{ Name: "Pasha", Phone: "+723456789",