mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
parent
a49becfff4
commit
59bc7b4bae
@ -1427,7 +1427,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) global
|
||||
func (bot *CQBot) CQGetImage(file string) global.MSG {
|
||||
var b []byte
|
||||
var err error
|
||||
if cache.EnableCacheDB && strings.HasSuffix(file, ".image") {
|
||||
if strings.HasSuffix(file, ".image") {
|
||||
var f []byte
|
||||
f, err = hex.DecodeString(strings.TrimSuffix(file, ".image"))
|
||||
b = cache.Image.Get(f)
|
||||
|
@ -1309,7 +1309,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
||||
}
|
||||
rawPath := path.Join(global.ImagePath, f)
|
||||
if video {
|
||||
if strings.HasSuffix(f, ".video") && cache.EnableCacheDB {
|
||||
if strings.HasSuffix(f, ".video") {
|
||||
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".video"))
|
||||
if err == nil {
|
||||
if b := cache.Video.Get(hash); b != nil {
|
||||
@ -1334,7 +1334,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
||||
return &LocalImageElement{File: cacheFile}, nil
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(f, ".image") && cache.EnableCacheDB {
|
||||
if strings.HasSuffix(f, ".image") {
|
||||
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".image"))
|
||||
if err == nil {
|
||||
if b := cache.Image.Get(hash); b != nil {
|
||||
|
@ -726,7 +726,6 @@ func (bot *CQBot) groupDecrease(groupCode, userUin int64, operator *client.Group
|
||||
}
|
||||
|
||||
func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||
// TODO(wdvxdr): remove these old cache file in v1.0.0
|
||||
for _, elem := range e {
|
||||
switch i := elem.(type) {
|
||||
case *message.GroupImageElement:
|
||||
@ -744,12 +743,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||
w.WriteString(i.ImageId)
|
||||
w.WriteString(i.Url)
|
||||
})
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if cache.EnableCacheDB {
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
||||
}
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
|
||||
case *message.GuildImageElement:
|
||||
data := binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
@ -758,14 +753,9 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||
w.WriteString(i.Url)
|
||||
})
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if cache.EnableCacheDB {
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
||||
}
|
||||
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
if i.Url != "" && !global.PathExists(path.Join(global.ImagePath, "guild-images", filename)) {
|
||||
if err := global.DownloadFile(i.Url, path.Join(global.ImagePath, "guild-images", filename), -1, map[string]string{}); err != nil {
|
||||
if err := global.DownloadFile(i.Url, path.Join(global.ImagePath, "guild-images", filename), -1, nil); err != nil {
|
||||
log.Warnf("下载频道图片时出现错误: %v", err)
|
||||
}
|
||||
}
|
||||
@ -776,12 +766,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||
w.WriteString(i.ImageId)
|
||||
w.WriteString(i.Url)
|
||||
})
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if cache.EnableCacheDB {
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
||||
}
|
||||
cache.Image.Insert(i.Md5, data)
|
||||
|
||||
case *message.VoiceElement:
|
||||
// todo: don't download original file?
|
||||
i.Name = strings.ReplaceAll(i.Name, "{", "")
|
||||
@ -804,11 +790,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||
w.Write(i.Uuid)
|
||||
})
|
||||
filename := hex.EncodeToString(i.Md5) + ".video"
|
||||
if cache.EnableCacheDB {
|
||||
cache.Video.Insert(i.Md5, data)
|
||||
} else if !global.PathExists(path.Join(global.VideoPath, filename)) {
|
||||
_ = os.WriteFile(path.Join(global.VideoPath, filename), data, 0o644)
|
||||
}
|
||||
cache.Video.Insert(i.Md5, data)
|
||||
i.Name = filename
|
||||
i.Url = bot.Client.GetShortVideoUrl(i.Uuid, i.Md5)
|
||||
}
|
||||
|
@ -71,9 +71,6 @@ func DownloadFile(url, path string, limit int64, headers map[string]string) erro
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
if _, ok := headers["User-Agent"]; !ok {
|
||||
req.Header["User-Agent"] = []string{UserAgent}
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
|
17
internal/cache/cache.go
vendored
17
internal/cache/cache.go
vendored
@ -12,11 +12,6 @@ import (
|
||||
"github.com/Mrs4s/go-cqhttp/internal/btree"
|
||||
)
|
||||
|
||||
// todo(wdvxdr): always enable db-cache in v1.0.0
|
||||
|
||||
// EnableCacheDB 是否启用 btree db缓存图片等
|
||||
var EnableCacheDB bool
|
||||
|
||||
// Media Cache DBs
|
||||
var (
|
||||
Image Cache
|
||||
@ -63,14 +58,12 @@ func (c *Cache) Delete(md5 []byte) {
|
||||
// Init 初始化 Cache
|
||||
func Init() {
|
||||
node, ok := base.Database["cache"]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
EnableCacheDB = true
|
||||
var conf map[string]string
|
||||
err := node.Decode(&conf)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read cache config: %v", err)
|
||||
if ok {
|
||||
err := node.Decode(&conf)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read cache config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var open = func(typ string, cache *Cache) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user