feat: implement GenFileNameFromMessage function for improved file naming

This commit is contained in:
krau
2025-03-22 09:33:50 +08:00
parent 635f00ac71
commit 19efab0665
6 changed files with 40 additions and 12 deletions

View File

@@ -47,7 +47,7 @@ func handleFileMessage(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups return dispatcher.EndGroups
} }
if file.FileName == "" { if file.FileName == "" {
file.FileName = fmt.Sprintf("%d_%d_%s", update.EffectiveChat().GetID(), update.EffectiveMessage.ID, file.Hash()) file.FileName = GenFileNameFromMessage(*update.EffectiveMessage.Message, file)
} }
if err := dao.SaveReceivedFile(&dao.ReceivedFile{ if err := dao.SaveReceivedFile(&dao.ReceivedFile{

View File

@@ -1,7 +1,6 @@
package bot package bot
import ( import (
"fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -72,10 +71,8 @@ func handleLinkMessage(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString("获取文件失败: "+err.Error()), nil) ctx.Reply(update, ext.ReplyTextString("获取文件失败: "+err.Error()), nil)
return dispatcher.EndGroups return dispatcher.EndGroups
} }
// TODO: Better file name
if file.FileName == "" { if file.FileName == "" {
common.Log.Warnf("文件名为空,使用生成的名称") file.FileName = GenFileNameFromMessage(*update.EffectiveMessage.Message, file)
file.FileName = fmt.Sprintf("%d_%d_%s", linkChat.GetID(), messageID, file.Hash())
} }
receivedFile := &dao.ReceivedFile{ receivedFile := &dao.ReceivedFile{

View File

@@ -76,9 +76,8 @@ func saveCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups return dispatcher.EndGroups
} }
// TODO: better file name
if file.FileName == "" { if file.FileName == "" {
file.FileName = fmt.Sprintf("%d_%d_%s", update.EffectiveChat().GetID(), replyToMsgID, file.Hash()) file.FileName = GenFileNameFromMessage(*msg, file)
} }
receivedFile := &dao.ReceivedFile{ receivedFile := &dao.ReceivedFile{
Processing: false, Processing: false,

View File

@@ -3,6 +3,8 @@ package bot
import ( import (
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings"
"time" "time"
"github.com/celestix/gotgproto/dispatcher" "github.com/celestix/gotgproto/dispatcher"
@@ -270,3 +272,19 @@ func HandleSilentAddTask(ctx *ext.Context, update *ext.Update, user *dao.User, t
}) })
return dispatcher.EndGroups return dispatcher.EndGroups
} }
func GenFileNameFromMessage(message tg.Message, file *types.File) string {
if file.FileName != "" {
return file.FileName
}
text := strings.TrimSpace(message.GetMessage())
if text == "" {
return file.Hash()
}
tags := common.ExtractTagsFromText(text)
if len(tags) > 0 {
return fmt.Sprintf("%s_%s", strings.Join(tags, "_"), strconv.Itoa(message.GetID()))
}
runes := []rune(text)
return string(runes[:min(128, len(runes))])
}

View File

@@ -7,6 +7,8 @@ import (
"path/filepath" "path/filepath"
"syscall" "syscall"
"slices"
"github.com/krau/SaveAny-Bot/bot" "github.com/krau/SaveAny-Bot/bot"
"github.com/krau/SaveAny-Bot/common" "github.com/krau/SaveAny-Bot/common"
"github.com/krau/SaveAny-Bot/config" "github.com/krau/SaveAny-Bot/config"
@@ -29,11 +31,9 @@ func Run(_ *cobra.Command, _ []string) {
return return
} }
if config.Cfg.Temp.BasePath != "" && !config.Cfg.Stream { if config.Cfg.Temp.BasePath != "" && !config.Cfg.Stream {
for _, path := range []string{"/", ".", "\\", ".."} { if slices.Contains([]string{"/", ".", "\\", ".."}, filepath.Clean(config.Cfg.Temp.BasePath)) {
if filepath.Clean(config.Cfg.Temp.BasePath) == path { common.Log.Error("无效的缓存文件夹: ", config.Cfg.Temp.BasePath)
common.Log.Error("无效的缓存文件夹: ", config.Cfg.Temp.BasePath) return
return
}
} }
currentDir, err := os.Getwd() currentDir, err := os.Getwd()
if err != nil { if err != nil {

View File

@@ -3,6 +3,7 @@ package common
import ( import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"regexp"
) )
func HashString(s string) string { func HashString(s string) string {
@@ -10,3 +11,16 @@ func HashString(s string) string {
hash.Write([]byte(s)) hash.Write([]byte(s))
return hex.EncodeToString(hash.Sum(nil)) return hex.EncodeToString(hash.Sum(nil))
} }
var TagRe = regexp.MustCompile(`(?:^|[\p{Zs}\s.,!?(){}[\]<>\"\',。!?():;、])#([\p{L}\d_]+)`)
func ExtractTagsFromText(text string) []string {
matches := TagRe.FindAllStringSubmatch(text, -1)
tags := make([]string, 0)
for _, match := range matches {
if len(match) > 1 {
tags = append(tags, match[1])
}
}
return tags
}