From 1971e2274a0d5b6798a623e27ffe91721d1e2768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 2 May 2024 22:35:04 +0900 Subject: [PATCH] fix(emojimix): connection issue in some regions --- plugin/emojimix/mix.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/plugin/emojimix/mix.go b/plugin/emojimix/mix.go index 4cd532f17c..d2338e05c4 100644 --- a/plugin/emojimix/mix.go +++ b/plugin/emojimix/mix.go @@ -2,10 +2,14 @@ package emojimix import ( + "bytes" + "encoding/base64" "fmt" + "io" "net/http" "strconv" + "github.com/FloatTech/floatbox/binary" ctrl "github.com/FloatTech/zbpctrl" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" @@ -31,21 +35,29 @@ func init() { u2 := fmt.Sprintf(bed, emojis[r2], r2, r2, r1) logrus.Debugln("[emojimix] u1:", u1) logrus.Debugln("[emojimix] u2:", u2) - resp1, err := http2.Head(u1) - if err == nil { - resp1.Body.Close() - if resp1.StatusCode == http.StatusOK { - ctx.SendChain(message.Image(u1)) + buf := bytes.NewBuffer(make([]byte, 0, 65536)) + buf.WriteString("base64://") + resp, err := http2.Get(u1) + sendandclose := func(resp *http.Response) { + enc := base64.NewEncoder(base64.StdEncoding, buf) + _, err = io.Copy(enc, resp.Body) + if err != nil { return } + _ = enc.Close() + _ = resp.Body.Close() + ctx.SendChain(message.Image(binary.BytesToString(buf.Bytes()))) } - resp2, err := http2.Head(u2) if err == nil { - resp2.Body.Close() - if resp2.StatusCode == http.StatusOK { - ctx.SendChain(message.Image(u2)) - return - } + sendandclose(resp) + return + } + buf.Reset() + buf.WriteString("base64://") + resp, err = http2.Head(u2) + if err == nil { + sendandclose(resp) + return } }) }