Documentation ¶
Overview ¶
Package downloader contains downloading files helpers.
Index ¶
- Variables
- func ExpandThumbnail(data, to []byte) ([]byte, error)
- type Builder
- func (b *Builder) Parallel(ctx context.Context, output io.WriterAt) (tg.StorageFileTypeClass, error)
- func (b *Builder) Stream(ctx context.Context, output io.Writer) (tg.StorageFileTypeClass, error)
- func (b *Builder) ToPath(ctx context.Context, path string) (_ tg.StorageFileTypeClass, err error)
- func (b *Builder) WithThreads(threads int) *Builder
- func (b *Builder) WithVerify(verify bool) *Builder
- type CDN
- type Client
- type Downloader
- func (d *Downloader) CDN(rpc Client, cdnRPC CDN, redirect *tg.UploadFileCDNRedirect) *Builder
- func (d *Downloader) Download(rpc Client, location tg.InputFileLocationClass) *Builder
- func (d *Downloader) DownloadDirect(rpc Client, location tg.InputFileLocationClass) *Builder
- func (d *Downloader) Web(rpc Client, location tg.InputWebFileLocationClass) *Builder
- func (d *Downloader) WithPartSize(partSize int) *Downloader
- type ExpiredTokenError
- type RedirectError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrHashMismatch = xerrors.New("file hash mismatch")
ErrHashMismatch means that download hash verification was failed.
Functions ¶
func ExpandThumbnail ¶ added in v0.41.0
ExpandThumbnail creates a JPG payload from stripped thumbnail bytes.
See https://core.telegram.org/api/files#stripped-thumbnails for reference.
Based on tdesktop implementation. See https://github.com/telegramdesktop/tdesktop/blob/v2.7.5/Telegram/SourceFiles/ui/image/image.cpp#L43.
Example ¶
package main import ( "bytes" "encoding/base64" "fmt" "image/jpeg" "image/png" "github.com/gotd/td/telegram/downloader" "github.com/gotd/td/tg" ) func main() { userPhoto := tg.UserProfilePhoto{ StrippedThumb: []uint8{ 0x01, 0x28, 0x28, 0x29, 0x40, 0x1d, 0xff, 0x00, 0x2a, 0x41, 0x8e, 0xe3, 0x22, 0xac, 0x42, 0x51, 0xa0, 0x28, 0x03, 0x19, 0x1b, 0x9e, 0x07, 0x3f, 0xad, 0x69, 0x51, 0xbd, 0x91, 0xcf, 0x42,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xe4, 0xd1, 0x45, 0x32, 0x6e, 0x3e, 0x19, 0x5a, 0x27, 0xdc, 0x3a, 0x77, 0x1e, 0xb5, 0x62, 0xe2, 0xe5, 0x1e, 0x1d, 0xa9, 0xd4, 0xf5, 0xe3, 0xa5, 0x14, 0x52, 0x71, 0x4d, 0xdc, 0xa5, 0x36, 0x95, 0x8a, 0x74, 0x51, 0x45, 0x32, 0x4f, }, } result, err := downloader.ExpandThumbnail(userPhoto.StrippedThumb, nil) if err != nil { panic(err) } img, err := jpeg.Decode(bytes.NewReader(result)) if err != nil { panic(err) } var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { panic(err) } fmt.Println("IMAGE:" + base64.StdEncoding.EncodeToString(buf.Bytes())) }
Output:
Types ¶
type Builder ¶ added in v0.24.0
type Builder struct {
// contains filtered or unexported fields
}
Builder is a download builder.
func (*Builder) Parallel ¶ added in v0.24.0
func (b *Builder) Parallel(ctx context.Context, output io.WriterAt) (tg.StorageFileTypeClass, error)
Parallel downloads file to given io.WriterAt.
func (*Builder) Stream ¶ added in v0.24.0
Stream downloads file to given io.Writer. NB: in this mode download can't be parallel.
func (*Builder) WithThreads ¶ added in v0.24.0
WithThreads sets downloading goroutines limit.
func (*Builder) WithVerify ¶ added in v0.24.0
WithVerify sets verify parameter. If verify is true, file hashes will be checked Verify is true by default for CDN downloads.
type CDN ¶ added in v0.24.0
type CDN interface {
UploadGetCDNFile(ctx context.Context, request *tg.UploadGetCDNFileRequest) (tg.UploadCDNFileClass, error)
}
CDN represents Telegram RPC client to CDN server.
type Client ¶ added in v0.24.0
type Client interface { UploadGetFile(ctx context.Context, request *tg.UploadGetFileRequest) (tg.UploadFileClass, error) UploadGetFileHashes(ctx context.Context, request *tg.UploadGetFileHashesRequest) ([]tg.FileHash, error) UploadReuploadCDNFile(ctx context.Context, request *tg.UploadReuploadCDNFileRequest) ([]tg.FileHash, error) UploadGetCDNFileHashes(ctx context.Context, request *tg.UploadGetCDNFileHashesRequest) ([]tg.FileHash, error) UploadGetWebFile(ctx context.Context, request *tg.UploadGetWebFileRequest) (*tg.UploadWebFile, error) }
Client represents Telegram RPC client.
type Downloader ¶
type Downloader struct {
// contains filtered or unexported fields
}
Downloader is Telegram file downloader.
func (*Downloader) CDN ¶ added in v0.24.0
func (d *Downloader) CDN(rpc Client, cdnRPC CDN, redirect *tg.UploadFileCDNRedirect) *Builder
CDN creates Builder for CDN downloads.
func (*Downloader) Download ¶
func (d *Downloader) Download(rpc Client, location tg.InputFileLocationClass) *Builder
Download creates Builder for plain downloads.
Method sets CDNSupported field for upload.getFile. Use DownloadDirect for direct downloads without CDN support.
func (*Downloader) DownloadDirect ¶ added in v0.24.0
func (d *Downloader) DownloadDirect(rpc Client, location tg.InputFileLocationClass) *Builder
DownloadDirect creates Builder for plain downloads with disabled CDN redirect handling.
func (*Downloader) Web ¶ added in v0.24.0
func (d *Downloader) Web(rpc Client, location tg.InputWebFileLocationClass) *Builder
Web creates Builder for web files downloads.
func (*Downloader) WithPartSize ¶
func (d *Downloader) WithPartSize(partSize int) *Downloader
WithPartSize sets chunk size. Must be divisible by 4KB.
type ExpiredTokenError ¶ added in v0.24.0
type ExpiredTokenError struct {
*tg.UploadCDNFileReuploadNeeded
}
ExpiredTokenError error is returned when Downloader get expired file token for CDN. See https://core.telegram.org/constructor/upload.fileCdnRedirect.
func (*ExpiredTokenError) Error ¶ added in v0.24.0
func (r *ExpiredTokenError) Error() string
Error implements error interface.
type RedirectError ¶
type RedirectError struct {
Redirect *tg.UploadFileCDNRedirect
}
RedirectError error is returned when Downloader get CDN redirect. See https://core.telegram.org/constructor/upload.fileCdnRedirect.
func (*RedirectError) Error ¶
func (r *RedirectError) Error() string
Error implements error interface.