mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
refactor(cqcode): simply cache arg handle
This commit is contained in:
parent
a19baec013
commit
c499389e66
@ -1069,10 +1069,6 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
}
|
||||
return bot.makeShowPic(img, source, brief, icon, minWidth, minHeight, maxWidth, maxHeight, isGroup)
|
||||
case "video":
|
||||
cache := d["cache"]
|
||||
if cache == "" {
|
||||
cache = "1"
|
||||
}
|
||||
file, err := bot.makeImageOrVideoElem(d, true, isGroup)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -1086,7 +1082,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
}
|
||||
var data []byte
|
||||
if cover, ok := d["cover"]; ok {
|
||||
data, _ = global.FindFile(cover, cache, global.ImagePath)
|
||||
data, _ = global.FindFile(cover, d["cache"], global.ImagePath)
|
||||
} else {
|
||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
||||
data, _ = os.ReadFile(v.File + ".jpg")
|
||||
@ -1107,7 +1103,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
_, _ = video.Seek(0, io.SeekStart)
|
||||
hash, _ := utils.ComputeMd5AndLength(video)
|
||||
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash)+".mp4")
|
||||
if global.PathExists(cacheFile) && cache == "1" {
|
||||
if global.PathExists(cacheFile) && (d["cache"] == "" || d["cache"] == "1") {
|
||||
goto ok
|
||||
}
|
||||
err = global.EncodeMP4(v.File, cacheFile)
|
||||
@ -1230,23 +1226,16 @@ func CQCodeUnescapeValue(content string) string {
|
||||
func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (message.IMessageElement, error) {
|
||||
f := d["file"]
|
||||
if strings.HasPrefix(f, "http") {
|
||||
cache := d["cache"]
|
||||
c := d["c"]
|
||||
if cache == "" {
|
||||
cache = "1"
|
||||
}
|
||||
hash := md5.Sum([]byte(f))
|
||||
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
maxSize := func() int64 {
|
||||
if video {
|
||||
return maxVideoSize
|
||||
}
|
||||
return maxImageSize
|
||||
}()
|
||||
thread, _ := strconv.Atoi(c)
|
||||
maxSize := int64(maxImageSize)
|
||||
if video {
|
||||
maxSize = maxVideoSize
|
||||
}
|
||||
thread, _ := strconv.Atoi(d["c"])
|
||||
exist := global.PathExists(cacheFile)
|
||||
if exist && cache == "1" {
|
||||
goto hasCacheFile
|
||||
if exist && (d["cache"] == "" || d["cache"] == "1") {
|
||||
goto useCacheFile
|
||||
}
|
||||
if exist {
|
||||
_ = os.Remove(cacheFile)
|
||||
@ -1254,7 +1243,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
if err := global.DownloadFileMultiThreading(f, cacheFile, maxSize, thread, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hasCacheFile:
|
||||
useCacheFile:
|
||||
if video {
|
||||
return &LocalVideoElement{File: cacheFile}, nil
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ var backends []Database
|
||||
// drivers 多数据库启动
|
||||
var drivers = make(map[string]func(node yaml.Node) Database)
|
||||
|
||||
// DatabaseDisabledError 没有可用的db
|
||||
var DatabaseDisabledError = errors.New("database disabled")
|
||||
|
||||
// Register 添加数据库后端
|
||||
func Register(name string, init func(yaml.Node) Database) {
|
||||
if _, ok := drivers[name]; ok {
|
||||
@ -47,21 +50,21 @@ func Open() error {
|
||||
|
||||
func GetMessageByGlobalID(id int32) (StoredMessage, error) {
|
||||
if len(backends) == 0 {
|
||||
return nil, errors.New("database disabled")
|
||||
return nil, DatabaseDisabledError
|
||||
}
|
||||
return backends[0].GetMessageByGlobalID(id)
|
||||
}
|
||||
|
||||
func GetGroupMessageByGlobalID(id int32) (*StoredGroupMessage, error) {
|
||||
if len(backends) == 0 {
|
||||
return nil, errors.New("database disabled")
|
||||
return nil, DatabaseDisabledError
|
||||
}
|
||||
return backends[0].GetGroupMessageByGlobalID(id)
|
||||
}
|
||||
|
||||
func GetPrivateMessageByGlobalID(id int32) (*StoredPrivateMessage, error) {
|
||||
if len(backends) == 0 {
|
||||
return nil, errors.New("database disabled")
|
||||
return nil, DatabaseDisabledError
|
||||
}
|
||||
return backends[0].GetPrivateMessageByGlobalID(id)
|
||||
}
|
||||
|
@ -85,12 +85,9 @@ func FindFile(file, cache, p string) (data []byte, err error) {
|
||||
data, err = nil, ErrSyntax
|
||||
switch {
|
||||
case strings.HasPrefix(file, "http"): // https also has prefix http
|
||||
if cache == "" {
|
||||
cache = "1"
|
||||
}
|
||||
hash := md5.Sum([]byte(file))
|
||||
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
if PathExists(cacheFile) && cache == "1" {
|
||||
if (cache == "" || cache == "1") && PathExists(cacheFile) {
|
||||
return os.ReadFile(cacheFile)
|
||||
}
|
||||
data, err = GetBytes(file)
|
||||
|
Loading…
x
Reference in New Issue
Block a user