download

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package download is the built-in Usenet downloader: NZB parsing, NNTP connections to the user's providers, article decoding and assembly.

Package download implements the NNTP (Usenet) download client (NNTP written/adapted in-house). Phase 2 will add a torrent client alongside this via anacrolix/torrent; nothing here assumes it's the only download protocol.

Index

Constants

View Source
const ResumeFile = ".mediarium-progress"

ResumeFile is the name of the small file a download keeps in its folder listing which articles are already saved. A download that was paused, stopped or cut off by a restart reads it and fetches only the rest.

View Source
const TooManyConnectionsMessage = "" /* 137-byte string literal not displayed */

TooManyConnectionsMessage is what a person is told when the provider says the login has too many connections.

Variables

This section is empty.

Functions

func ConfigureServer

func ConfigureServer(cfg ClientConfig)

ConfigureServer tells the shared connection limit about a server that was just added or edited. A lower connection count takes effect at once: the downloads running now close their extra connections after the article they are on.

func Download

func Download(ctx context.Context, cfg ClientConfig, nzb *NZB, destDir string, onProgress ProgressFunc) ([]string, error)

Download fetches every file in nzb from a single server and fails if any article is missing. It is DownloadFromServers for the one-server case.

func EncodeYenc

func EncodeYenc(name string, data []byte) []byte

EncodeYenc produces a single-part yEnc article body for data, named name. Used to build local test fixtures (no live Usenet server needed) and can double as a reference implementation if the app ever needs to verify its own decoder against known-good output.

func FriendlyError

func FriendlyError(err error) (message string, ok bool)

FriendlyError says what went wrong with a news server in plain words, for the connection test and the download queue. ok is false when there is nothing better to say than the error itself.

func IsTooManyConnections

func IsTooManyConnections(err error) bool

IsTooManyConnections reports whether err is the provider saying the login is using more connections than its plan allows: "502 Too many connections", or the same in words on any code. A plain 502 also counts unless the server is clearly talking about the login itself.

func RetireServer

func RetireServer(cfg ClientConfig)

RetireServer closes every connection to the server and stops the downloads running now from using it. Call it when the server is removed, switched off, or moved to another address or login. A download that has another server to fall back on carries on with that one; otherwise it fails with a message that says why.

Types

type ClientConfig

type ClientConfig struct {
	Host        string
	Port        int
	UseSSL      bool
	Username    string
	Password    string
	Connections int
}

ClientConfig is one configured Usenet server (a news server account from a provider such as Newshosting or Eweka): the built-in downloader connects straight to it, so no separate download client program is involved.

type NNTPConn

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

NNTPConn is a single connection to a Usenet server. The engine opens one per configured "connections" slot (connection count is part of the download client config) so segment fetches run in parallel.

func DialNNTP

func DialNNTP(host string, port int, useSSL bool, timeout time.Duration) (*NNTPConn, error)

DialNNTP connects (optionally over TLS) and reads the server's greeting.

func (*NNTPConn) Authenticate

func (c *NNTPConn) Authenticate(username, password string) error

