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
}
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{

View File

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

View File

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

View File

@@ -3,6 +3,8 @@ package bot
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/celestix/gotgproto/dispatcher"
@@ -270,3 +272,19 @@ func HandleSilentAddTask(ctx *ext.Context, update *ext.Update, user *dao.User, t
})
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"
"syscall"
"slices"
"github.com/krau/SaveAny-Bot/bot"
"github.com/krau/SaveAny-Bot/common"
"github.com/krau/SaveAny-Bot/config"
@@ -29,12 +31,10 @@ func Run(_ *cobra.Command, _ []string) {
return
}
if config.Cfg.Temp.BasePath != "" && !config.Cfg.Stream {
for _, path := range []string{"/", ".", "\\", ".."} {
if filepath.Clean(config.Cfg.Temp.BasePath) == path {
if slices.Contains([]string{"/", ".", "\\", ".."}, filepath.Clean(config.Cfg.Temp.BasePath)) {
common.Log.Error("无效的缓存文件夹: ", config.Cfg.Temp.BasePath)
return
}
}
currentDir, err := os.Getwd()
if err != nil {
common.Log.Error("获取工作目录失败: ", err)

View File

@@ -3,6 +3,7 @@ package common
import (
"crypto/md5"
"encoding/hex"
"regexp"
)
func HashString(s string) string {
@@ -10,3 +11,16 @@ func HashString(s string) string {
hash.Write([]byte(s))
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
}