dlstream

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2019 License: MIT Imports: 10 Imported by: 0

README

Go streaming download

This is a HTTP download library that writes a file its downloading to a file while also presenting an io.Writer so the download can be processed while being downloaded. Downloads are automatically resumed when necessary.

Documentation

Take a look at the autogenerated documentation:

https://godoc.org/github.com/vansante/go-dl-stream

TODO

  • Look at Modified and ETag headers to determine whether we are dealing with the same file
  • ???

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoMoreRetries is returned when all retry attempts at downloading have failed
	ErrNoMoreRetries = errors.New("no download attempts remaining")
	// ErrInconsistentDownload is returned when the Content-Length header is not equal to the bytes received
	ErrInconsistentDownload = errors.New("inconsistent download")
	// ErrDownloadComplete is returned when a download is already completed
	ErrDownloadComplete = errors.New("download already complete")
)

Functions

func DownloadStream

func DownloadStream(ctx context.Context, url, filePath string, writer io.Writer) (err error)

DownloadStream downloads the file at the given URL to the filepath, while also sending a stream to the given writer while retrying any broken connections. Consecutive calls with the same URL and filePath will attempt to resume the download. The download stream written to the writer will then be replayed from the beginning of the download. With the given context the whole operation can be aborted.

func DownloadStreamOpts

func DownloadStreamOpts(ctx context.Context, url, filePath string, writer io.Writer, options Options) (err error)

DownloadStreamOpts is the same as DownloadStream, but allows you to override the default options with own values. See DownloadStream for more information.

func FetchURLInfo

func FetchURLInfo(ctx context.Context, url string, timeout time.Duration) (contentLength int64, resumable bool, err error)

FetchURLInfo does a HEAD request to see if the download URL is valid and returns the size of the content. Also checks if the download can be resumed by looking at capability headers.

Types

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

Logger is an optional interface used for outputting debug logging

type Options

type Options struct {
	Timeout             time.Duration
	InitialHeadTimeout  time.Duration
	RetryWait           time.Duration
	Retries             int
	RetryWaitMultiplier float64
	FileMode            os.FileMode
	BufferSize          int
	HTTPTransport       http.RoundTripper
	Logger              Logger
}

Options are a set of options used while downloading a stream

func DefaultOptions

func DefaultOptions() Options

DefaultOptions are the default options used when no options are specified by users of the library. Call this function to get a new default options struct where you can adjust only the things you need to.

func (*Options) Errorf

func (o *Options) Errorf(format string, args ...interface{})

Errorf checks if a logger is present and logs to it at error level if it is

func (*Options) Infof

func (o *Options) Infof(format string, args ...interface{})

Infof checks if a logger is present and logs to it at info level if it is

Jump to

Keyboard shortcuts

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