Skip to content

Commit

Permalink
fix: 修复无法使用url发送图片
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Nov 8, 2024
1 parent 020f67c commit afe1183
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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")}}
Expand Down
6 changes: 6 additions & 0 deletions global/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit afe1183

Please sign in to comment.