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
- func ConfigureServer(cfg ClientConfig)
- func Download(ctx context.Context, cfg ClientConfig, nzb *NZB, destDir string, ...) ([]string, error)
- func EncodeYenc(name string, data []byte) []byte
- func FriendlyError(err error) (message string, ok bool)
- func IsTooManyConnections(err error) bool
- func RetireServer(cfg ClientConfig)
- type ClientConfig
- type NNTPConn
- type NNTPError
- type NZB
- type NZBFile
- type NZBSegment
- type ProgressFunc
- type ReleaseError
- type Repo
- type Result
- type ServerState
- type StoredClient
- type YencPart
Constants ¶
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.
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 ¶
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 ¶
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 ¶
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 (*NNTPConn) Authenticate ¶
Authenticate performs AUTHINFO USER/PASS. Skipped entirely if username is empty (some providers/block accounts don't require it).
func (*NNTPConn) Close ¶
Close closes the connection at once, without saying goodbye. It also wakes up anything blocked reading from it.
func (*NNTPConn) FetchBody ¶
FetchBody retrieves one article's raw body (still yEnc-encoded) by message-id, e.g. "<abc123@example>".
func (*NNTPConn) Quit ¶
Quit says goodbye and closes the connection. It never waits long for the server's answer, and always closes.
func (*NNTPConn) SelectGroup ¶
SelectGroup issues GROUP <name>. Several providers require at least one successful GROUP before BODY-by-message-id works in that session.
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 ¶
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 ¶
TotalBytes sums every segment's declared size across all files.
type NZBFile ¶
type NZBFile struct {
Subject string
Groups []string
Segments []NZBSegment
}
type NZBSegment ¶
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 (*Repo) Create ¶
func (r *Repo) Create(sc StoredClient) (StoredClient, error)
Create stores a new server (enabled).
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 ¶
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.