Documentation
¶
Overview ¶
Package engine is a segmented HTTP downloader with pause/resume and dynamic segmentation.
- Probe with "Range: bytes=0-0" to learn the size and Range support.
- Split the file into N segments; one connection per segment, each writing directly at its offset in a preallocated .part file.
- Dynamic segmentation: when a connection finishes its segment, it takes over the back half of the segment with the most bytes left, so fast connections keep working until the very end.
- Progress (each segment's pos/end) is saved to <file>.part.json every 2s and on pause, so a download resumes after a pause or restart.
Servers without Range support fall back to one stream that can't resume.
Index ¶
- Constants
- Variables
- func CategoryOf(name string) string
- func Downloadable(rawURL string) (ok bool, kind string)
- func IsPlaylistURL(raw string) bool
- func ParseChecksum(s string) (string, error)
- func ParseProxy(s string) (string, error)
- func SafeName(name string) string
- func Transient(err error) bool
- func VideoPage(raw string) bool
- func VideoTool(name string) (string, error)
- type ChecksumError
- type DiskFullError
- type Download
- func (d *Download) Connections() int
- func (d *Download) Info() (path string, total int64)
- func (d *Download) Progress() float64
- func (d *Download) ProgressWith(status string, total int64) float64
- func (d *Download) QueueID() string
- func (d *Download) RemainingGaps() [][2]float64
- func (d *Download) SetQueue(q string)
- func (d *Download) SetRequest(r Request)
- func (d *Download) SetStatus(s string)
- func (d *Download) State() (string, error)
- func (d *Download) StateInfo() (status string, err error, path string, total int64)
- func (d *Download) StateInfoQueue() (status string, err error, path string, total int64, queueID string)
- type Engine
- func (e *Engine) Dir() string
- func (e *Engine) FolderFor(name string) string
- func (e *Engine) Probe(ctx context.Context, r Request) (Info, error)
- func (e *Engine) Run(ctx context.Context, d *Download) error
- func (e *Engine) SegmentsDefault() int
- func (e *Engine) SetDir(d string)
- func (e *Engine) SetProxy(s string)
- func (e *Engine) SetSegments(n int)
- func (e *Engine) SetSortByType(on bool)
- func (e *Engine) VideoFilename(ctx context.Context, r Request) (string, error)
- type Info
- type Limiter
- type PlaylistEntry
- type Request
- type Segment
- type StatusError
Constants ¶
const MinSplit = 128 << 10
MinSplit is the smallest remaining range worth splitting for a new connection.
Variables ¶
var ( ErrEncrypted = errors.New("this stream is encrypted (DRM) and can't be downloaded") ErrLive = errors.New("this is a live stream; only complete (on-demand) videos are supported") )
var Categories = []string{"Video", "Music", "Images", "Documents", "E-books", "Compressed", "Disk images", "Programs", "Torrents", "Fonts"}
var ErrStalled = errors.New("connection stalled (no data received)")
ErrStalled means a connection received no data for too long; callers retry it like any other network error.
Functions ¶
func CategoryOf ¶
CategoryOf returns the category subfolder for a file name, or "".
func Downloadable ¶
Downloadable reports whether a URL points at a file type worth offering to download (a known category, or an HLS/DASH stream). Used for the clipboard monitor; returns the stream kind ("hls", "dash" or "").
func IsPlaylistURL ¶ added in v0.8.9
IsPlaylistURL reports whether raw is a YouTube playlist page URL.
func ParseChecksum ¶
ParseChecksum accepts a hex MD5, SHA-1, SHA-256 or SHA-512, optionally prefixed like "sha256:" or followed by a file name (as in .sha256 files), and returns it normalised. The algorithm is told by its length.
func ParseProxy ¶
ParseProxy checks a proxy setting and returns it normalised.
func SafeName ¶
safeName makes name (from a web server, so untrusted) a plain file name inside the download folder: no path separators or "..", no Windows device names, no trailing dots or spaces, and short enough for Windows. SafeName cleans a file name the way downloads are saved (see safeName).
func Transient ¶
Transient reports whether err is worth retrying later: the network dropped or the server had a temporary problem. A missing file, a refused link or a full disk is not.
Types ¶
type ChecksumError ¶
type ChecksumError struct{ Algo, Want, Got string }
ChecksumError means the finished file doesn't match the expected checksum.
func (*ChecksumError) Error ¶
func (e *ChecksumError) Error() string
type DiskFullError ¶
errBusy means the server refused another connection (429/503). DiskFullError means the file is bigger than the space left on the drive.
func (*DiskFullError) Error ¶
func (e *DiskFullError) Error() string
type Download ¶
type Download struct {
ID int
Req Request
Path string
Total int64 // -1 if unknown
Ranged bool
Done atomic.Int64
Status string // queued, downloading, paused, done, error
Err error
Verified bool // matched Req.Checksum
// Validator is the file's strong ETag or Last-Modified date from the
// first response. Every range request sends it as If-Range, so a file
// that changed on the server since is never stitched into the old one.
Validator string
// Gzipped means the server sent the file compressed (Content-Encoding:
// gzip); it is unpacked when complete, as a browser would.
Gzipped bool
// contains filtered or unexported fields
}
func FindPaused ¶
FindPaused lists the paused downloads in dir.
func (*Download) Connections ¶
Connections reports how many connections are currently downloading.
func (*Download) Info ¶
Info returns the file path and total size (-1 if unknown); safe while running.
func (*Download) ProgressWith ¶ added in v0.8.10
ProgressWith returns percent complete given an already fetched status and total size.
func (*Download) RemainingGaps ¶ added in v0.8.8
RemainingGaps returns unfetched [startPct, widthPct] intervals for a multi-part download once bytes have started arriving.
func (*Download) SetRequest ¶
SetRequest replaces the download's request (a refreshed link) while it isn't running; progress and the file are kept.
func (*Download) SetStatus ¶
SetStatus sets the status of a download that isn't running (e.g. queued).
type Engine ¶
type Engine struct {
Client *http.Client
Limit *Limiter // shared speed limit; nil = unlimited
// contains filtered or unexported fields
}
func (*Engine) Dir ¶
Dir is the folder new downloads are saved to. Downloads already started keep their own path when it changes.
func (*Engine) Run ¶
Run downloads (or resumes) d. Cancelling ctx pauses it: progress is saved and Run returns context.Canceled with Status "paused".
func (*Engine) SegmentsDefault ¶
SegmentsDefault is the default number of connections per download.
func (*Engine) SetSegments ¶
SetSegments sets the default number of connections per download (1-32).
func (*Engine) SetSortByType ¶
SetSortByType turns category subfolders on or off for new downloads.
type Info ¶
type Info struct {
Name string `json:"name"`
Size int64 `json:"size"` // -1 if unknown
Resumable bool `json:"resumable"` // server supports byte ranges
Category string `json:"category"`
Kind string `json:"kind"` // "", "hls" or "dash"
}
Info describes a link before it's downloaded (for the Add Download dialog).
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a shared bandwidth budget for all downloads (a global speed limit). The rate can be changed at any time; 0 means unlimited.
type PlaylistEntry ¶ added in v0.8.9
PlaylistEntry is one video inside an expanded playlist.
func ExpandPlaylist ¶ added in v0.8.9
ExpandPlaylist uses yt-dlp --flat-playlist to list the videos in a playlist.
type Request ¶
type Request struct {
URL, Referrer, Cookies, UserAgent, Filename string
Kind string // "" = file, "hls"/.m3u8, "dash"/.mpd, "page"/yt-dlp
Title string // page/video title, used to name videos
Dir string `json:",omitempty"` // save here instead of the default/category folder
Segments int `json:",omitempty"` // connections for this download (0 = engine default)
User, Pass string `json:",omitempty"` // HTTP login for sites that ask for one
Checksum string `json:",omitempty"`
Queue string `json:",omitempty"` // named queue ID; "" = the main queue // expected MD5/SHA-1/SHA-256/SHA-512 (hex), checked when done
Format string `json:",omitempty"` // video quality: "" (best), "1080p", "720p", "480p", "audio"
// Origin is the link the browser started from when it was redirected to
// URL, with that site's cookies. Some sites (Canvas, for one) redirect to
// a file server with a token only the browser may use; when URL is
// refused, Velo starts again from Origin and follows the redirect itself.
Origin, OriginCookies string `json:",omitempty"`
// Captured means the browser started this download and handed it to
// Velo, so the browser can take it back if the site refuses Velo.
Captured bool `json:",omitempty"`
}
type Segment ¶
type Segment struct {
Pos int64 `json:"pos"`
End int64 `json:"end"`
// contains filtered or unexported fields
}
Segment is the byte range [Pos, End] still to fetch. Guarded by Download.mu.
type StatusError ¶
StatusError is an HTTP error response.
func (*StatusError) Error ¶
func (e *StatusError) Error() string