Documentation ¶
Index ¶
- Variables
- func CliDownload(args []string)
- 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 *os.File) error
- func (d Download) GetInfo() (*Info, error)
- func (d *Download) Init() (err error)
- func (d Download) IsRangeable() bool
- func (d *Download) Name() 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 ProgressFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ChunkPool = &sync.Pool{ New: func() interface{} { return new(Chunk) }, }
ChunkPool helps in multi *Download files.
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 HeaderSlice []GotHeader
var UserAgent = "Got/1.0"
UserAgent is the default Got user agent to send http requests.
Functions ¶
func CliDownload ¶
func CliDownload(args []string)
func GetFilename ¶
GetFilename it returns default file name from a URL.
Types ¶
type Chunk ¶
type Chunk struct { // Chunk start pos. Start uint64 // Chunk end pos. End uint64 // Path name where this chunk downloaded. Path string // Done to check is this chunk downloaded. Done chan struct{} }
Chunk is a partial content range.
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) 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.
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 ProgressFunc ¶
type ProgressFunc func(d *Download)
ProgressFunc to show progress state, called by RunProgress based on interval.