mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +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 {
|
func (bot *CQBot) CQGetImage(file string) global.MSG {
|
||||||
var b []byte
|
var b []byte
|
||||||
var err error
|
var err error
|
||||||
if cache.EnableCacheDB && strings.HasSuffix(file, ".image") {
|
if strings.HasSuffix(file, ".image") {
|
||||||
var f []byte
|
var f []byte
|
||||||
f, err = hex.DecodeString(strings.TrimSuffix(file, ".image"))
|
f, err = hex.DecodeString(strings.TrimSuffix(file, ".image"))
|
||||||
b = cache.Image.Get(f)
|
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)
|
rawPath := path.Join(global.ImagePath, f)
|
||||||
if video {
|
if video {
|
||||||
if strings.HasSuffix(f, ".video") && cache.EnableCacheDB {
|
if strings.HasSuffix(f, ".video") {
|
||||||
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".video"))
|
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".video"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if b := cache.Video.Get(hash); b != 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
|
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"))
|
hash, err := hex.DecodeString(strings.TrimSuffix(f, ".image"))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if b := cache.Image.Get(hash); b != 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) {
|
func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
||||||
// TODO(wdvxdr): remove these old cache file in v1.0.0
|
|
||||||
for _, elem := range e {
|
for _, elem := range e {
|
||||||
switch i := elem.(type) {
|
switch i := elem.(type) {
|
||||||
case *message.GroupImageElement:
|
case *message.GroupImageElement:
|
||||||
@ -744,12 +743,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
|||||||
w.WriteString(i.ImageId)
|
w.WriteString(i.ImageId)
|
||||||
w.WriteString(i.Url)
|
w.WriteString(i.Url)
|
||||||
})
|
})
|
||||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
|
||||||
if cache.EnableCacheDB {
|
|
||||||
cache.Image.Insert(i.Md5, data)
|
cache.Image.Insert(i.Md5, data)
|
||||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
|
||||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
|
||||||
}
|
|
||||||
case *message.GuildImageElement:
|
case *message.GuildImageElement:
|
||||||
data := binary.NewWriterF(func(w *binary.Writer) {
|
data := binary.NewWriterF(func(w *binary.Writer) {
|
||||||
w.Write(i.Md5)
|
w.Write(i.Md5)
|
||||||
@ -758,14 +753,9 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
|||||||
w.WriteString(i.Url)
|
w.WriteString(i.Url)
|
||||||
})
|
})
|
||||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||||
if cache.EnableCacheDB {
|
|
||||||
cache.Image.Insert(i.Md5, data)
|
cache.Image.Insert(i.Md5, data)
|
||||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
|
||||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
|
||||||
}
|
|
||||||
|
|
||||||
if i.Url != "" && !global.PathExists(path.Join(global.ImagePath, "guild-images", filename)) {
|
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)
|
log.Warnf("下载频道图片时出现错误: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -776,12 +766,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
|||||||
w.WriteString(i.ImageId)
|
w.WriteString(i.ImageId)
|
||||||
w.WriteString(i.Url)
|
w.WriteString(i.Url)
|
||||||
})
|
})
|
||||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
|
||||||
if cache.EnableCacheDB {
|
|
||||||
cache.Image.Insert(i.Md5, data)
|
cache.Image.Insert(i.Md5, data)
|
||||||
} else if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
|
||||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), data, 0o644)
|
|
||||||
}
|
|
||||||
case *message.VoiceElement:
|
case *message.VoiceElement:
|
||||||
// todo: don't download original file?
|
// todo: don't download original file?
|
||||||
i.Name = strings.ReplaceAll(i.Name, "{", "")
|
i.Name = strings.ReplaceAll(i.Name, "{", "")
|
||||||
@ -804,11 +790,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
|
|||||||
w.Write(i.Uuid)
|
w.Write(i.Uuid)
|
||||||
})
|
})
|
||||||
filename := hex.EncodeToString(i.Md5) + ".video"
|
filename := hex.EncodeToString(i.Md5) + ".video"
|
||||||
if cache.EnableCacheDB {
|
|
||||||
cache.Video.Insert(i.Md5, data)
|
cache.Video.Insert(i.Md5, data)
|
||||||
} else if !global.PathExists(path.Join(global.VideoPath, filename)) {
|
|
||||||
_ = os.WriteFile(path.Join(global.VideoPath, filename), data, 0o644)
|
|
||||||
}
|
|
||||||
i.Name = filename
|
i.Name = filename
|
||||||
i.Url = bot.Client.GetShortVideoUrl(i.Uuid, i.Md5)
|
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)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := headers["User-Agent"]; !ok {
|
|
||||||
req.Header["User-Agent"] = []string{UserAgent}
|
|
||||||
}
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
11
internal/cache/cache.go
vendored
11
internal/cache/cache.go
vendored
@ -12,11 +12,6 @@ import (
|
|||||||
"github.com/Mrs4s/go-cqhttp/internal/btree"
|
"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
|
// Media Cache DBs
|
||||||
var (
|
var (
|
||||||
Image Cache
|
Image Cache
|
||||||
@ -63,15 +58,13 @@ func (c *Cache) Delete(md5 []byte) {
|
|||||||
// Init 初始化 Cache
|
// Init 初始化 Cache
|
||||||
func Init() {
|
func Init() {
|
||||||
node, ok := base.Database["cache"]
|
node, ok := base.Database["cache"]
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
EnableCacheDB = true
|
|
||||||
var conf map[string]string
|
var conf map[string]string
|
||||||
|
if ok {
|
||||||
err := node.Decode(&conf)
|
err := node.Decode(&conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to read cache config: %v", err)
|
log.Fatalf("failed to read cache config: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var open = func(typ string, cache *Cache) {
|
var open = func(typ string, cache *Cache) {
|
||||||
file := conf[typ]
|
file := conf[typ]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user