Skip to content

Commit

Permalink
fix: nt img pool
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 4, 2024
1 parent a5e741c commit b5c7732
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 65 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931
github.com/FloatTech/floatbox v0.0.0-20240502092157-9deaab837c04
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50
github.com/FloatTech/gg v1.1.2
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9
github.com/FloatTech/rendercard v0.0.10-0.20230223064326-45d29fa4ede9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931 h1:jTc1mb2sG1krnxpgD75SMojeOMIl6bGX5t3YGIRKkHI=
github.com/FloatTech/ZeroBot-Plugin-Webui v1.0.1-0.20230412164529-cfe878675931/go.mod h1:Se2A9vnC4eSYF7vR2LNSayV7rvbFcpgVs1ZN4sEYMyg=
github.com/FloatTech/floatbox v0.0.0-20240502092157-9deaab837c04 h1:qTlD6NN7+NMuxr4p2C4E0VUITh+ikIwLx7Z0nKCF3Kc=
github.com/FloatTech/floatbox v0.0.0-20240502092157-9deaab837c04/go.mod h1:OzGLhvmtz1TKIdGaJDd8pQumvD36UqK+dWsiCISmzQQ=
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50 h1:1D9BS5nI8UFQ8dg3QJicntfGbtL5WdZsKm/OMyBd/lM=
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50/go.mod h1:OzGLhvmtz1TKIdGaJDd8pQumvD36UqK+dWsiCISmzQQ=
github.com/FloatTech/gg v1.1.2 h1:YolgOYg3uDHc1+g0bLtt6QuRA/pvLn+b9IBCIhOOX88=
github.com/FloatTech/gg v1.1.2/go.mod h1:uzPzAeT35egARdRuu+1oyjU3CmTwCceoq3Vvje7LpcI=
github.com/FloatTech/imgfactory v0.2.2-0.20230315152233-49741fc994f9 h1:IzZLuM/fgKclyMaU/Qb1qlLdGrs2FTietkqOWhh07Gw=
Expand Down
90 changes: 45 additions & 45 deletions img/pool/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GetImage(name string) (m *Image, err error) {
}

// NewImage context name file
func NewImage(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg, name, f string) (m *Image, hassent bool, err error) {
func NewImage(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg, name, f string) (m *Image, err error) {
m = new(Image)
m.n = name
m.SetFile(f)
Expand All @@ -80,7 +80,7 @@ func NewImage(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg, name, f string)
logrus.Debugln("[imgpool] image", name, m, "outdated, updating...")
get = nil
}
hassent, err = m.Push(send, get)
err = m.Push(send, get)
return
}

Expand Down Expand Up @@ -109,69 +109,69 @@ func (m *Image) SetFile(f string) {
}

// Push context
func (m *Image) Push(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg) (hassent bool, err error) {
func (m *Image) Push(send ctxext.NoCtxSendMsg, get ctxext.NoCtxGetMsg) (err error) {
id := send(message.Message{message.Image(m.f)})
if id == 0 {
err = ErrSendImg
return
}
hassent = true
if get != nil {
msg := get(id)
for _, e := range msg.Elements {
if e.Type == "image" {
u := e.Data["url"]
if ntcachere.MatchString(u) { // is NTQQ
raw := ""
raw, err = nturl(u).pack()
if err != nil {
logrus.Errorln("[imgpool] pack nturl err:", err)
err = nil
return
}
m.item, err = newItem(m.n, raw)
if err != nil {
logrus.Errorln("[imgpool] get newItem err:", err)
err = nil
return
}
logrus.Debugln("[imgpool] 缓存:", m.n, "url:", u)
err = m.item.push("minamoto")
if err != nil {
logrus.Errorln("[imgpool] item.push err:", err)
err = nil
}
if get == nil {
return
}
msg := get(id)
for _, e := range msg.Elements {
if e.Type == "image" {
u := e.Data["url"]
if ntcachere.MatchString(u) { // is NTQQ
raw := ""
raw, err = nturl(u).pack()
if err != nil {
logrus.Errorln("[imgpool] pack nturl err:", err)
err = nil
return
}
i := strings.LastIndex(u, "/")
if i <= 0 {
break
}
u = u[:i]
i = strings.LastIndex(u, "-")
if i <= 0 {
break
}
u = u[i:]
if u == "" {
break
}
m.item, err = newItem(m.n, "0-0"+u)
m.item, err = newItem(m.n, raw)
if err != nil {
logrus.Errorln("[imgpool] get newItem err:", err)
err = nil
return
}
logrus.Debugln("[imgpool] 缓存:", m.n, "url:", "0-0"+u)
logrus.Debugln("[imgpool] 缓存:", m.n, "url:", u)
err = m.item.push("minamoto")
if err != nil {
logrus.Errorln("[imgpool] item.push err:", err)
err = nil
}
return
}
i := strings.LastIndex(u, "/")
if i <= 0 {
break
}
u = u[:i]
i = strings.LastIndex(u, "-")
if i <= 0 {
break
}
u = u[i:]
if u == "" {
break
}
m.item, err = newItem(m.n, "0-0"+u)
if err != nil {
logrus.Errorln("[imgpool] get newItem err:", err)
err = nil
return
}
logrus.Debugln("[imgpool] 缓存:", m.n, "url:", "0-0"+u)
err = m.item.push("minamoto")
if err != nil {
logrus.Errorln("[imgpool] item.push err:", err)
err = nil
}
return
}
err = ErrGetMsg
}
err = ErrGetMsg
return
}
19 changes: 2 additions & 17 deletions img/pool/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ func SendImageFromPool(imgname, imgpath string, genimg func() error, send ctxext
}
}
m.SetFile(file.BOTPATH + "/" + imgpath)
if err == ErrImgFileOutdated {
get = nil
}
hassent, err := m.Push(send, get)
if hassent {
return nil
}
if err != nil {
return err
}
return m.Push(send, get)
}
// 发送图片
img := message.Image(m.String())
Expand All @@ -55,13 +46,7 @@ func SendRemoteImageFromPool(imgname, imgurl string, send ctxext.NoCtxSendMsg, g
if err == ErrImgFileOutdated {
get = nil
}
hassent, err := m.Push(send, get)
if hassent {
return nil
}
if err != nil {
return err
}
return m.Push(send, get)
}
// 发送图片
img := message.Image(m.String())
Expand Down

0 comments on commit b5c7732

Please sign in to comment.