From 46c21b77e95f76ff10e4a7be3fbd5e8a1159e968 Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Sun, 24 Aug 2025 12:02:27 +0800 Subject: [PATCH] feat: enhance Save method to validate chat ID and adjust ForceFile logic for image uploads --- pkg/consts/tglimit/tglimit.go | 1 + storage/telegram/telegram.go | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/consts/tglimit/tglimit.go b/pkg/consts/tglimit/tglimit.go index e5432fd..94c89db 100644 --- a/pkg/consts/tglimit/tglimit.go +++ b/pkg/consts/tglimit/tglimit.go @@ -7,4 +7,5 @@ import ( const ( MaxPartSize = 1024 * 1024 MaxUploadPartSize = uploader.MaximumPartSize + MaxPhotoSize = 10 * 1024 * 1024 ) diff --git a/storage/telegram/telegram.go b/storage/telegram/telegram.go index ad42aba..a19ebd8 100644 --- a/storage/telegram/telegram.go +++ b/storage/telegram/telegram.go @@ -10,6 +10,7 @@ import ( "github.com/charmbracelet/log" "github.com/duke-git/lancet/v2/slice" + "github.com/duke-git/lancet/v2/validator" "github.com/gabriel-vasile/mimetype" "github.com/gotd/td/constant" "github.com/gotd/td/telegram/message" @@ -87,7 +88,7 @@ func (t *Telegram) Save(ctx context.Context, r io.Reader, storagePath string) er if len(parts) >= 1 { filename = parts[len(parts)-1] } - if len(parts) >= 2 { + if len(parts) >= 2 && validator.IsAlphaNumeric(parts[0]) { cid, err := tgutil.ParseChatID(tctx, parts[0]) if err != nil { // id不合法时使用配置文件中的 chat_id @@ -141,9 +142,13 @@ func (t *Telegram) Save(ctx context.Context, r io.Reader, storagePath string) er return fmt.Errorf("failed to upload file to telegram: %w", err) } caption := styling.Plain(filename) + forceFile := t.config.ForceFile + if strings.HasPrefix(mtype.String(), "image/") && size >= tglimit.MaxPhotoSize { + forceFile = true + } docb := message.UploadedDocument(file, caption). Filename(filename). - ForceFile(t.config.ForceFile). + ForceFile(forceFile). MIME(mtype.String()) var media message.MediaOption = docb