added logic for render multi img

This commit is contained in:
Pavel 2024-09-17 13:34:07 +03:00
parent 813bee5232
commit b656124ca9
2 changed files with 60 additions and 8 deletions

@ -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("<td>%s<br><img class=\"image\" style=\"width:100%%; max-width:250px; max-height:250px\" src=\"%s\"/></td>", res.Description, res.Image))
}
}
tpl := template.HTML(fmt.Sprintf("<td>%s<br><img class=\"image\" style=\"width:100%%; max-width:250px; max-height:250px\" src=\"%s\"/></td>", 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(`<a href="%s" download>%s</a>`, url, "invalid filename"))
}
filename := string(filenameBytes)
return template.HTML(fmt.Sprintf(`<a href="%s" download>%s</a>`, url, filename))
}
return template.HTML(content)

@ -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",