fix: 更新 ImageMessage 的 URL 处理逻辑,修复日志记录方式并移除冗余日志
This commit is contained in:
parent
bb6187dcc1
commit
9400e317ea
@ -3,7 +3,6 @@ package message
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -49,7 +48,7 @@ func (msg *ImageMessage) ToCQString() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
// URL 转义
|
// URL 转义
|
||||||
escapedURL := url.QueryEscape(msg.Data.URL)
|
escapedURL, _ := url.QueryUnescape(msg.Data.URL)
|
||||||
|
|
||||||
escapedURL = strings.ReplaceAll(escapedURL, ",", ",")
|
escapedURL = strings.ReplaceAll(escapedURL, ",", ",")
|
||||||
escapedURL = strings.ReplaceAll(escapedURL, "[", "[")
|
escapedURL = strings.ReplaceAll(escapedURL, "[", "[")
|
||||||
@ -64,7 +63,6 @@ func (msg *ImageMessage) ToCQString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (msg *ImageMessage) ParseMessage(data string) error {
|
func (msg *ImageMessage) ParseMessage(data string) error {
|
||||||
log.Println("ParseMessage", data)
|
|
||||||
// 使用正则表达式提取各个字段
|
// 使用正则表达式提取各个字段
|
||||||
re := regexp.MustCompile(`\[CQ:image,(.*?)\]`)
|
re := regexp.MustCompile(`\[CQ:image,(.*?)\]`)
|
||||||
matches := re.FindStringSubmatch(data)
|
matches := re.FindStringSubmatch(data)
|
||||||
@ -81,7 +79,7 @@ func (msg *ImageMessage) ParseMessage(data string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg.Data.URL = attrs["url"]
|
msg.Data.URL = attrs["url"]
|
||||||
msg.Data.URL = url.QueryEscape(msg.Data.URL)
|
msg.Data.URL, _ = url.QueryUnescape(msg.Data.URL)
|
||||||
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, ",", ",")
|
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, ",", ",")
|
||||||
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, "[", "[")
|
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, "[", "[")
|
||||||
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, "]", "]")
|
msg.Data.URL = strings.ReplaceAll(msg.Data.URL, "]", "]")
|
||||||
|
@ -94,13 +94,13 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
|
|||||||
// }
|
// }
|
||||||
fontBytes, err := os.ReadFile("./resource/emoji.ttf")
|
fontBytes, err := os.ReadFile("./resource/emoji.ttf")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("failed to load font: %v", err)
|
log.Printf("failed to load font: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析字体
|
// 解析字体
|
||||||
font, err := opentype.Parse(fontBytes)
|
font, err := opentype.Parse(fontBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("failed to parse font: %v", err)
|
log.Printf("failed to parse font: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置字体大小和 DPI
|
// 设置字体大小和 DPI
|
||||||
@ -114,7 +114,7 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
|
|||||||
Hinting: 0,
|
Hinting: 0,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print("failed to create font face: %v", err)
|
log.Printf("failed to create font face: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +154,6 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isImageCQ(text string) (string, bool) {
|
func isImageCQ(text string) (string, bool) {
|
||||||
log.Println("isImageCQ ", text)
|
|
||||||
imgMsg := message.ImageMessage{}
|
imgMsg := message.ImageMessage{}
|
||||||
if err := imgMsg.ParseMessage(text); err == nil {
|
if err := imgMsg.ParseMessage(text); err == nil {
|
||||||
return imgMsg.Data.URL, true
|
return imgMsg.Data.URL, true
|
||||||
|
@ -65,11 +65,6 @@ func normalizeURL(rawURL string) string {
|
|||||||
|
|
||||||
// DownloadFile 下载文件到指定目录,返回带有正确扩展名的完整文件路径
|
// DownloadFile 下载文件到指定目录,返回带有正确扩展名的完整文件路径
|
||||||
func DownloadFile(url string, dirPath string) (filepath string, err error) {
|
func DownloadFile(url string, dirPath string) (filepath string, err error) {
|
||||||
|
|
||||||
url = strings.ReplaceAll(url, ",", ",")
|
|
||||||
url = strings.ReplaceAll(url, "[", "[")
|
|
||||||
url = strings.ReplaceAll(url, "]", "]")
|
|
||||||
url = strings.ReplaceAll(url, "&", "&")
|
|
||||||
// 发送 HTTP GET 请求
|
// 发送 HTTP GET 请求
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
var maxRetry = 100
|
var maxRetry = 100
|
||||||
|
Loading…
x
Reference in New Issue
Block a user