diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 73717c80f..fc018b060 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -885,7 +885,7 @@ func (bot *CQBot) ConvertElement(spec *onebot.Spec, elem msg.Element, sourceType func (bot *CQBot) makeImageOrVideoElem(elem msg.Element, video bool, sourceType message.SourceType) (message.IMessageElement, error) { f := elem.Get("file") u := elem.Get("url") - if strings.HasPrefix(u, "http") { + if strings.HasPrefix(f, "http") { hash := md5.Sum([]byte(f)) cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash[:])+".cache") maxSize := int64(maxImageSize) @@ -901,7 +901,7 @@ func (bot *CQBot) makeImageOrVideoElem(elem msg.Element, video bool, sourceType _ = os.Remove(cacheFile) } { - r := download.Request{URL: u, Limit: maxSize} + r := download.Request{URL: f, Limit: maxSize} if err := r.WriteToFileMultiThreading(cacheFile, thread); err != nil { return nil, err } @@ -980,7 +980,7 @@ func (bot *CQBot) makeImageOrVideoElem(elem msg.Element, video bool, sourceType } } } - exist := global.PathExists(rawPath) + exist := global.FileExists(rawPath) if !exist { if elem.Get("url") != "" { elem.Data = []msg.Pair{{K: "file", V: elem.Get("url")}} diff --git a/global/fs.go b/global/fs.go index af34fab83..abf8bfe14 100644 --- a/global/fs.go +++ b/global/fs.go @@ -46,6 +46,12 @@ func PathExists(path string) bool { return err == nil || errors.Is(err, os.ErrExist) } +// FileExists 判断给定path是否为存在且path为文件 +func FileExists(path string) bool { + fi, err := os.Stat(path) + return err == nil && !fi.IsDir() +} + // ReadAllText 读取给定path对应文件,无法读取时返回空值 func ReadAllText(path string) string { b, err := os.ReadFile(path)