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