Documentation
¶
Index ¶
- func Download(ctx context.Context, c *http.Client, req *http.Request, dst string) (n int64, err error)
- func DownloadBuffer(ctx context.Context, c *http.Client, req *http.Request, dst string, buf []byte) (n int64, err error)
- func DownloadBufferWithProgress(ctx context.Context, c *http.Client, req *http.Request, dst string, buf []byte, ...) (n int64, err error)
- func DownloadWithProgress(ctx context.Context, c *http.Client, req *http.Request, dst string, ...) (n int64, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Download ¶ added in v0.0.7
func Download(ctx context.Context, c *http.Client, req *http.Request, dst string) (n int64, err error)
Download does an HTTP request to download the content to a local file and returns the number of bytes downloaded. It accepts context.Context to make download cancalable.
func DownloadBuffer ¶ added in v0.0.14
func DownloadBuffer(ctx context.Context, c *http.Client, req *http.Request, dst string, buf []byte) (n int64, err error)
DownloadBuffer is the buffered version of Download.
func DownloadBufferWithProgress ¶ added in v0.0.17
func DownloadBufferWithProgress( ctx context.Context, c *http.Client, req *http.Request, dst string, buf []byte, downloaded int64, fn func(total, prev, current int64, percent float32)) (n int64, err error)
DownloadBufferWithProgress does an HTTP request to download the content to a local file and returns the number of bytes downloaded. It accepts context.Context to make download cancalable. It also accepts callback function on bytes written to report progress. downloaded: number of bytes downloaded previously. It can be used to resume the download. 1. Set downloaded to 0 when call DownloadBufferWithProgress for the first time. 2. User stops the download and DownloadBufferWithProgress returns the number of bytes downloaded and error. 3. Check if err == context.Canceled || err == context.DeadlineExceeded. 4. Set downloaded to the "n" return value of previous DownloadBufferWithProgress when make next call to resume the download. fn: callback on bytes written.
Example ¶
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/northbright/download"
"github.com/northbright/pathelper"
)
func main() {
url := "https://golang.google.cn/dl/go1.23.1.darwin-amd64.pkg"
c := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
// Set User-Agent.
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76")
dst := filepath.Join(os.TempDir(), "go", "go1.23.1.darwin-amd64.pkg")
// Create parent dir of dst if it does not exist.
dir := filepath.Dir(dst)
if err := pathelper.CreateDirIfNotExists(dir, 0755); err != nil {
log.Printf("create dir(%s) error: %v", dir, err)
return
}
log.Printf("create dir(%s) OK", dir)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*800)
defer cancel()
log.Printf("download.DownloadBufferWithProgress() starts...\nurl: %v\ndst: %v", url, dst)
buf := make([]byte, 1024*640)
n, err := download.DownloadBufferWithProgress(
ctx,
c,
req,
dst,
buf,
// Number of bytes downloaded previously.
0,
// Callback to report progress.
func(total, prev, current int64, percent float32) {
log.Printf("%v / %v(%.2f%%) downloaded", prev+current, total, percent)
},
)
if err != nil {
if err != context.Canceled && err != context.DeadlineExceeded {
log.Printf("download.DownloadBufferWithProgress() error: %v", err)
return
}
log.Printf("download.DownloadBufferWithProgress() stopped, cause: %v. %v bytes downloaded", err, n)
} else {
log.Printf("download.DownloadBufferWithProgress() OK, %v bytes downloaded", n)
fmt.Printf("download successfully, total %v bytes downloaded", n)
return
}
log.Printf("download.DownloadBufferWithProgress() starts again to resume downloading...\nurl: %v\ndst: %v\ndownloaded: %v", url, dst, n)
// Resume the download by setting downloaded to n.
n2, err := download.DownloadBufferWithProgress(
context.Background(),
c,
req,
dst,
buf,
// Number of bytes downloaded.
n,
// Callback to report progress.
func(total, prev, current int64, percent float32) {
log.Printf("%v / %v(%.2f%%) downloaded", prev+current, total, percent)
},
)
if err != nil {
if err != context.Canceled && err != context.DeadlineExceeded {
log.Printf("download.DownloadBufferWithProgress() error: %v", err)
return
}
log.Printf("download.DownloadBufferWithProgress() stopped, cause: %v. %v bytes downloaded", err, n2)
} else {
log.Printf("download.DownloadBufferWithProgress() OK, %v bytes downloaded", n2)
}
log.Printf("total %v bytes downloaded", n+n2)
fmt.Printf("download successfully, total %v bytes downloaded", n+n2)
// Remove the files after test's done.
os.Remove(dst)
}
Output: download successfully, total 75917313 bytes downloaded
func DownloadWithProgress ¶ added in v0.0.17
func DownloadWithProgress( ctx context.Context, c *http.Client, req *http.Request, dst string, downloaded int64, fn func(total, prev, current int64, percent float32)) (n int64, err error)
DownloadWithProgress is the non-buffered version of DownloadBufferWithProgress.
Types ¶
This section is empty.