warplib

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT, MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	B  int64 = 1
	KB       = 1024 * B
	MB       = 1024 * KB
	GB       = 1024 * MB
	TB       = 1024 * GB
)
View Source
const (
	DEF_MAX_CONNS  = 1
	DEF_CHUNK_SIZE = 32 * KB
	DEF_USER_AGENT = "Warp/1.0"
)
View Source
const MAIN_HASH = "main"
View Source
const (
	USER_AGENT_KEY = "User-Agent"
)

Variables

View Source
var (
	ErrContentLengthInvalid        = errors.New("content length is invalid")
	ErrContentLengthNotImplemented = errors.New("unknown size downloads not implemented yet")
	ErrNotSupported                = errors.New("file you're trying to download is not supported yet")

	ErrDownloadNotFound = errors.New("Item you are trying to download is not found")

	ErrFlushHashNotFound = errors.New("Item you are trying to flush is not found")
)
View Source
var (
	SizeOptionBy = SizeOption{B, "Bytes"}
	SizeOptionKB = SizeOption{KB, "KB"}
	SizeOptionMB = SizeOption{MB, "MB"}
	SizeOptionGB = SizeOption{GB, "GB"}
	SizeOptionTB = SizeOption{TB, "TB"}
)
View Source
var ConfigDir = func() (warpDir string) {
	cdr, err := os.UserConfigDir()
	if err != nil {
		panic(err)
	}

	if !dirExists(cdr) {
		err = os.Mkdir(cdr, os.ModePerm)
		if err != nil {
			panic(err)
		}
	}
	warpDir = cdr + "/warp"
	if dirExists(warpDir) {
		return
	}
	err = os.Mkdir(warpDir, os.ModePerm)
	if err != nil {
		panic(err)
	}
	return
}()
View Source
var DlDataDir = func() (dlDir string) {
	dlDir = ConfigDir + "/dldata"
	if dirExists(dlDir) {
		return
	}
	err := os.Mkdir(dlDir, os.ModePerm)
	if err != nil {
		panic(err)
	}
	return
}()

Functions

func GetPath

func GetPath(directory, file string) (path string)

func Place

func Place[t any](src []t, e t, index int) (dst []t)

func SortInt64s

func SortInt64s(x []int64)

Types

type AddDownloadOpts

type AddDownloadOpts struct {
	IsHidden         bool
	IsChildren       bool
	Child            *Downloader
	AbsoluteLocation string
}

type CompileCompleteHandlerFunc

type CompileCompleteHandlerFunc func(hash string, tread int64)

type CompileProgressHandlerFunc

type CompileProgressHandlerFunc func(hash string, nread int)

type CompileSkippedHandlerFunc

type CompileSkippedHandlerFunc func(hash string, tread int64)

type CompileStartHandlerFunc

type CompileStartHandlerFunc func(hash string)

type ContentLength

type ContentLength int64

func (ContentLength) Format

func (c ContentLength) Format(sep string, sizeOpts ...SizeOption) (clen string)

func (*ContentLength) IsUnknown

func (c *ContentLength) IsUnknown() (unknown bool)

func (ContentLength) String

func (c ContentLength) String() (clen string)

type DownloadCompleteHandlerFunc

type DownloadCompleteHandlerFunc func(hash string, tread int64)

type DownloadProgressHandlerFunc

type DownloadProgressHandlerFunc func(hash string, nread int)

type Downloader

type Downloader struct {
	// contains filtered or unexported fields
}

func NewDownloader

func NewDownloader(client *http.Client, url string, opts *DownloaderOpts) (d *Downloader, err error)

NewDownloader creates a new downloader with provided arguments. Use downloader.Start() to download the file.

func (*Downloader) GetContentLength

func (d *Downloader) GetContentLength() ContentLength

func (*Downloader) GetContentLengthAsInt

func (d *Downloader) GetContentLengthAsInt() int64

