downloader

package module
v0.0.0-...-7a2606d Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: GPL-3.0 Imports: 12 Imported by: 1

README

SimpleDownloader

Go Report Card

A simple multi-thread downloader package for Go.

Example:

package main

import (
    "fmt"
    "time"
    "github.com/XiaoMengXinX/SimpleDownloader"
)

func main() {
    d := downloader.NewDownloader().SetDownloadRoutine(4)

    url := "https://xve.me/DemoVideo"
    task, _ := d.NewDownloadTask(url)
    ch := task.ForceMultiThread().SetFileName("demo.mp4").DownloadWithChannel()

loop:
    for {
        select {
        case err := <-ch:
            if err != nil {
                panic(err)
            }
            break loop
        default:
            fmt.Printf("Download Speed: %s\n", task.CalculateSpeed(time.Millisecond*200))
        }
    }
}

Documentation

See pkg.go.dev

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DownloadTask

type DownloadTask struct {
	Downloader *Downloader
	Scheme     string
	Host       string
	Path       string
	Query      string
	// contains filtered or unexported fields
}

func (*DownloadTask) AddHeader

func (d *DownloadTask) AddHeader(key, value string) *DownloadTask

AddHeader add a http header to the task

func (*DownloadTask) AddHostNameToHeader

func (d *DownloadTask) AddHostNameToHeader(host string) *DownloadTask

AddHostNameToHeader add the host name to the header

func (*DownloadTask) CalculateSpeed

func (d *DownloadTask) CalculateSpeed(elapse time.Duration) string

CalculateSpeed returns the download speed of the current task.

func (*DownloadTask) Cancel

func (d *DownloadTask) Cancel()

Cancel the task

func (*DownloadTask) CleanTempFiles

func (d *DownloadTask) CleanTempFiles()

CleanTempFiles delete the temp files of the task

func (*DownloadTask) Download

func (d *DownloadTask) Download() (err error)

Download starts the download task

func (*DownloadTask) DownloadWithChannel

func (d *DownloadTask) DownloadWithChannel() (ch chan error)

DownloadWithChannel returns a channel to receive the error

func (*DownloadTask) ForceHttp

func (d *DownloadTask) ForceHttp() *DownloadTask

ForceHttp set the scheme to http

func (*DownloadTask) ForceHttps

func (d *DownloadTask) ForceHttps() *DownloadTask

ForceHttps set the scheme to https

func (*DownloadTask) ForceMultiThread

func (d *DownloadTask) ForceMultiThread() *DownloadTask

ForceMultiThread force to use multi-thread download without checking the Accept-Ranges header

func (*DownloadTask) GetFileSize

func (d *DownloadTask) GetFileSize() int64

GetFileSize return the content length of the task

func (*DownloadTask) GetHostName

func (d *DownloadTask) GetHostName() string

GetHostName return the real host name of the task

func (*DownloadTask) GetWrittenBytes

func (d *DownloadTask) GetWrittenBytes() int64

GetWrittenBytes return the downloaded bytes number of the task

func (*DownloadTask) IgnoreCertificateVerify

func (d *DownloadTask) IgnoreCertificateVerify() *DownloadTask

IgnoreCertificateVerify ignore the SSL certificate verification

func (*DownloadTask) ReplaceHostName

func (d *DownloadTask) ReplaceHostName(host string) *DownloadTask

ReplaceHostName replace the host name in url

func (*DownloadTask) SetFileName

func (d *DownloadTask) SetFileName(fileName string) *DownloadTask

SetFileName set the file name of the task

func (*DownloadTask) WithResolvedIp

func (d *DownloadTask) WithResolvedIp(ip string) *DownloadTask

WithResolvedIp will force to make http request with the specific IP address like the "--resolve" option in curl.

For example:

d := downloader.NewDownloader()
task, _ := d.NewDownloadTask("https://www.example.com/1.jpg")
task.WithResolvedIp("114.5.1.4").Download()

This is similar to:

curl --resolve www.example.com:443:114.5.1.4 https://www.example.com/1.jpg

func (*DownloadTask) WithResolvedIpOnHost

func (d *DownloadTask) WithResolvedIpOnHost(ip string) *DownloadTask

WithResolvedIpOnHost is similar to WithResolvedIp, but instead of replacing the dns record, it replaces the host name in url and add the original host name to the header.

For example:

d := downloader.NewDownloader()
task, _ := d.NewDownloadTask("https://www.example.com/1.jpg")
task.WithResolvedIp("114.5.1.4:23333").Download()

It is similar to:

curl -H "Host: www.example.com" https://114.5.1.4:23333/1.jpg

This can be used to download a file through your own hosted reverse proxy server.

Notice that enable this option will default ignore the SSL certificate verification.

type Downloader

type Downloader struct {
	SavePath        string
	HttpProxy       HttpProxy
	TimeOut         time.Duration
	UserAgent       string
	DownloadRoutine int
	BreakPoint      bool
}

func NewDownloader

func NewDownloader() *Downloader

NewDownloader is to create a new downloader

func (*Downloader) NewDownloadTask

func (d *Downloader) NewDownloadTask(URL string) (*DownloadTask, error)

NewDownloadTask is to create a new download task belongs to the downloader

func (*Downloader) SetBreakPoint

func (d *Downloader) SetBreakPoint(isEnabled bool) *Downloader

SetBreakPoint set the break point download

func (*Downloader) SetDownloadRoutine

func (d *Downloader) SetDownloadRoutine(routine int) *Downloader

SetDownloadRoutine set the threads to download

func (*Downloader) SetNoProxy

func (d *Downloader) SetNoProxy() *Downloader

SetNoProxy disable the proxy settings from environment

func (*Downloader) SetProxy

func (d *Downloader) SetProxy(host string) *Downloader

SetProxy set the proxy settings

func (*Downloader) SetSavePath

func (d *Downloader) SetSavePath(path string) *Downloader

SetSavePath set the path to save files

func (*Downloader) SetTimeOut

func (d *Downloader) SetTimeOut(timeout time.Duration) *Downloader

SetTimeOut set the context timeout

func (*Downloader) SetUserAgent

func (d *Downloader) SetUserAgent(userAgent string) *Downloader

SetUserAgent set the user agent

type HttpProxy

type HttpProxy struct {
	Host string
	// contains filtered or unexported fields
}

type ReaderF

type ReaderF func(b []byte) (n int, err error)

func (ReaderF) Read

func (f ReaderF) Read(b []byte) (n int, err error)

type WriterF

type WriterF func(b []byte) (n int, err error)

func (WriterF) Write

func (f WriterF) Write(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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