From c499389e66011f5ed447f93b6af1dcb98ae47b9e Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Tue, 19 Oct 2021 22:14:00 +0800 Subject: [PATCH] refactor(cqcode): simply cache arg handle --- coolq/cqcode.go | 31 ++++++++++--------------------- db/multidb.go | 9 ++++++--- global/fs.go | 5 +---- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/coolq/cqcode.go b/coolq/cqcode.go index f567463..eb2278f 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -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 } diff --git a/db/multidb.go b/db/multidb.go index f878c9c..741ebf7 100644 --- a/db/multidb.go +++ b/db/multidb.go @@ -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) } diff --git a/global/fs.go b/global/fs.go index 700a177..91d346b 100644 --- a/global/fs.go +++ b/global/fs.go @@ -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)