From 7ba0f74b49192a34c42334a02ca612ec97c4b2f4 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 12 Apr 2025 21:16:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=B8=B8=E9=87=8F=20?= =?UTF-8?q?TempDir=20=E5=B9=B6=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=E5=B7=B2=E5=AD=98=E5=9C=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/path.go | 5 +++++ handler/beatleader/beatleader.go | 2 +- handler/scoresaber/score.go | 14 ++++++++------ service/beatleader/model.go | 4 ++-- service/scoresaber/model.go | 4 ++-- service/xibao/image_gen.go | 2 +- util/url.go | 2 +- 7 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 constants/path.go diff --git a/constants/path.go b/constants/path.go new file mode 100644 index 0000000..220abbd --- /dev/null +++ b/constants/path.go @@ -0,0 +1,5 @@ +package constants + +const ( + TempDir = "/tmp/qqbot" +) diff --git a/handler/beatleader/beatleader.go b/handler/beatleader/beatleader.go index ddd74cd..0a611cb 100644 --- a/handler/beatleader/beatleader.go +++ b/handler/beatleader/beatleader.go @@ -178,7 +178,7 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) { wg.Add(1) go func(songHash string) { defer wg.Done() - filePath, err := util.DownloadFile(coverImageMap[songHash], "/tmp/qqbot") + filePath, err := util.DownloadFile(coverImageMap[songHash], "/tmp/qqbot", true) if err != nil { log.Printf("下载图片失败: %v", err) return diff --git a/handler/scoresaber/score.go b/handler/scoresaber/score.go index db03404..2783144 100644 --- a/handler/scoresaber/score.go +++ b/handler/scoresaber/score.go @@ -3,7 +3,6 @@ package scoresaber import ( "fmt" "log" - "os" "strconv" "strings" "sync" @@ -211,12 +210,15 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) { go func(songHash string) { defer wg.Done() //文件存在则跳过 - if _, err := os.Stat(util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(songHash + ".jpeg"))); err == nil { + // if _, err := os.Stat(util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(songHash + ".jpeg"))); err == nil { + // return + // } + filePath, err := util.DownloadFile(coverImageMap[songHash], constants.TempDir, true) + if err != nil { + log.Printf("下载图片失败: %v", err) return } - util.DownloadFile(coverImageMap[songHash], util.GenTempFilePath(songHash+".jpeg")) - newPath, err := util.ResizeImageByMaxHeight(util.GenTempFilePath(songHash+".jpeg"), 20) - os.Remove(util.GenTempFilePath(songHash + ".jpeg")) + newPath, err := util.ResizeImageByMaxHeight(filePath, 20) if err != nil { log.Printf("缩放图片失败: %v", err) } @@ -228,7 +230,7 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) { imageMsg := message.ImageMessage{ Type: "image", Data: message.ImageMessageData{ - File: util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(record.SongHash + ".jpeg")), + File: coverImageMap[record.SongHash], }, } scoreMsg += imageMsg.ToCQString() + record.ToString() + "\n" diff --git a/service/beatleader/model.go b/service/beatleader/model.go index 1ea79a5..3cfb1d9 100644 --- a/service/beatleader/model.go +++ b/service/beatleader/model.go @@ -408,7 +408,7 @@ func (p PlayerDataLite) IsDiffFrom(p2 PlayerDataLite) bool { func (p PlayerData) ToString() string { - filePath, err := util.DownloadFile(p.Avatar, "/tmp/qqbot") + filePath, err := util.DownloadFile(p.Avatar, "/tmp/qqbot", false) if err != nil { log.Default().Printf("下载头像失败,url:%s,err:%v", p.Avatar, err) } @@ -468,7 +468,7 @@ func (p PlayerDataLite) ToString() string { } func (p PlayerData) LastDiffToString(lastDayQueryData PlayerDataLite) string { - filePath, err := util.DownloadFile(p.Avatar, "/tmp/qqbot") + filePath, err := util.DownloadFile(p.Avatar, "/tmp/qqbot", false) if err != nil { log.Default().Printf("下载头像失败,url:%s,err:%v", p.Avatar, err) } diff --git a/service/scoresaber/model.go b/service/scoresaber/model.go index a989a80..bf5df24 100644 --- a/service/scoresaber/model.go +++ b/service/scoresaber/model.go @@ -224,7 +224,7 @@ func (p PlayerDataLite) IsDiffFrom(p2 PlayerDataLite) bool { } func (p PlayerData) ToString() string { - filePath, err := util.DownloadFile(p.ProfilePicture, "/tmp/qqbot") + filePath, err := util.DownloadFile(p.ProfilePicture, "/tmp/qqbot", false) if err != nil { log.Default().Printf("下载头像失败,url:%s,err:%v", p.ProfilePicture, err) } @@ -284,7 +284,7 @@ func (p PlayerDataLite) ToString() string { } func (p PlayerData) LastDiffToString(lastDayQueryData PlayerDataLite) string { - filePath, err := util.DownloadFile(p.ProfilePicture, "/tmp/qqbot") + filePath, err := util.DownloadFile(p.ProfilePicture, "/tmp/qqbot", false) if err != nil { log.Default().Printf("下载头像失败,url:%s,err:%v", p.ProfilePicture, err) } diff --git a/service/xibao/image_gen.go b/service/xibao/image_gen.go index 0f8d773..0f1ca7f 100644 --- a/service/xibao/image_gen.go +++ b/service/xibao/image_gen.go @@ -28,7 +28,7 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo dc.DrawImage(im, 0, 0) // 判断是否为图片 if imgUrl, ok := isImageCQ(text); ok { - filePath, err := util.DownloadFile(imgUrl, "/tmp/qqbot") + filePath, err := util.DownloadFile(imgUrl, "/tmp/qqbot", false) if err != nil { log.Print("无法下载图片:", err) return diff --git a/util/url.go b/util/url.go index 6dcccaf..2bd253f 100644 --- a/util/url.go +++ b/util/url.go @@ -65,7 +65,7 @@ func normalizeURL(rawURL string) string { } // DownloadFile 下载文件到指定目录,返回带有正确扩展名的完整文件路径 -func DownloadFile(urlStr string, dirPath string) (filepath string, err error) { +func DownloadFile(urlStr string, dirPath string, skipExist bool) (filepath string, err error) { // 创建fasthttp客户端 client := &fasthttp.Client{ ReadTimeout: 30 * time.Second,