Authenticate performs AUTHINFO USER/PASS. Skipped entirely if username is empty (some providers/block accounts don't require it).

func (*NNTPConn) Close

func (c *NNTPConn) Close() error

Close closes the connection at once, without saying goodbye. It also wakes up anything blocked reading from it.

func (*NNTPConn) FetchBody

func (c *NNTPConn) FetchBody(messageID string) ([]byte, error)

FetchBody retrieves one article's raw body (still yEnc-encoded) by message-id, e.g. "<abc123@example>".

func (*NNTPConn) Quit

func (c *NNTPConn) Quit() error

Quit says goodbye and closes the connection. It never waits long for the server's answer, and always closes.

func (*NNTPConn) SelectGroup

func (c *NNTPConn) SelectGroup(name string) error

SelectGroup issues GROUP <name>. Several providers require at least one successful GROUP before BODY-by-message-id works in that session.

type NNTPError

type NNTPError struct {
	Code    int
	Message string
}

func (*NNTPError) Error

func (e *NNTPError) Error() string

type NZB

type NZB struct {
	Files []NZBFile
}

NZB is a parsed .nzb file: one or more posted files, each split into numbered article segments that must be fetched and reassembled in order.

func ParseNZB

func ParseNZB(data []byte) (*NZB, error)

ParseNZB parses raw .nzb XML content, sorting each file's segments into download order (segment "number" is 1-based and not guaranteed to already be in document order). Many real-world .nzb files declare iso-8859-1 rather than UTF-8; that's handled explicitly since Go's encoding/xml has no built-in non-UTF-8 charset support.

func (NZB) TotalBytes

func (n NZB) TotalBytes() int64

TotalBytes sums every segment's declared size across all files.

type NZBFile

type NZBFile struct {
	Subject  string
	Groups   []string
	Segments []NZBSegment
}

type NZBSegment

type NZBSegment struct {
	Number    int
	Bytes     int64
	MessageID string
}

type ProgressFunc

type ProgressFunc func(bytesDone, bytesTotal int64)

ProgressFunc is invoked as segments complete; bytesTotal is the NZB's declared total size (may be approximate — Usenet segment sizes are nominal).

type ReleaseError

type ReleaseError struct{ Err error }

ReleaseError marks a failure that is the release's fault (a missing or undecodable article), as opposed to a connection or configuration problem on our side. Callers use it to decide whether to blocklist the release.

func (*ReleaseError) Error

func (e *ReleaseError) Error() string

func (*ReleaseError) Unwrap

func (e *ReleaseError) Unwrap() error

type Repo

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

Repo persists Usenet server configs, encrypting the password at rest.

func NewRepo

func NewRepo(db *sql.DB, box *crypto.Box) *Repo

func (*Repo) Create

func (r *Repo) Create(sc StoredClient) (StoredClient, error)

Create stores a new server (enabled).

func (*Repo) Delete

func (r *Repo) Delete(id int64) error

func (*Repo) Get

func (r *Repo) Get(id int64) (StoredClient, error)

Get returns one server by id.

func (*Repo) List

func (r *Repo) List() ([]StoredClient, error)

List returns the enabled servers in priority order — what a download uses.

func (*Repo) ListAll

func (r *Repo) ListAll() ([]StoredClient, error)

ListAll returns every server, disabled ones included, for the settings UI.

func (*Repo) Update

func (r *Repo) Update(sc StoredClient) error

Update rewrites a server. An empty password keeps the stored one, so the edit form never has to show or resend it.

type Result

type Result struct {
	Paths           []string
	TotalSegments   int
	MissingSegments int
}

Result reports what a download managed to fetch. Articles that no server had are counted in MissingSegments rather than failing the download: like SABnzbd, the caller carries on to PAR2 repair, which can rebuild a file from its recovery blocks.

func DownloadFromServers

func DownloadFromServers(ctx context.Context, servers []ClientConfig, nzb *NZB, destDir string, onProgress ProgressFunc) (Result, error)

DownloadFromServers fetches every file in nzb into destDir (normally /downloads/incomplete/<job-id>). servers are in priority order: the first is tried for every article; an article it lacks (or any article, if the server is unreachable) falls through to the next server, and so on, exactly like SABnzbd's primary and backup servers. A server only opens connections once it is actually needed.

A download that stops part way (paused, stopped, cut off by a restart or a failure) leaves a progress file in destDir (ResumeFile) listing the articles already saved. Calling this again for the same NZB and folder carries on with the rest; a folder with no progress file, or one written for another release, starts from nothing.

It returns an error only for problems that are not the release's fault (nothing configured, a server down with no backup, disk errors).

type ServerState

type ServerState struct {
	Host       string
	InUse      int
	Limit      int // what is allowed now
	Configured int // what the person set
}

ServerState is how many connections a news server login has open, for the diagnostics report.

func ServerStates

func ServerStates() []ServerState

ServerStates lists every login the downloader has used since it started.

type StoredClient

type StoredClient struct {
	ID       int64
	Name     string
	Config   ClientConfig
	Priority int  // 0 = primary; higher numbers are backup servers
	Enabled  bool // disabled servers are kept but never used
}

StoredClient is a configured Usenet server (a provider account the built-in downloader connects to) as persisted in the download_clients table, with its own DB id. The table keeps its original name; in the app and API these are "Usenet servers".

type YencPart

type YencPart struct {
	Name       string
	PartBegin  int64 // 1-based byte offset within the whole file (0 if single-part)
	PartEnd    int64
	Size       int64 // declared total file size (from =ybegin, single-part; or =ysize on multi-part)
	Data       []byte
	CRC32      uint32
	CRC32Valid bool
}

YencPart is one decoded yEnc-encoded article body, with enough header info to reassemble multi-part files in order.

func DecodeYenc

func DecodeYenc(r io.Reader) (*YencPart, error)

DecodeYenc decodes a single yEnc-encoded article body (the content of one NNTP ARTICLE/BODY response), per the yEnc 1.3 draft spec.

Jump to

Keyboard shortcuts

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