download

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 13 Imported by: 0

README

Download - File Download, support parallel

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/download

Getting Started

func TestDownload(t *testing.T) {
	url := "YOUR_FILE_URL"
	fileName := "test.mp4"
	err := Download(url, &Config{
		FilePath: fileName,
	})
	if err != nil {
		t.Error(err)
	}
}

Functions

  • Parallel
  • Progress

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultSegmentSize = 10 * 1024 * 1024

DefaultSegmentSize stands for the default segment size (10 Mb)

if the segment size is not set, the default segment size is used

Functions

func Download

func Download(url string, cfg ...*Config) error

Download downloads the file by url and config

Types

type Config

type Config struct {
	// FileName
	FilePath string
	// SegmentSize
	SegmentSize int
	// TmpDir
	TmpDir string
	//
	IsRangesDisabled bool
}

Config represents the download config

type Downloader

type Downloader struct {
	// URL is the url to download
	URL string
	// FileDir represents the directory to store the downloaded file
	FileDir string
	// FileName represents the file name
	FileName string
	// FileExt represents the file extension
	FileExt string
	// HeadHeaders represents the headers of the head file response
	HeadHeaders http.Header
	// ContentType represents the content type of the file
	ContentType string
	// ContentLength represents the content length of the file
	ContentLength int64
	// Hash represents the file info hash, use for temp dir
	Hash string
	// IsSupportRange represents if the server supports the range header
	IsSupportRange bool
	// SegmentSize represents the size of each segment, default is 10 Mb
	SegmentSize int
	// Ranges represents the ranges of the file
	Ranges []*Range
	// FileParts represents the file parts by ranges
	FileParts []*FilePart
	// TmpDir represents the temporary directory to store file parts
	TmpDir string
	//
	IsRangesDisabled bool
}

Downloader is the downloader

func New

func New(url string, config *Config) *Downloader

New returns a new downloader

func (*Downloader) Download

func (d *Downloader) Download() error

Download downloads the file

type FilePart

type FilePart struct {
	// Name of the file.
	// format <file_name>.<file_ext>.part.<part_index>.<range_start>.<range_end>
	Name string
	// FilePath concats the TmpDir and Hash.
	Path string
	//
	FileName   string
	FileExt    string
	Index      int
	RangeStart int
	RangeEnd   int
}

FilePart represents a part of a file. Format: <file_name>.<file_ext>.part.<part_index>.<range_start>.<range_end>

type Range

type Range struct {
	Start int
	End   int
}

Range represents the range of the file

Jump to

Keyboard shortcuts

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