From c34b7270422447760745c7c32d9f5a1dc3cfaa8a 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: Sun, 5 May 2024 03:47:44 +0900 Subject: [PATCH] fix: img pool --- go.mod | 2 +- img/pool/img.go | 32 ++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index f3af95a..09b88d8 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/fumiama/cron v1.3.0 github.com/fumiama/go-base16384 v1.7.0 github.com/fumiama/go-registry v0.2.6 + github.com/fumiama/terasu v0.0.0-20240502091919-c887e26289a8 github.com/gin-gonic/gin v1.8.2 github.com/go-playground/assert/v2 v2.2.0 github.com/go-playground/validator/v10 v10.11.1 @@ -38,7 +39,6 @@ require ( github.com/fumiama/go-simple-protobuf v0.1.0 // indirect github.com/fumiama/gofastTEA v0.0.10 // indirect github.com/fumiama/imgsz v0.0.2 // indirect - github.com/fumiama/terasu v0.0.0-20240502091919-c887e26289a8 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.6 // indirect diff --git a/img/pool/img.go b/img/pool/img.go index d4aa16e..3e96f1c 100644 --- a/img/pool/img.go +++ b/img/pool/img.go @@ -10,10 +10,10 @@ import ( "regexp" "strings" + "github.com/fumiama/terasu/http2" "github.com/sirupsen/logrus" "github.com/wdvxdr1123/ZeroBot/message" - "github.com/FloatTech/floatbox/web" "github.com/FloatTech/zbputils/ctxext" ) @@ -42,14 +42,21 @@ func GetImage(name string) (m *Image, err error) { m.n = name m.item, err = getItem(name) if err == nil && m.u != "" { - _, err = web.RequestDataWithHeaders(web.NewDefaultClient(), m.String(), "HEAD", func(r *http.Request) error { - r.Header.Set("user-agent", web.RandUA()) - return nil - }, nil) + var resp *http.Response + resp, err = http2.Head(m.String()) + if err == nil { + return + } + if resp.StatusCode == http.StatusNotFound { + logrus.Debugln("[imgpool] image", name, m, "outdated:", err) + err = ErrImgFileOutdated + return + } + resp, err = http2.Get(m.String()) + _ = resp.Body.Close() if err == nil { return } - logrus.Debugln("[imgpool] image", name, m, "outdated:", err) err = ErrImgFileOutdated return } @@ -65,13 +72,18 @@ func NewImage(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg, name, f string) m.SetFile(f) m.item, err = getItem(name) if err == nil && m.item.u != "" { - _, err = web.RequestDataWithHeaders(web.NewDefaultClient(), m.String(), "HEAD", func(r *http.Request) error { - r.Header.Set("user-agent", web.RandUA()) - return nil - }, nil) + var resp *http.Response + resp, err = http2.Head(m.String()) if err == nil { return } + if resp.StatusCode != http.StatusNotFound { + resp, err = http2.Get(m.String()) + _ = resp.Body.Close() + if err == nil { + return + } + } logrus.Debugln("[imgpool] image", name, m, "outdated:", err, "updating...") } hassent, err = m.Push(send, get)