media/transfer: add function to directly download bytes
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user