package telegram
import (
"context"
"io"
"sync"
"github.com/go-faster/errors"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/gotd/log"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
)
type downloadClient struct {
client *Client
raw *tg .Client
}
func (d downloadClient ) api () *tg .Client {
if d .raw != nil {
return d .raw
}
return d .client .API ()
}
func (d downloadClient ) UploadGetFile (
ctx context .Context ,
request *tg .UploadGetFileRequest ,
) (tg .UploadFileClass , error ) {
resp , err := d .api ().UploadGetFile (ctx , request )
return resp , err
}
func (d downloadClient ) UploadGetFileHashes (
ctx context .Context ,
request *tg .UploadGetFileHashesRequest ,
) ([]tg .FileHash , error ) {
resp , err := d .api ().UploadGetFileHashes (ctx , request )
return resp , err
}
func (d downloadClient ) UploadReuploadCDNFile (
ctx context .Context ,
request *tg .UploadReuploadCDNFileRequest ,
) ([]tg .FileHash , error ) {
resp , err := d .api ().UploadReuploadCDNFile (ctx , request )
return resp , err
}
func (d downloadClient ) UploadGetCDNFileHashes (
ctx context .Context ,
request *tg .UploadGetCDNFileHashesRequest ,
) ([]tg .FileHash , error ) {
resp , err := d .api ().UploadGetCDNFileHashes (ctx , request )
return resp , err
}
func (d downloadClient ) UploadGetWebFile (
ctx context .Context ,
request *tg .UploadGetWebFileRequest ,
) (*tg .UploadWebFile , error ) {
return d .api ().UploadGetWebFile (ctx , request )
}
func (d downloadClient ) CDN (ctx context .Context , dc int , max int64 ) (downloader .CDN , io .Closer , error ) {
return cdnProvider {client : d .client }.CDN (ctx , dc , max )
}
type cdnProvider struct {
client *Client
}
func (p cdnProvider ) CDN (ctx context .Context , dc int , max int64 ) (downloader .CDN , io .Closer , error ) {
invoker , err := p .client .CDN (ctx , dc , max )
if err != nil {
return nil , nil , err
}
if invoker == nil {
return nil , nil , errors .New ("telegram CDN pool returned nil invoker" )
}
cdnClient := tg .NewClient (invoker )
return cdnClient , invoker , nil
}
func (c *Client ) Downloader () *downloader .Downloader {
if c .downloader == nil {
return downloader .NewDownloader ().WithAllowCDN (c .allowCDN )
}
return c .downloader .Clone ()
}
func newClientDownloader (allowCDN bool , mp metric .MeterProvider , lg log .Logger ) *downloader .Downloader {
d := downloader .NewDownloader ().WithAllowCDN (allowCDN )
if mp != nil {
d = d .WithMeterProvider (mp )
}
if lg != nil && lg != log .Nop {
d = d .WithLogger (log .Named (lg , "downloader" ))
}
return d
}
func (c *Client ) homeDownloader () *downloader .Downloader {
d := c .Downloader ()
if c .downloadSchedulers == nil || c .session == nil {
return d
}
return d .WithScheduler (c .DownloadScheduler (c .session .Load ().DC ))
}
func (c *Client ) DownloadScheduler (dc int ) *downloader .Scheduler {
return c .downloadSchedulers .get (dc )
}
const downloaderMeterName = "github.com/gotd/td/telegram/downloader"
type downloadSchedulers struct {
opts downloader .SchedulerOptions
meter metric .Meter
mux sync .Mutex
m map [int ]*downloader .Scheduler
regs map [int ]metric .Registration
}
func newDownloadSchedulers (opts *downloader .SchedulerOptions , mp metric .MeterProvider ) *downloadSchedulers {
if opts == nil {
return nil
}
d := &downloadSchedulers {
opts : *opts ,
m : map [int ]*downloader .Scheduler {},
regs : map [int ]metric .Registration {},
}
if mp != nil {
d .meter = mp .Meter (downloaderMeterName )
}
return d
}
func (d *downloadSchedulers ) get (dc int ) *downloader .Scheduler {
if d == nil {
return nil
}
d .mux .Lock ()
defer d .mux .Unlock ()
s , ok := d .m [dc ]
if !ok {
s = downloader .NewScheduler (d .opts )
d .m [dc ] = s
}
if _ , ok := d .regs [dc ]; !ok && d .meter != nil {
if reg , err := s .Instrument (d .meter , attribute .Int ("dc" , dc )); err == nil {
d .regs [dc ] = reg
}
}
return s
}
func (d *downloadSchedulers ) unregister () {
if d == nil {
return
}
d .mux .Lock ()
defer d .mux .Unlock ()
for dc , reg := range d .regs {
_ = reg .Unregister ()
delete (d .regs , dc )
}
}
func (d *downloadSchedulers ) registered () int {
if d == nil {
return 0
}
d .mux .Lock ()
defer d .mux .Unlock ()
return len (d .regs )
}
func (c *Client ) CDNProvider () downloader .CDNProvider {
return cdnProvider {client : c }
}
func (c *Client ) Download (location tg .InputFileLocationClass ) *downloader .Builder {
return c .homeDownloader ().Download (downloadClient {client : c }, location )
}
func (c *Client ) DownloadVia (invoker tg .Invoker , location tg .InputFileLocationClass ) *downloader .Builder {
return c .Downloader ().Download (downloadClient {client : c , raw : tg .NewClient (invoker )}, location )
}
func (c *Client ) DownloadWeb (location tg .InputWebFileLocationClass ) *downloader .Builder {
return c .homeDownloader ().Web (downloadClient {client : c }, location )
}
func (c *Client ) DownloadWebVia (invoker tg .Invoker , location tg .InputWebFileLocationClass ) *downloader .Builder {
return c .Downloader ().Web (downloadClient {client : c , raw : tg .NewClient (invoker )}, location )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .