mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 03:53:50 +08:00
feat: read cache with disk btree backend
This commit is contained in:
parent
7c4be95c19
commit
ce6b65ddb5
@ -17,16 +17,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Mrs4s/go-cqhttp/db"
|
|
||||||
|
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/message"
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
"github.com/Mrs4s/MiraiGo/utils"
|
"github.com/Mrs4s/MiraiGo/utils"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
|
"github.com/Mrs4s/go-cqhttp/db"
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/base"
|
"github.com/Mrs4s/go-cqhttp/internal/base"
|
||||||
|
"github.com/Mrs4s/go-cqhttp/internal/cache"
|
||||||
"github.com/Mrs4s/go-cqhttp/internal/param"
|
"github.com/Mrs4s/go-cqhttp/internal/param"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -1281,23 +1281,31 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
|||||||
}
|
}
|
||||||
rawPath := path.Join(global.ImagePath, f)
|
rawPath := path.Join(global.ImagePath, f)
|
||||||
if video {
|
if video {
|
||||||
|
if strings.HasSuffix(f, ".video") && cache.EnableCacheDB {
|
||||||
|
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".video"))
|
||||||
|
if err == nil {
|
||||||
|
if b := cache.Video.Get(hash); b != nil {
|
||||||
|
return bot.readVideoCache(b), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
rawPath = path.Join(global.VideoPath, f)
|
rawPath = path.Join(global.VideoPath, f)
|
||||||
if !global.PathExists(rawPath) {
|
if !global.PathExists(rawPath) {
|
||||||
return nil, errors.New("invalid video")
|
return nil, errors.New("invalid video")
|
||||||
}
|
}
|
||||||
if path.Ext(rawPath) == ".video" {
|
if path.Ext(rawPath) != ".video" {
|
||||||
b, _ := os.ReadFile(rawPath)
|
return &LocalVideoElement{File: rawPath}, nil
|
||||||
r := binary.NewReader(b)
|
}
|
||||||
return &message.ShortVideoElement{ // todo 检查缓存是否有效
|
b, _ := os.ReadFile(rawPath)
|
||||||
Md5: r.ReadBytes(16),
|
return bot.readVideoCache(b), nil
|
||||||
ThumbMd5: r.ReadBytes(16),
|
}
|
||||||
Size: r.ReadInt32(),
|
if strings.HasSuffix(f, ".image") && cache.EnableCacheDB {
|
||||||
ThumbSize: r.ReadInt32(),
|
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".image"))
|
||||||
Name: r.ReadString(),
|
if err == nil {
|
||||||
Uuid: r.ReadAvailable(),
|
if b := cache.Image.Get(hash); b != nil {
|
||||||
}, nil
|
return bot.readImageCache(b, group)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &LocalVideoElement{File: rawPath}, nil
|
|
||||||
}
|
}
|
||||||
exist := global.PathExists(rawPath)
|
exist := global.PathExists(rawPath)
|
||||||
if !exist && global.PathExists(path.Join(global.ImagePathOld, f)) {
|
if !exist && global.PathExists(path.Join(global.ImagePathOld, f)) {
|
||||||
@ -1317,8 +1325,13 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return bot.readImageCache(b, group)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) readImageCache(b []byte, group bool) (message.IMessageElement, error) {
|
||||||
|
var err error
|
||||||
if len(b) < 20 {
|
if len(b) < 20 {
|
||||||
return nil, errors.New("invalid local file")
|
return nil, errors.New("invalid cache")
|
||||||
}
|
}
|
||||||
r := binary.NewReader(b)
|
r := binary.NewReader(b)
|
||||||
hash := r.ReadBytes(16)
|
hash := r.ReadBytes(16)
|
||||||
@ -1350,6 +1363,18 @@ ok:
|
|||||||
return rsp, nil
|
return rsp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) readVideoCache(b []byte) message.IMessageElement {
|
||||||
|
r := binary.NewReader(b)
|
||||||
|
return &message.ShortVideoElement{ // todo 检查缓存是否有效
|
||||||
|
Md5: r.ReadBytes(16),
|
||||||
|
ThumbMd5: r.ReadBytes(16),
|
||||||
|
Size: r.ReadInt32(),
|
||||||
|
ThumbSize: r.ReadInt32(),
|
||||||
|
Name: r.ReadString(),
|
||||||
|
Uuid: r.ReadAvailable(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// makeShowPic 一种xml 方式发送的群消息图片
|
// makeShowPic 一种xml 方式发送的群消息图片
|
||||||
func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, brief string, icon string, minWidth int64, minHeight int64, maxWidth int64, maxHeight int64, group bool) ([]message.IMessageElement, error) {
|
func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, brief string, icon string, minWidth int64, minHeight int64, maxWidth int64, maxHeight int64, group bool) ([]message.IMessageElement, error) {
|
||||||
xml := ""
|
xml := ""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user