Skip to content

Commit

Permalink
optimize(img): drop remote image pool
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 13, 2024
1 parent ef3fa92 commit 8bcab9b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 69 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/FloatTech/sqlite v1.6.3
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
github.com/antchfx/htmlquery v1.3.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ=
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo=
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8 h1:XlOrX7WJqIQVBziB+ZXXzp/OG0YQICO5IC6T9s4iohE=
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8/go.mod h1:2ZS6vZHn/imRIOhlDytd/FPVdBIY5KYdTPTIwBJU9Js=
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08 h1:fwVPkvJ5dR0/GisRomaDHEJ5whQBu6dSLQJpAP1gbwA=
github.com/FloatTech/zbputils v1.7.2-0.20241013175349-67b3df244b08/go.mod h1:J/uDSgIGr8exK1B49XebRKAKphgg/Gpfh1bpjb6t6GY=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
Expand Down
4 changes: 2 additions & 2 deletions plugin/fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func init() {
digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text))
cachefile := cache + hex.EncodeToString(digest[:])

err = pool.SendImageFromPool(cachefile, cachefile, func() error {
err = pool.SendImageFromPool(cachefile, func(cachefile string) error {
f, err := os.Create(cachefile)
if err != nil {
return err
}
_, err = draw(background, fontdata, title, text, f)
_ = f.Close()
return err
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
}, ctxext.Send(ctx))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down
14 changes: 1 addition & 13 deletions plugin/lolicon/lolicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
imagepool "github.com/FloatTech/zbputils/img/pool"
)

const (
Expand Down Expand Up @@ -68,18 +67,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
continue
}
name := imageurl[strings.LastIndex(imageurl, "/")+1 : len(imageurl)-4]
m, err := imagepool.GetImage(name)
if err != nil {
m.SetFile(imageurl)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
process.SleepAbout1sTo2s()
}
if err == nil {
queue <- m.String()
} else {
queue <- imageurl
}
queue <- imageurl
}
}()
select {
Expand Down
11 changes: 0 additions & 11 deletions plugin/saucenao/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/pool"
)

const (
Expand Down Expand Up @@ -73,20 +72,10 @@ func init() { // 插件主体
for i := range illust.ImageUrls {
f := file.BOTPATH + "/" + illust.Path(i)
n := name + "_p" + strconv.Itoa(i)
var m *pool.Image
if file.IsNotExist(f) {
m, err = pool.GetImage(n)
if err == nil {
imgs = append(imgs, message.Image(m.String()))
continue
}
logrus.Debugln("[saucenao]开始下载", n)
logrus.Debugln("[saucenao]urls:", illust.ImageUrls)
err1 := illust.DownloadToCache(i)
if err1 == nil {
m.SetFile(f)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
}
if err1 != nil {
logrus.Debugln("[saucenao]下载err:", err1)
}
Expand Down
29 changes: 8 additions & 21 deletions plugin/setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -158,28 +157,16 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
if len(illust.ImageUrls) == 0 {
return
}
u := illust.ImageUrls[0]
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
m, err := imagepool.GetImage(n)
var msg message.MessageSegment
f := fileutil.BOTPATH + "/" + illust.Path(0)
if err != nil {
if fileutil.IsNotExist(f) {
// 下载图片
if err := illust.DownloadToCache(0); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
}
m.SetFile(f)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
msg = message.Image("file:///" + f)
} else {
msg = message.Image(m.String())
if ctxext.SendToSelf(ctx)(msg) == 0 {
msg = msg.Add("cache", "0")
if fileutil.IsNotExist(f) {
// 下载图片
if err := illust.DownloadToCache(0); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
}
msg = message.Image("file:///" + f)
p.poolmu.Lock()
p.pool[imgtype] = append(p.pool[imgtype], &msg)
p.poolmu.Unlock()
Expand Down Expand Up @@ -229,9 +216,9 @@ func (p *imgpool) add(ctx *zero.Ctx, imgtype string, id int64) error {
if len(illust.ImageUrls) == 0 {
return errors.New("nil image url")
}
err = imagepool.SendImageFromPool(strconv.FormatInt(illust.Pid, 10)+"_p0", illust.Path(0), func() error {
err = imagepool.SendImageFromPool(illust.Path(0), func(string) error {
return illust.DownloadToCache(0)
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
}, ctxext.Send(ctx))
if err != nil {
return err
}
Expand Down
28 changes: 9 additions & 19 deletions plugin/tarot/tarot.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ func init() {
imgname = name
}
imgpath := cache + "/" + imgname + ".png"
err := pool.SendImageFromPool("pool"+imgname, imgpath, func() error {
err := pool.SendImageFromPool(imgpath, func(pth string) error {
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
if err != nil {
return err
}
f, err := os.Create(imgpath)
f, err := os.Create(pth)
if err != nil {
return err
}
defer f.Close()
return os.WriteFile(f.Name(), data, 0755)
}, ctxext.Send(ctx), ctxext.GetMessage(ctx))
}, ctxext.Send(ctx))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -190,9 +190,9 @@ func init() {
var imgmsg message.MessageSegment
var err error
if p == 1 {
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(ctx, imgurl, name, cache)
imgmsg, err = poolimg(imgurl, name, cache)
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
Expand All @@ -213,7 +213,7 @@ func init() {
if ok {
imgurl := bed + info.ImgURL
var tarotmsg message.Message
imgmsg, err := poolimg(ctx, imgurl, match, cache)
imgmsg, err := poolimg(imgurl, match, cache)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -284,9 +284,9 @@ func init() {
var imgmsg message.MessageSegment
var err error
if p == 1 {
imgmsg, err = poolimg(ctx, imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(ctx, imgurl, name, cache)
imgmsg, err = poolimg(imgurl, name, cache)
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
Expand Down Expand Up @@ -319,17 +319,9 @@ func init() {
})
}

func poolimg(ctx *zero.Ctx, imgurl, imgname, cache string) (msg message.MessageSegment, err error) {
func poolimg(imgurl, imgname, cache string) (msg message.MessageSegment, err error) {
imgfile := cache + "/" + imgname + ".png"
aimgfile := file.BOTPATH + "/" + imgfile
m, err := pool.GetImage("pool" + imgname)
if err == nil {
msg = message.Image(m.String())
if ctxext.SendToSelf(ctx)(msg) == 0 {
msg = msg.Add("cache", "0")
}
return
}
if file.IsNotExist(aimgfile) {
var data []byte
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
Expand All @@ -347,8 +339,6 @@ func poolimg(ctx *zero.Ctx, imgurl, imgname, cache string) (msg message.MessageS
return
}
}
m.SetFile(aimgfile)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
msg = message.Image("file:///" + aimgfile)
return
}

0 comments on commit 8bcab9b

Please sign in to comment.