Documentation ¶
Index ¶
- Variables
- func GetFilename(URL string) string
- func NewRequest(ctx context.Context, method, URL string, header []GotHeader) (req *http.Request, err error)
- type Chunk
- type Download
- func (d *Download) AvgSpeed() uint64
- func (d *Download) Context() context.Context
- func (d *Download) DownloadChunk(c *Chunk, dest io.Writer) error
- func (d *Download) GetInfoOrDownload() (*Info, error)
- func (d *Download) Init() (err error)
- func (d *Download) IsRangeable() bool
- func (d *Download) Path() string
- func (d *Download) RunProgress(fn ProgressFunc)
- func (d *Download) Size() uint64
- func (d *Download) Speed() uint64
- func (d *Download) Start() (err error)
- func (d *Download) TotalCost() time.Duration
- func (d *Download) TotalSize() uint64
- func (d *Download) Write(b []byte) (int, error)
- type Got
- type GotHeader
- type Info
- type OffsetWriter
- type ProgressFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultClient = &http.Client{ Transport: &http.Transport{ MaxIdleConns: 10, IdleConnTimeout: 30 * time.Second, TLSHandshakeTimeout: 5 * time.Second, Proxy: http.ProxyFromEnvironment, }, }
DefaultClient is the default http client for got requests.
var DefaultFileName = "got.output"
DefaultFileName is the fallback name for GetFilename.
var ErrDownloadAborted = errors.New("Operation aborted")
ErrDownloadAborted - When download is aborted by the OS before it is completed, ErrDownloadAborted will be triggered
var UserAgent = "Got/1.0"
UserAgent is the default Got user agent to send http requests.
Functions ¶
func GetFilename ¶
GetFilename it returns default file name from a URL.
Types ¶
type Download ¶
type Download struct { Client *http.Client Concurrency uint URL, Dir, Dest string Interval, ChunkSize, MinChunkSize, MaxChunkSize uint64 Header []GotHeader StopProgress bool // contains filtered or unexported fields }
Download holds downloadable file config and infos.
Example ¶
// Just for testing destPath := createTemp() defer clean(destPath) ctx := context.Background() dl := got.NewDownload(ctx, testUrl, destPath) // Init if err := dl.Init(); err != nil { fmt.Println(err) } // Start download if err := dl.Start(); err != nil { fmt.Println(err) } fmt.Println("Done")
Output: Done
func NewDownload ¶
NewDownload returns new *Download with context.
func (*Download) DownloadChunk ¶
DownloadChunk downloads a file chunk.
func (*Download) GetInfoOrDownload ¶
Try downloading the first byte of the file using a range request. If the server supports range requests, then we'll extract the length info from content-range, Otherwise this just downloads the whole file in one go
func (*Download) Init ¶
Init set defaults and split file into chunks and gets Info, you should call Init before Start
func (*Download) IsRangeable ¶
IsRangeable returns file server partial content support state.
func (*Download) RunProgress ¶
func (d *Download) RunProgress(fn ProgressFunc)
RunProgress runs ProgressFunc based on Interval and updates lastSize.
func (*Download) Start ¶
Start downloads the file chunks, and merges them. Must be called only after init
type Got ¶
type Got struct { ProgressFunc Client *http.Client // contains filtered or unexported fields }
Got holds got download config.
Example ¶
// Just for testing destPath := createTemp() defer clean(destPath) g := got.New() err := g.Download(testUrl, destPath) if err != nil { log.Fatal(err) return } fmt.Println("done")
Output: done
Example (WithContext) ¶
// Just for testing destPath := createTemp() defer clean(destPath) ctx := context.Background() g := got.NewWithContext(ctx) err := g.Download(testUrl, destPath) if err != nil { log.Fatal(err) return } fmt.Println("done")
Output: done
func NewWithContext ¶
NewWithContext wants Context and returns *Got with default http client.
type OffsetWriter ¶
type ProgressFunc ¶
type ProgressFunc func(d *Download)
ProgressFunc to show progress state, called by RunProgress based on interval.