func (*Downloader) GetContentLengthAsString

func (d *Downloader) GetContentLengthAsString() string

func (*Downloader) GetDownloadDirectory

func (d *Downloader) GetDownloadDirectory() string

func (*Downloader) GetFileName

func (d *Downloader) GetFileName() string

func (*Downloader) GetSavePath

func (d *Downloader) GetSavePath() (svPath string)

func (*Downloader) Log

func (d *Downloader) Log(s string, a ...any)

Log adds the provided string to download's log file. It can't be used once download is complete.

func (*Downloader) NumConnections

func (d *Downloader) NumConnections() int

NumConnections returns the number of connections running currently.

func (*Downloader) Resume

func (d *Downloader) Resume(parts map[int64]*ItemPart) (err error)

map[InitialOffset(int64)]ItemPart

func (*Downloader) Start

func (d *Downloader) Start() (err error)

Start downloads the file and blocks current goroutine until the downloading is complete.

type DownloaderOpts

type DownloaderOpts struct {
	ForceParts   bool
	NumBaseParts int
	// FileName is used to set name of to-be-downloaded
	// file explicitly.
	//
	// Note: Warplib sets the file name sent by server
	// if file name not set explicitly.
	FileName string
	// DownloadDirectory sets the download directory for
	// file to be downloaded.
	DownloadDirectory string
	// MaxConnections sets the maximum number of parallel
	// network connections to be used for the downloading the file.
	MaxConnections int
	// MaxSegments sets the maximum number of file segments
	// to be created for the downloading the file.
	MaxSegments int

	Headers Headers

	Handlers *Handlers

	SkipSetup bool
}

Optional fields of downloader

type ErrorHandlerFunc

type ErrorHandlerFunc func(hash string, err error)

type Handlers

type Handlers struct {
	SpawnPartHandler        SpawnPartHandlerFunc
	RespawnPartHandler      RespawnPartHandlerFunc
	DownloadProgressHandler DownloadProgressHandlerFunc
	ResumeProgressHandler   ResumeProgressHandlerFunc
	ErrorHandler            ErrorHandlerFunc
	DownloadCompleteHandler DownloadCompleteHandlerFunc
	CompileStartHandler     CompileStartHandlerFunc
	CompileProgressHandler  CompileProgressHandlerFunc
	CompileSkippedHandler   CompileSkippedHandlerFunc
	CompileCompleteHandler  CompileCompleteHandlerFunc
}
type Header struct {
	Key, Value string
}

func (*Header) Add

func (h *Header) Add(header http.Header)

func (*Header) Set

func (h *Header) Set(header http.Header)

type Headers

type Headers []Header

func (Headers) Add

func (h Headers) Add(header http.Header)

func (Headers) Get

func (h Headers) Get(key string) (index int, have bool)

func (*Headers) InitOrUpdate

func (h *Headers) InitOrUpdate(key, value string)

func (Headers) Set

func (h Headers) Set(header http.Header)

func (*Headers) Update

func (h *Headers) Update(key, value string)

type Int64Slice

type Int64Slice []int64

func (Int64Slice) Len

func (x Int64Slice) Len() int

func (Int64Slice) Less

func (x Int64Slice) Less(i, j int) bool

func (Int64Slice) Swap

func (x Int64Slice) Swap(i, j int)

type Item

type Item struct {
	Hash             string
	Name             string
	Url              string
	Headers          Headers
	DateAdded        time.Time
	TotalSize        ContentLength
	Downloaded       ContentLength
	DownloadLocation string
	AbsoluteLocation string
	ChildHash        string
	Hidden           bool
	Children         bool
	Parts            map[int64]*ItemPart
	// contains filtered or unexported fields
}

func (*Item) GetAbsolutePath

func (i *Item) GetAbsolutePath() (aPath string)

func (*Item) GetPercentage

func (i *Item) GetPercentage() int64

func (*Item) GetSavePath

