From a573740b9aaa60dea211162bb0ab956778741008 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Tue, 22 Oct 2024 09:21:56 -0600 Subject: [PATCH] media/transfer: add function to directly download bytes Signed-off-by: Sumner Evans --- pkg/connector/chatinfo.go | 3 +-- pkg/connector/client.go | 3 +-- pkg/connector/media/transfer.go | 10 ++++++++-- pkg/connector/telegram.go | 3 +-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/connector/chatinfo.go b/pkg/connector/chatinfo.go index 9aed7bf4..798f3249 100644 --- a/pkg/connector/chatinfo.go +++ b/pkg/connector/chatinfo.go @@ -151,8 +151,7 @@ func (t *TelegramClient) avatarFromPhoto(photo tg.PhotoClass) *bridgev2.Avatar { return &bridgev2.Avatar{ ID: ids.MakeAvatarID(photo.GetID()), Get: func(ctx context.Context) (data []byte, err error) { - data, _, err = media.NewTransferer(t.client.API()).WithPhoto(photo).Download(ctx) - return + return media.NewTransferer(t.client.API()).WithPhoto(photo).DownloadBytes(ctx) }, } } diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 1f7fd178..efdc1eaa 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -556,8 +556,7 @@ func (t *TelegramClient) getUserInfoFromTelegramUser(ctx context.Context, u tg.U if err != nil { return nil, err } - data, _, err = transferer.Download(ctx) - return + return transferer.DownloadBytes(ctx) }, } } diff --git a/pkg/connector/media/transfer.go b/pkg/connector/media/transfer.go index c5627f8e..0e200085 100644 --- a/pkg/connector/media/transfer.go +++ b/pkg/connector/media/transfer.go @@ -83,7 +83,6 @@ func getLocationID(loc any) (locID store.TelegramFileLocationID) { // Transferer is a utility for downloading media from Telegram and uploading it // to Matrix. -// TODO better name? type Transferer struct { client downloader.Client @@ -247,7 +246,7 @@ func (t *ReadyTransferer) Transfer(ctx context.Context, store *store.Container, return file.MXC, nil, &t.inner.fileInfo, nil } - data, _, err := t.Download(ctx) + data, err := t.DownloadBytes(ctx) if err != nil { return "", nil, nil, fmt.Errorf("downloading file failed: %w", err) } @@ -344,6 +343,13 @@ func (t *ReadyTransferer) Download(ctx context.Context) ([]byte, *event.FileInfo return buf.Bytes(), &t.inner.fileInfo, nil } +// DownloadBytes downloads the media from Telegram to a byte buffer. +func (t *ReadyTransferer) DownloadBytes(ctx context.Context) ([]byte, error) { + var buf bytes.Buffer + _, err := downloader.NewDownloader().Download(t.inner.client, t.loc).Stream(ctx, &buf) + return buf.Bytes(), err +} + // DirectDownloadURL returns the direct download URL for the media. func (t *ReadyTransferer) DirectDownloadURL(ctx context.Context, loggedInUserID int64, portal *bridgev2.Portal, msgID int, thumbnail bool, telegramMediaID int64) (id.ContentURIString, *event.FileInfo, error) { peerType, chatID, err := ids.ParsePortalID(portal.ID) diff --git a/pkg/connector/telegram.go b/pkg/connector/telegram.go index 77b98a27..dd187856 100644 --- a/pkg/connector/telegram.go +++ b/pkg/connector/telegram.go @@ -477,8 +477,7 @@ func (t *TelegramClient) updateChannel(ctx context.Context, channel *tg.Channel) avatar = &bridgev2.Avatar{ ID: ids.MakeAvatarID(photo.PhotoID), Get: func(ctx context.Context) (data []byte, err error) { - data, _, err = media.NewTransferer(t.client.API()).WithChannelPhoto(channel.ID, channel.AccessHash, photo.PhotoID).Download(ctx) - return + return media.NewTransferer(t.client.API()).WithChannelPhoto(channel.ID, channel.AccessHash, photo.PhotoID).DownloadBytes(ctx) }, } }