mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-06 20:13:50 +08:00
coolq: clean readImageCache
This commit is contained in:
parent
a85f846a5f
commit
75bed6aabc
@ -81,7 +81,7 @@ func Main() {
|
|||||||
|
|
||||||
mkCacheDir := func(path string, _type string) {
|
mkCacheDir := func(path string, _type string) {
|
||||||
if !global.PathExists(path) {
|
if !global.PathExists(path) {
|
||||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
if err := os.MkdirAll(path, 0o644); err != nil {
|
||||||
log.Fatalf("创建%s缓存文件夹失败: %v", _type, err)
|
log.Fatalf("创建%s缓存文件夹失败: %v", _type, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ type PokeElement struct {
|
|||||||
type LocalImageElement struct {
|
type LocalImageElement struct {
|
||||||
Stream io.ReadSeeker
|
Stream io.ReadSeeker
|
||||||
File string
|
File string
|
||||||
Url string
|
URL string
|
||||||
|
|
||||||
Flash bool
|
Flash bool
|
||||||
EffectID int32
|
EffectID int32
|
||||||
@ -366,7 +366,7 @@ func ToStringMessage(e []message.IMessageElement, source MessageSource, isRaw ..
|
|||||||
if ur {
|
if ur {
|
||||||
write("[CQ:image,file=%s%s]", hex.EncodeToString(m[:])+".image", arg)
|
write("[CQ:image,file=%s%s]", hex.EncodeToString(m[:])+".image", arg)
|
||||||
} else {
|
} else {
|
||||||
write("[CQ:image,file=%s,url=%s%s]", hex.EncodeToString(m[:])+".image", CQCodeEscapeValue(o.Url), arg)
|
write("[CQ:image,file=%s,url=%s%s]", hex.EncodeToString(m[:])+".image", cqcode.EscapeValue(o.URL), arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *message.GuildImageElement:
|
case *message.GuildImageElement:
|
||||||
@ -1198,7 +1198,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
|||||||
if video {
|
if video {
|
||||||
return &LocalVideoElement{File: cacheFile}, nil
|
return &LocalVideoElement{File: cacheFile}, nil
|
||||||
}
|
}
|
||||||
return &LocalImageElement{File: cacheFile, Url: f}, nil
|
return &LocalImageElement{File: cacheFile, URL: f}, nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(f, "file") {
|
if strings.HasPrefix(f, "file") {
|
||||||
fu, err := url.Parse(f)
|
fu, err := url.Parse(f)
|
||||||
@ -1224,14 +1224,14 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
|||||||
if info.Size() == 0 || info.Size() >= maxImageSize {
|
if info.Size() == 0 || info.Size() >= maxImageSize {
|
||||||
return nil, errors.New("invalid image size")
|
return nil, errors.New("invalid image size")
|
||||||
}
|
}
|
||||||
return &LocalImageElement{File: fu.Path, Url: f}, nil
|
return &LocalImageElement{File: fu.Path, URL: f}, nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(f, "base64") && !video {
|
if strings.HasPrefix(f, "base64") && !video {
|
||||||
b, err := param.Base64DecodeString(strings.TrimPrefix(f, "base64://"))
|
b, err := param.Base64DecodeString(strings.TrimPrefix(f, "base64://"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &LocalImageElement{Stream: bytes.NewReader(b), Url: f}, nil
|
return &LocalImageElement{Stream: bytes.NewReader(b), URL: f}, nil
|
||||||
}
|
}
|
||||||
rawPath := path.Join(global.ImagePath, f)
|
rawPath := path.Join(global.ImagePath, f)
|
||||||
if video {
|
if video {
|
||||||
@ -1280,7 +1280,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
|
|||||||
return nil, errors.New("invalid image")
|
return nil, errors.New("invalid image")
|
||||||
}
|
}
|
||||||
if path.Ext(rawPath) != ".image" {
|
if path.Ext(rawPath) != ".image" {
|
||||||
return &LocalImageElement{File: rawPath, Url: u}, nil
|
return &LocalImageElement{File: rawPath, URL: u}, nil
|
||||||
}
|
}
|
||||||
b, err := os.ReadFile(rawPath)
|
b, err := os.ReadFile(rawPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1299,38 +1299,27 @@ func (bot *CQBot) readImageCache(b []byte, sourceType MessageSourceType) (messag
|
|||||||
size := r.ReadInt32()
|
size := r.ReadInt32()
|
||||||
r.ReadString()
|
r.ReadString()
|
||||||
imageURL := r.ReadString()
|
imageURL := r.ReadString()
|
||||||
if size == 0 {
|
if size == 0 && imageURL != "" {
|
||||||
if imageURL != "" {
|
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, sourceType)
|
||||||
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, sourceType)
|
|
||||||
}
|
|
||||||
return nil, errors.New("img size is 0")
|
|
||||||
}
|
|
||||||
if len(hash) != 16 {
|
|
||||||
return nil, errors.New("invalid hash")
|
|
||||||
}
|
}
|
||||||
var rsp message.IMessageElement
|
var rsp message.IMessageElement
|
||||||
if sourceType == MessageSourceGroup {
|
switch sourceType { // nolint:exhaustive
|
||||||
|
case MessageSourceGroup:
|
||||||
rsp, err = bot.Client.QueryGroupImage(int64(rand.Uint32()), hash, size)
|
rsp, err = bot.Client.QueryGroupImage(int64(rand.Uint32()), hash, size)
|
||||||
goto ok
|
case MessageSourceGuildChannel:
|
||||||
}
|
|
||||||
if sourceType == MessageSourceGuildChannel {
|
|
||||||
if len(bot.Client.GuildService.Guilds) == 0 {
|
if len(bot.Client.GuildService.Guilds) == 0 {
|
||||||
err = errors.New("cannot query guild image: not any joined guild")
|
err = errors.New("cannot query guild image: not any joined guild")
|
||||||
goto ok
|
break
|
||||||
}
|
}
|
||||||
guild := bot.Client.GuildService.Guilds[0]
|
guild := bot.Client.GuildService.Guilds[0]
|
||||||
rsp, err = bot.Client.GuildService.QueryImage(guild.GuildId, guild.Channels[0].ChannelId, hash, uint64(size))
|
rsp, err = bot.Client.GuildService.QueryImage(guild.GuildId, guild.Channels[0].ChannelId, hash, uint64(size))
|
||||||
goto ok
|
default:
|
||||||
|
rsp, err = bot.Client.QueryFriendImage(int64(rand.Uint32()), hash, size)
|
||||||
}
|
}
|
||||||
rsp, err = bot.Client.QueryFriendImage(int64(rand.Uint32()), hash, size)
|
if err != nil && imageURL != "" {
|
||||||
ok:
|
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, sourceType)
|
||||||
if err != nil {
|
|
||||||
if imageURL != "" {
|
|
||||||
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, sourceType)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
return rsp, nil
|
return rsp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *CQBot) readVideoCache(b []byte) message.IMessageElement {
|
func (bot *CQBot) readVideoCache(b []byte) message.IMessageElement {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user