From 38940fa07ff8acb2460f3b2f41d5e223993a3771 Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 22 Feb 2025 18:40:26 +0800 Subject: [PATCH] refactor: centralize temporary file path generation and update file references --- handler/beatleader/beatleader.go | 2 +- handler/getweb/getweb.go | 5 +++-- handler/scoresaber/score.go | 2 +- handler/xibao/xibao.go | 17 +++++++++-------- service/beatleader/gen_picture.go | 3 ++- service/scoresaber/gen_picture.go | 3 ++- util/gen_temp_file_path.go | 7 +++++++ 7 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 util/gen_temp_file_path.go diff --git a/handler/beatleader/beatleader.go b/handler/beatleader/beatleader.go index b202f94..da7dc87 100644 --- a/handler/beatleader/beatleader.go +++ b/handler/beatleader/beatleader.go @@ -158,7 +158,7 @@ func getMyRecentScore(msg model.Message) (reply model.Reply) { func screenShotBL(msg model.Message) (reply model.Reply) { return model.Reply{ - ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/" + beatleader.GetBLPicture(strconv.Itoa(int(msg.UserId))) + "]", + ReplyMsg: "[CQ:image,file=file:///root/qqbot/" + beatleader.GetBLPicture(strconv.Itoa(int(msg.UserId))) + "]", ReferOriginMsg: true, FromMsg: msg, } diff --git a/handler/getweb/getweb.go b/handler/getweb/getweb.go index a614339..a29b6a5 100644 --- a/handler/getweb/getweb.go +++ b/handler/getweb/getweb.go @@ -16,7 +16,8 @@ func getweb(msg model.Message) (reply model.Reply) { } url := msg.RawMsg[len("上网 "):] url = formatURL(url) - if err := util.ScreenshotURL(url, "./tmp/getweb/url.png", 1620, 1960, 0, 0, 0, 0, ""); err != nil { + // if err := util.ScreenshotURL(url, "./tmp/getweb/url.png", 1620, 1960, 0, 0, 0, 0, ""); err != nil { + if err := util.ScreenshotURL(url, util.GenTempFilePath("getweb/url.png"), 1620, 1960, 0, 0, 0, 0, ""); err != nil { return model.Reply{ ReplyMsg: err.Error(), ReferOriginMsg: true, @@ -24,7 +25,7 @@ func getweb(msg model.Message) (reply model.Reply) { } } return model.Reply{ - ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/getweb/url.png]", + ReplyMsg: "[CQ:image,file=file:///root/qqbot/getweb/url.png]", ReferOriginMsg: true, FromMsg: msg, } diff --git a/handler/scoresaber/score.go b/handler/scoresaber/score.go index fddb1cf..9a50033 100644 --- a/handler/scoresaber/score.go +++ b/handler/scoresaber/score.go @@ -176,7 +176,7 @@ func getMyRecentScore(msg model.Message) (reply model.Reply) { func screenshotSS(msg model.Message) (reply model.Reply) { return model.Reply{ - ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/" + scoresaber.GetSSPicture(strconv.Itoa(int(msg.UserId))) + "]", + ReplyMsg: "[CQ:image,file=file:///root/qqbot/" + scoresaber.GetSSPicture(strconv.Itoa(int(msg.UserId))) + "]", ReferOriginMsg: true, FromMsg: msg, } diff --git a/handler/xibao/xibao.go b/handler/xibao/xibao.go index 2362949..de6e585 100644 --- a/handler/xibao/xibao.go +++ b/handler/xibao/xibao.go @@ -8,6 +8,7 @@ import ( "git.lxtend.com/qqbot/handler" "git.lxtend.com/qqbot/model" "git.lxtend.com/qqbot/service/xibao" + "git.lxtend.com/qqbot/util" "github.com/google/uuid" ) @@ -20,7 +21,7 @@ func init() { func xiBao(msg model.Message) (reply model.Reply) { fileName := uuid.New().String() - filePath := "./tmp/" + fileName + ".png" + filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName)) re := regexp.MustCompile(`\s+`) tokens := re.Split(msg.RawMsg, 2) if len(tokens) < 2 { @@ -33,7 +34,7 @@ func xiBao(msg model.Message) (reply model.Reply) { } xibao.GenerateCongratulationImage(tokens[1], "./resource/xibao_background.png", filePath, true) return model.Reply{ - ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"), + ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"), ReferOriginMsg: true, FromMsg: msg, } @@ -42,10 +43,10 @@ func xiBao(msg model.Message) (reply model.Reply) { func xiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) { handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId) fileName := uuid.New().String() - filePath := "./tmp/" + fileName + ".png" + filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName)) xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/xibao_background.png", filePath, true) return model.Reply{ - ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"), + ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"), ReferOriginMsg: true, FromMsg: msg, }, true @@ -53,7 +54,7 @@ func xiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) { func beiBao(msg model.Message) (reply model.Reply) { fileName := uuid.New().String() - filePath := "./tmp/" + fileName + ".png" + filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName)) re := regexp.MustCompile(`\s+`) tokens := re.Split(msg.RawMsg, 2) if len(tokens) < 2 { @@ -66,7 +67,7 @@ func beiBao(msg model.Message) (reply model.Reply) { } xibao.GenerateCongratulationImage(tokens[1], "./resource/beibao_background.png", filePath, false) return model.Reply{ - ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"), + ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"), ReferOriginMsg: true, FromMsg: msg, } @@ -75,10 +76,10 @@ func beiBao(msg model.Message) (reply model.Reply) { func beiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) { handler.UnRegisterLiveHandler(msg.GroupInfo.GroupId, msg.UserId) fileName := uuid.New().String() - filePath := "./tmp/" + fileName + ".png" + filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName)) xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/beibao_background.png", filePath, false) return model.Reply{ - ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/tmp/%s]", fileName+".png"), + ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///root/qqbot/%s]", fileName+".png"), ReferOriginMsg: true, FromMsg: msg, }, true diff --git a/service/beatleader/gen_picture.go b/service/beatleader/gen_picture.go index ea47410..a21b488 100644 --- a/service/beatleader/gen_picture.go +++ b/service/beatleader/gen_picture.go @@ -12,7 +12,8 @@ func GetBLPicture(qqId string) (outputImgName string) { return "" } url := fmt.Sprintf("https://beatleader.xyz/u/%s", blId) - outputImgPath := fmt.Sprintf("./tmp/beatleader_%s.png", blId) + // outputImgPath := fmt.Sprintf("./tmp/beatleader_%s.png", blId) + outputImgPath := util.GenTempFilePath(fmt.Sprintf("beatleader_%s.png", blId)) outputImgName = fmt.Sprintf("beatleader_%s.png", blId) if err := util.ScreenshotURL(url, outputImgPath, 1420, 2280, 70, 50, 160, 140, "chartjs"); err != nil { fmt.Println(err) diff --git a/service/scoresaber/gen_picture.go b/service/scoresaber/gen_picture.go index 6e6d53a..1426ef8 100644 --- a/service/scoresaber/gen_picture.go +++ b/service/scoresaber/gen_picture.go @@ -12,7 +12,8 @@ func GetSSPicture(qqId string) (outputImgName string) { return "" } url := fmt.Sprintf("https://scoresaber.com/u/%s", ssId) - outputImgPath := fmt.Sprintf("./tmp/scoresaber_%s.png", ssId) + // outputImgPath := fmt.Sprintf("./tmp/scoresaber_%s.png", ssId) + outputImgPath := util.GenTempFilePath(fmt.Sprintf("scoresaber_%s.png", ssId)) outputImgName = fmt.Sprintf("scoresaber_%s.png", ssId) if err := util.ScreenshotURL(url, outputImgPath, 1420, 2080, 50, 400, 150, 150, ""); err != nil { fmt.Println(err) diff --git a/util/gen_temp_file_path.go b/util/gen_temp_file_path.go new file mode 100644 index 0000000..cea1a47 --- /dev/null +++ b/util/gen_temp_file_path.go @@ -0,0 +1,7 @@ +package util + +import "fmt" + +func GenTempFilePath(fileName string) string { + return fmt.Sprintf("/tmp/napcat/%s", fileName) +}