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

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()