mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-07 23:21:23 +08:00
refactor: replace functions.GetMediaFileNameWithId with tgutil.GetMediaFileName for better media file name handling
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/celestix/gotgproto/dispatcher"
|
"github.com/celestix/gotgproto/dispatcher"
|
||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
"github.com/celestix/gotgproto/functions"
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gotd/td/tg"
|
"github.com/gotd/td/tg"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
|
||||||
@@ -158,9 +157,8 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
|
|||||||
sb.Reset()
|
sb.Reset()
|
||||||
sb.WriteString(msg.GetMessage())
|
sb.WriteString(msg.GetMessage())
|
||||||
sb.WriteString(" ")
|
sb.WriteString(" ")
|
||||||
fn, _ := functions.GetMediaFileNameWithId(media)
|
fn, _ := tgutil.GetMediaFileName(media)
|
||||||
sb.WriteString(fn)
|
sb.WriteString(fn)
|
||||||
log.FromContext(ctx).Debugf("正在检查消息内容: %s", sb.String())
|
|
||||||
if !filter.MatchString(sb.String()) {
|
if !filter.MatchString(sb.String()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -194,5 +192,4 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
|
|||||||
return dispatcher.EndGroups
|
return dispatcher.EndGroups
|
||||||
}
|
}
|
||||||
return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, update.GetUserChat().GetID(), stor, "", files, replied.ID)
|
return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, update.GetUserChat().GetID(), stor, "", files, replied.ID)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
40
common/utils/tgutil/media.go
Normal file
40
common/utils/tgutil/media.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package tgutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
|
"github.com/gotd/td/tg"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetMediaFileName(media tg.MessageMediaClass) (string, error) {
|
||||||
|
switch v := media.(type) {
|
||||||
|
case *tg.MessageMediaPhoto:
|
||||||
|
f, ok := v.Photo.AsNotEmpty()
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("unknown type media: %T", media)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d.png", f.ID), nil
|
||||||
|
case *tg.MessageMediaDocument:
|
||||||
|
f, ok := v.Document.AsNotEmpty()
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("unknown type media: %T", media)
|
||||||
|
}
|
||||||
|
fileName := ""
|
||||||
|
for _, attribute := range f.Attributes {
|
||||||
|
if name, ok := attribute.(*tg.DocumentAttributeFilename); ok {
|
||||||
|
fileName = name.GetFileName()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fileName == "" {
|
||||||
|
mmt := mimetype.Lookup(f.GetMimeType())
|
||||||
|
if mmt != nil {
|
||||||
|
fileName = fmt.Sprintf("%d.%s", f.GetID(), mmt.Extension())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fileName, nil
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("unsupported type media: %T", media)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
"github.com/celestix/gotgproto/functions"
|
|
||||||
"github.com/duke-git/lancet/v2/maputil"
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/mathutil"
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
@@ -86,7 +85,7 @@ func GenFileNameFromMessage(message tg.Message) string {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
mname, err := functions.GetMediaFileNameWithId(message.Media)
|
mname, err := GetMediaFileName(message.Media)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
filename = fmt.Sprintf("%d_%s", message.GetID(), xid.New().String())
|
filename = fmt.Sprintf("%d_%s", message.GetID(), xid.New().String())
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user