feat: 添加常量 TempDir 并更新文件下载逻辑以支持跳过已存在文件的选项

This commit is contained in:
lixiangwuxian 2025-04-12 21:16:58 +08:00
parent b49b780da2
commit 7ba0f74b49
7 changed files with 20 additions and 13 deletions

5
constants/path.go Normal file
View File

@ -0,0 +1,5 @@
package constants
const (
TempDir = "/tmp/qqbot"
)

View File

@ -178,7 +178,7 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
wg.Add(1) wg.Add(1)
go func(songHash string) { go func(songHash string) {
defer wg.Done() defer wg.Done()
filePath, err := util.DownloadFile(coverImageMap[songHash], "/tmp/qqbot") filePath, err := util.DownloadFile(coverImageMap[songHash], "/tmp/qqbot", true)
if err != nil { if err != nil {
log.Printf("下载图片失败: %v", err) log.Printf("下载图片失败: %v", err)
return return

View File

@ -3,7 +3,6 @@ package scoresaber
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -211,12 +210,15 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
go func(songHash string) { go func(songHash string) {
defer wg.Done() 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 return
} }
util.DownloadFile(coverImageMap[songHash], util.GenTempFilePath(songHash+".jpeg")) newPath, err := util.ResizeImageByMaxHeight(filePath, 20)
newPath, err := util.ResizeImageByMaxHeight(util.GenTempFilePath(songHash+".jpeg"), 20)
os.Remove(util.GenTempFilePath(songHash + ".jpeg"))
if err != nil { if err != nil {
log.Printf("缩放图片失败: %v", err) log.Printf("缩放图片失败: %v", err)
} }
@ -228,7 +230,7 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
imageMsg := message.ImageMessage{ imageMsg := message.ImageMessage{
Type: "image", Type: "image",
Data: message.ImageMessageData{ Data: message.ImageMessageData{
File: util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(record.SongHash + ".jpeg")), File: coverImageMap[record.SongHash],
}, },
} }
scoreMsg += imageMsg.ToCQString() + record.ToString() + "\n" scoreMsg += imageMsg.ToCQString() + record.ToString() + "\n"

View File

@ -408,7 +408,7 @@ func (p PlayerDataLite) IsDiffFrom(p2 PlayerDataLite) bool {
func (p PlayerData) ToString() string { 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 { if err != nil {
log.Default().Printf("下载头像失败url:%s,err:%v", p.Avatar, err) 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 { 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 { if err != nil {
log.Default().Printf("下载头像失败url:%s,err:%v", p.Avatar, err) log.Default().Printf("下载头像失败url:%s,err:%v", p.Avatar, err)
} }

View File

@ -224,7 +224,7 @@ func (p PlayerDataLite) IsDiffFrom(p2 PlayerDataLite) bool {
} }
func (p PlayerData) ToString() string { 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 { if err != nil {
log.Default().Printf("下载头像失败url:%s,err:%v", p.ProfilePicture, err) 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 { 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 { if err != nil {
log.Default().Printf("下载头像失败url:%s,err:%v", p.ProfilePicture, err) log.Default().Printf("下载头像失败url:%s,err:%v", p.ProfilePicture, err)
} }

View File

@ -28,7 +28,7 @@ func GenerateCongratulationImage(text string, inputFile, outputFile string, isGo
dc.DrawImage(im, 0, 0) dc.DrawImage(im, 0, 0)
// 判断是否为图片 // 判断是否为图片
if imgUrl, ok := isImageCQ(text); ok { if imgUrl, ok := isImageCQ(text); ok {
filePath, err := util.DownloadFile(imgUrl, "/tmp/qqbot") filePath, err := util.DownloadFile(imgUrl, "/tmp/qqbot", false)
if err != nil { if err != nil {
log.Print("无法下载图片:", err) log.Print("无法下载图片:", err)
return return

View File

@ -65,7 +65,7 @@ func normalizeURL(rawURL string) string {
} }
// DownloadFile 下载文件到指定目录,返回带有正确扩展名的完整文件路径 // DownloadFile 下载文件到指定目录,返回带有正确扩展名的完整文件路径
func DownloadFile(urlStr string, dirPath string) (filepath string, err error) { func DownloadFile(urlStr string, dirPath string, skipExist bool) (filepath string, err error) {
// 创建fasthttp客户端 // 创建fasthttp客户端
client := &fasthttp.Client{ client := &fasthttp.Client{
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,