func (i *Item) GetSavePath() (svPath string)

func (*Item) Resume

func (i *Item) Resume() error

type ItemPart

type ItemPart struct {
	Hash        string
	FinalOffset int64
	Compiled    bool
}

type ItemsMap

type ItemsMap map[string]*Item

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func InitManager

func InitManager() (m *Manager, err error)

func (*Manager) AddDownload

func (m *Manager) AddDownload(d *Downloader, opts *AddDownloadOpts) (err error)

func (*Manager) Close

func (m *Manager) Close() error

func (*Manager) Flush

func (m *Manager) Flush() error

func (*Manager) FlushOne

func (m *Manager) FlushOne(hash string) error

TODO: make FlushOne safe for flushing while the item is being downloaded

func (*Manager) GetCompletedItems

func (m *Manager) GetCompletedItems() []*Item

func (*Manager) GetIncompleteItems

func (m *Manager) GetIncompleteItems() []*Item

func (*Manager) GetItem

func (m *Manager) GetItem(hash string) (item *Item)

func (*Manager) GetItems

func (m *Manager) GetItems() []*Item

func (*Manager) GetPublicItems

func (m *Manager) GetPublicItems() []*Item

func (*Manager) ResumeDownload

func (m *Manager) ResumeDownload(client *http.Client, hash string, opts *ResumeDownloadOpts) (item *Item, err error)

func (*Manager) UpdateItem

func (m *Manager) UpdateItem(item *Item)

type Part

type Part struct {
	// contains filtered or unexported fields
}

func (*Part) String

func (p *Part) String() string

type ProxyReader

type ProxyReader struct {
	// contains filtered or unexported fields
}

func NewProxyReader

func NewProxyReader(reader io.Reader, callback func(n int)) *ProxyReader

func (*ProxyReader) Read

func (p *ProxyReader) Read(b []byte) (n int, err error)

type RespawnPartHandlerFunc

type RespawnPartHandlerFunc func(hash string, partIoff, ioffNew, foffNew int64)

type ResumeDownloadOpts

type ResumeDownloadOpts struct {
	ForceParts bool
	// MaxConnections sets the maximum number of parallel
	// network connections to be used for the downloading the file.
	MaxConnections int
	// MaxSegments sets the maximum number of file segments
	// to be created for the downloading the file.
	MaxSegments int
	Headers     Headers
	Handlers    *Handlers
}

type ResumeProgressHandlerFunc

type ResumeProgressHandlerFunc func(hash string, nread int)

type SizeOption

type SizeOption struct {
	// contains filtered or unexported fields
}

func (*SizeOption) Get

func (s *SizeOption) Get(l ContentLength) (siz, rem int64)

func (*SizeOption) GetFrom

func (s *SizeOption) GetFrom(l int64) (siz, rem int64)

func (*SizeOption) String

func (s *SizeOption) String(l ContentLength) string

func (*SizeOption) StringFrom

func (s *SizeOption) StringFrom(l int64) string

type SpawnPartHandlerFunc

type SpawnPartHandlerFunc func(hash string, ioff, foff int64)

type VMap

type VMap[kT comparable, vT any] struct {
	// contains filtered or unexported fields
}

func NewVMap

func NewVMap[kT comparable, vT any]() VMap[kT, vT]

func (*VMap[kT, vT]) Dump

func (vm *VMap[kT, vT]) Dump() (keys []kT, vals []vT)

func (*VMap[kT, vT]) Get

func (vm *VMap[kT, vT]) Get(key kT) (val vT)

func (*VMap[kT, vT]) GetUnsafe

func (vm *VMap[kT, vT]) GetUnsafe(key kT) (val vT)

func (*VMap[kT, vT]) Make

func (vm *VMap[kT, vT]) Make()

func (*VMap[kT, vT]) Set

func (vm *VMap[kT, vT]) Set(key kT, val vT)

Jump to

Keyboard shortcuts

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