download

package module
v0.0.0-...-e214003 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 22 Imported by: 1

README

go-download

Go Go Reference License

Golang download manager package.


Install

$ go get github.com/blacktop/go-download

Getting Started

package main

import (
    "context"
    "log/slog"
    "os"
    "os/signal"
    "syscall"    

    "github.com/blacktop/go-download"
)

func main() {
    ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
    defer stop()

    log := slog.NewJSONHandler(os.Stdout, nil)

    mgr, err := download.New(&download.Config{
        Context:  ctx,
        Logger:   log,
        Progress: true,
        Parts:    4,
    })
    if err != nil {
        log.Error(err.Error())
        os.Exit(1)
    }

    if err := mgr.Get(os.Args[1]); err != nil {
        log.Error(err.Error())
        os.Exit(1)
    }
}

License

MIT Copyright (c) 2024 blacktop

Documentation

Overview

Package download provides a simple way to download files from the internet as fast as possible.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff

func Backoff(dur time.Duration) *delay

func DefaultPooledTransport

func DefaultPooledTransport() *http.Transport

DefaultPooledTransport returns a new http.Transport with similar default values to http.DefaultTransport. Do not use this for transient transports as it can leak file descriptors over time. Only use this for transports that will be re-used for the same host(s).

func DefaultTransport

func DefaultTransport() *http.Transport

DefaultTransport returns a new http.Transport with similar default values to http.DefaultTransport, but with idle connections and keepalives disabled.

func GetProxy

func GetProxy(proxy string) func(*http.Request) (*url.URL, error)

GetProxy takes either an input string or read the enviornment and returns a proxy function

func NewRangeReader

func NewRangeReader()

func RandomAgent

func RandomAgent() string

func Retry

func Retry(attempts int, sleep *delay, f func() error) (err error)

Retry will retry a function f a number of attempts with a sleep duration in between

func RetryWithContext

func RetryWithContext(ctx context.Context, attempts int, sleep *delay, f func() error) (err error)

Types

type Config

type Config struct {
	Context  context.Context
	Proxy    string
	Insecure bool
	CertPath string

	Timeout time.Duration

	Parts int

	//
	Resume     bool
	SkipAll    bool
	ResumeAll  bool
	RestartAll bool

	Progress bool
	Logger   *slog.Logger

	IgnoreHash bool
	Verbose    bool
}

type HttpError

type HttpError int

func (HttpError) Error

func (e HttpError) Error() string

type Manager

type Manager struct {
	URL           *url.URL
	Hash          any
	DestName      string
	AcceptRanges  string
	ContentType   string
	ContentAge    string
	ContentDate   string
	ContentSha256 string
	ContentSha1   string
	ContentLength int64
	Redirected    bool

	Headers map[string]string

	Elapsed time.Duration
	Parts   []*Part
	// contains filtered or unexported fields
}

func New

func New(conf *Config) (*Manager, error)

func (*Manager) Get

func (mgr *Manager) Get(url string) error

type Part

type Part struct {
	FileName string
	Start    int64
	Stop     int64
	Written  int64
	Skip     bool
	Elapsed  time.Duration
	// contains filtered or unexported fields
}

Directories

Path Synopsis
cmd
dl Module

Jump to

Keyboard shortcuts

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