fix: 图片缓存池

This commit is contained in:
fumiama 2022-02-26 23:20:08 +08:00
parent 88318a7151
commit cf52997279
5 changed files with 23 additions and 33 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.17
require (
github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220224052148-f7a1379d4a00
github.com/FloatTech/sqlite v0.2.0
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220225052222-7539b0dd28e8
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220226150734-48567d2365af
github.com/antchfx/htmlquery v1.2.4
github.com/corona10/goimagehash v1.0.3
github.com/fogleman/gg v1.3.0

4
go.sum
View File

@ -4,8 +4,8 @@ github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9
github.com/FloatTech/sqlite v0.2.0 h1:x3uls/hExXH1+bbaNLkvilce6ATtWlDx4IqoxBW/bv8=
github.com/FloatTech/sqlite v0.2.0/go.mod h1:xIDWIvpOFl8AXmZm0FC8t3PZjiR6ZutytCpBv2EWCns=
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220224051618-597cdb58b3fe/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220225052222-7539b0dd28e8 h1:/sfOwdrK5Q4OuDuEpu8dDkUemIqR+TsjD9kNv3OxF7M=
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220225052222-7539b0dd28e8/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220226150734-48567d2365af h1:nwXPR9+RuTbjk0zh+4Sg37wWaqfQb8kGvSfPA1wsF+0=
github.com/FloatTech/zbputils v1.3.0-beta8.0.20220226150734-48567d2365af/go.mod h1:Jn5LmnwqYivr64qQJBdhDwm2gRut8Hhq5gPBydV0Gvg=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=

View File

@ -26,7 +26,7 @@ const (
)
var (
queue = make(chan [2]string, capacity)
queue = make(chan string, capacity)
)
func init() {
@ -58,28 +58,28 @@ func init() {
url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re")
name := url[strings.LastIndex(url, "/")+1 : len(url)-4]
m, err := pool.GetImage(name)
if err != nil {
if err != nil && err != pool.ErrImgFileAsync {
m.SetFile(url)
_, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
process.SleepAbout1sTo2s()
}
if err == nil {
queue <- [2]string{name, m.String()}
queue <- m.String()
} else {
queue <- [2]string{name, url}
queue <- url
}
}
}()
select {
case <-time.After(time.Minute):
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
case o := <-queue:
name := o[0]
url := o[1]
err := pool.SendRemoteImageFromPool(name, url, ctxext.Send(ctx), ctxext.GetMessage(ctx))
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
case img := <-queue:
id := ctx.SendChain(message.Image(img))
if id.ID() == 0 {
id = ctx.SendChain(message.Image(img).Add("cache", "0"))
if id.ID() == 0 {
ctx.SendChain(message.Text("ERROR:图片发送失败,可能被风控了~"))
}
}
}
})

View File

@ -49,16 +49,12 @@ func init() { // 插件主体
if file.IsNotExist(f) {
m, err = pool.GetImage(n)
if err == nil {
err = file.DownloadTo(m.String(), f, true)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
break
imgs = append(imgs, message.Image(m.String()))
continue
}
logrus.Debugln("[sausenao]开始下载", n)
err = illust.DownloadToCache(i)
if err == nil {
err1 := illust.DownloadToCache(i)
if err != pool.ErrImgFileAsync && err1 == nil {
m.SetFile(f)
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
}

View File

@ -159,25 +159,19 @@ func (p *imgpool) push(ctx *zero.Ctx, imgtype string, illust *pixiv.Illust) {
f := fileutil.BOTPATH + "/" + illust.Path(0)
if err != nil {
// 下载图片
if err = illust.DownloadToCache(0); err != nil {
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))
if err != imagepool.ErrImgFileAsync {
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 ctxext.SendToSelf(ctx)(msg) == 0 {
err = fileutil.DownloadTo(m.String(), f, true)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
msg = message.Image("file:///" + f)
}
}
}
p.poolmu.Lock()