mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-21 16:20:57 +08:00
refactor: simplify dler client interface
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
"github.com/celestix/gotgproto/types"
|
"github.com/celestix/gotgproto/types"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
"github.com/gotd/td/telegram/downloader"
|
||||||
"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"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
||||||
@@ -83,7 +84,7 @@ func GetFilesFromUpdateLinkMessageWithReplyEdit(ctx *ext.Context, update *ext.Up
|
|||||||
}
|
}
|
||||||
|
|
||||||
files = make([]tfile.TGFileMessage, 0, len(msgLinks))
|
files = make([]tfile.TGFileMessage, 0, len(msgLinks))
|
||||||
addFile := func(client tfile.DlerClient, msg *tg.Message) {
|
addFile := func(client downloader.Client, msg *tg.Message) {
|
||||||
if msg == nil || msg.Media == nil {
|
if msg == nil || msg.Media == nil {
|
||||||
logger.Warn("message is nil, skipping")
|
logger.Warn("message is nil, skipping")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -7,10 +7,6 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/pkg/consts/tglimit"
|
"github.com/krau/SaveAny-Bot/pkg/consts/tglimit"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DlerClient interface {
|
|
||||||
downloader.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDownloader(file TGFile) *downloader.Builder {
|
func NewDownloader(file TGFile) *downloader.Builder {
|
||||||
return downloader.NewDownloader().WithPartSize(tglimit.MaxPartSize).
|
return downloader.NewDownloader().WithPartSize(tglimit.MaxPartSize).
|
||||||
Download(file.Dler(), file.Location()).WithThreads(dlutil.BestThreads(file.Size(), config.Cfg.Threads))
|
Download(file.Dler(), file.Location()).WithThreads(dlutil.BestThreads(file.Size(), config.Cfg.Threads))
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ package tfile
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
|
"github.com/celestix/gotgproto/functions"
|
||||||
|
"github.com/gotd/td/telegram/downloader"
|
||||||
"github.com/gotd/td/tg"
|
"github.com/gotd/td/tg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TGFile interface {
|
type TGFile interface {
|
||||||
Location() tg.InputFileLocationClass
|
Location() tg.InputFileLocationClass
|
||||||
Dler() DlerClient // witch client to use for downloading
|
Dler() downloader.Client // witch client to use for downloading
|
||||||
Size() int64
|
Size() int64
|
||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ type tgFile struct {
|
|||||||
size int64
|
size int64
|
||||||
name string
|
name string
|
||||||
message *tg.Message
|
message *tg.Message
|
||||||
dler DlerClient
|
dler downloader.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *tgFile) Location() tg.InputFileLocationClass {
|
func (f *tgFile) Location() tg.InputFileLocationClass {
|
||||||
@@ -44,13 +45,13 @@ func (f *tgFile) Message() *tg.Message {
|
|||||||
return f.message
|
return f.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *tgFile) Dler() DlerClient {
|
func (f *tgFile) Dler() downloader.Client {
|
||||||
return f.dler
|
return f.dler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTGFile(
|
func NewTGFile(
|
||||||
location tg.InputFileLocationClass,
|
location tg.InputFileLocationClass,
|
||||||
dler DlerClient,
|
dler downloader.Client,
|
||||||
size int64,
|
size int64,
|
||||||
name string,
|
name string,
|
||||||
opts ...TGFileOptions,
|
opts ...TGFileOptions,
|
||||||
@@ -67,7 +68,7 @@ func NewTGFile(
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromMedia(media tg.MessageMediaClass, client DlerClient, opts ...TGFileOptions) (TGFile, error) {
|
func FromMedia(media tg.MessageMediaClass, client downloader.Client, opts ...TGFileOptions) (TGFile, error) {
|
||||||
switch m := media.(type) {
|
switch m := media.(type) {
|
||||||
case *tg.MessageMediaDocument:
|
case *tg.MessageMediaDocument:
|
||||||
document, ok := m.Document.AsNotEmpty()
|
document, ok := m.Document.AsNotEmpty()
|
||||||
@@ -108,7 +109,10 @@ func FromMedia(media tg.MessageMediaClass, client DlerClient, opts ...TGFileOpti
|
|||||||
location.AccessHash = photo.GetAccessHash()
|
location.AccessHash = photo.GetAccessHash()
|
||||||
location.FileReference = photo.GetFileReference()
|
location.FileReference = photo.GetFileReference()
|
||||||
location.ThumbSize = size.GetType()
|
location.ThumbSize = size.GetType()
|
||||||
fileName := fmt.Sprintf("photo_%s_%d.jpg", time.Now().Format("2006-01-02_15-04-05"), photo.GetID())
|
fileName, err := functions.GetMediaFileName(m)
|
||||||
|
if err != nil {
|
||||||
|
fileName = fmt.Sprintf("photo_%d.png", photo.GetID())
|
||||||
|
}
|
||||||
file := NewTGFile(
|
file := NewTGFile(
|
||||||
location,
|
location,
|
||||||
client,
|
client,
|
||||||
@@ -121,7 +125,7 @@ func FromMedia(media tg.MessageMediaClass, client DlerClient, opts ...TGFileOpti
|
|||||||
return nil, fmt.Errorf("unsupported media type: %T", media)
|
return nil, fmt.Errorf("unsupported media type: %T", media)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromMediaMessage(media tg.MessageMediaClass, client DlerClient, msg *tg.Message, opts ...TGFileOptions) (TGFileMessage, error) {
|
func FromMediaMessage(media tg.MessageMediaClass, client downloader.Client, msg *tg.Message, opts ...TGFileOptions) (TGFileMessage, error) {
|
||||||
file, err := FromMedia(media, client, opts...)
|
file, err := FromMedia(media, client, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user