downloader

package
v0.143.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 21 Imported by: 39

Documentation

Overview

Package downloader contains downloading files helpers.

Index

Constants

View Source
const (
	RetryOperationGetFile         = "cdn.get_file"
	RetryOperationGetFileHashes   = "cdn.get_file_hashes"
	RetryOperationReupload        = "cdn.reupload"
	RetryOperationRefreshRedirect = "cdn.refresh_redirect"
	RetryOperationCreateClient    = "cdn.create_client"
	RetryOperationReaderChunk     = "reader.chunk"
	RetryOperationReaderHashes    = "reader.hashes"
)

Variables

View Source
var ErrHashMismatch = errors.New("file hash mismatch")

ErrHashMismatch means that download hash verification was failed.

Functions

This section is empty.

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, err error)

Parallel downloads file to given io.WriterAt.

func (*Builder) Stream added in v0.24.0

func (b *Builder) Stream(ctx context.Context, output io.Writer) (_ tg.StorageFileTypeClass, err error)

Stream downloads file to given io.Writer. NB: in this mode download can't be parallel.

func (*Builder) ToPath added in v0.24.0

func (b *Builder) ToPath(ctx context.Context, path string) (_ tg.StorageFileTypeClass, err error)

ToPath downloads file to given path.

func (*Builder) WithRetryHandler added in v0.142.0

func (b *Builder) WithRetryHandler(handler RetryHandler) *Builder

WithRetryHandler sets callback for transient download errors that are retried internally.

Handler can be called concurrently from download workers.

func (*Builder) WithThreads added in v0.24.0

func (b *Builder) WithThreads(threads int) *Builder

WithThreads sets downloading goroutines limit.

func (*Builder) WithVerify added in v0.24.0

func (b *Builder) WithVerify(verify bool) *Builder

WithVerify controls global hash verification behavior.

`true` enables classic verifier reader (preloads hash queue and validates all chunks, both legacy and CDN). `false` disables classic verifier reader.

If not called explicitly: - non-CDN path preserves old behavior (no upfront hash requests); - CDN path enables strict inline CDN verification after redirect.

Use WithVerify(true) to force verifier-queue mode on all paths.

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 CDNProvider added in v0.142.0

type CDNProvider interface {
	CDN(ctx context.Context, dc int, max int64) (CDN, io.Closer, error)
}

CDNProvider creates client bound to requested CDN DC. Returned closer is schema-scoped; for shared client-level pool adapters this can be a no-op closer.

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 NewDownloader

func NewDownloader() *Downloader

NewDownloader creates new Downloader.

func (*Downloader) Download

func (d *Downloader) Download(rpc Client, location tg.InputFileLocationClass) *Builder

Download creates Builder for plain downloads.

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) WithAllowCDN added in v0.142.0

func (d *Downloader) WithAllowCDN(allow bool) *Downloader

WithAllowCDN explicitly enables or disables CDN redirect flow.

This flag is explicit: if it is not set, downloader keeps legacy master-DC-only behavior and does not attempt CDN redirect handling. Client integration (`telegram.Client.Downloader`) sets this option from `telegram.Options.AllowCDN`.

func (*Downloader) WithPartSize

func (d *Downloader) WithPartSize(partSize int) *Downloader

WithPartSize sets chunk size. Must be divisible by 4KB.

See https://core.telegram.org/api/files#downloading-files.

func (*Downloader) WithRetryHandler added in v0.142.0

func (d *Downloader) WithRetryHandler(handler RetryHandler) *Downloader

WithRetryHandler sets callback for transient download errors that are retried internally by downloader.

Handler can be called concurrently from download workers.

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.

type RetryEvent added in v0.142.0

type RetryEvent struct {
	// Operation identifies retry source.
	Operation string
	// Attempt is 1-based counter inside current retry loop.
	Attempt int
	// Err is the error that triggered retry.
	Err error
}

RetryEvent describes retried transient downloader error.

type RetryHandler added in v0.142.0

type RetryHandler func(event RetryEvent)

RetryHandler is called for every downloader error that is retried internally.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL