Documentation
¶
Index ¶
- Constants
- Variables
- type DownloadManager
- type File
- func (f *File) Close() error
- func (f *File) Complete() error
- func (f *File) GetSize() (int64, error)
- func (f *File) InvalidateRange(start, end int64) error
- func (f *File) Read(p []byte) (n int, err error)
- func (f *File) ReadAt(p []byte, off int64) (int, error)
- func (f *File) SavePart() error
- func (f *File) Seek(offset int64, whence int) (int64, error)
- func (f *File) SetSize(size int64)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Verify(expected [32]byte) error
- func (f *File) VerifyRange(start, end int64, expected [32]byte) error
Constants ¶
const DefaultBlockSize = 65536
DefaultBlockSize is the size in bytes of each download block (64KB). Downloaded data is tracked and stored in blocks of this size.
Variables ¶
var DefaultDownloadManager = NewDownloadManager()
DefaultDownloadManager is the default DownloadManager used by package-level functions like Open. It is initialized with sensible defaults.
var ErrChecksumMismatch = errors.New("checksum mismatch")
ErrChecksumMismatch is returned when a checksum verification fails. The affected blocks are automatically invalidated so they will be re-downloaded on next access.
Functions ¶
This section is empty.
Types ¶
type DownloadManager ¶
type DownloadManager struct {
// MaxConcurrent is the maximum number of concurrent downloads.
// changing it might not be effective immediately. Default is 10
MaxConcurrent int
// MaxReadersPerFile is the maximum number of concurrent HTTP connections
// per file. This allows efficient random access patterns (e.g., ZIP files).
// Default is 3.
MaxReadersPerFile int
// Client is the http client used to access urls to be downloaded
Client *http.Client
// TmpDir is where temporary files are created, and by default will be os.TempDir()
TmpDir string
// MaxDataJump is the maximum data that can be read & dropped when seeking forward
// default is 512kB
MaxDataJump int64
*log.Logger
// contains filtered or unexported fields
}
DownloadManager orchestrates concurrent downloads and manages HTTP connections for accessing remote files. It maintains a pool of download clients, handles connection reuse, and coordinates background downloading of file blocks during idle periods. A default manager is provided as DefaultDownloadManager.
func NewDownloadManager ¶
func NewDownloadManager() *DownloadManager
NewDownloadManager creates and returns a new DownloadManager with default settings: 10 concurrent connections, 512KB maximum data jump for seeking, and the system temp directory for temporary files. The manager starts a background goroutine for connection management and idle downloading.
func (*DownloadManager) For ¶
func (dl *DownloadManager) For(u string) io.ReaderAt
For returns an io.ReaderAt interface for the given URL. This provides a simple way to read from a remote URL at arbitrary offsets without managing a File object. Each ReadAt call may open a new HTTP connection.
func (*DownloadManager) Open ¶
func (dlm *DownloadManager) Open(u string) (*File, error)
Open a given URL and return a file pointer that will run partial downloads when reads are needed. Downloaded data will be stored in the system temp directory, and will be removed at the end if download is incomplete.
func (*DownloadManager) OpenTo ¶
func (dlm *DownloadManager) OpenTo(u, localPath string) (*File, error)
OpenTo opens a given URL and stores downloaded data at the specified local path. If the file already exists with a .part file, the download will resume. If the file exists without a .part file, it is assumed to be complete.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a remote file that can be accessed locally through partial downloads. It implements io.Reader, io.ReaderAt, and io.Seeker interfaces, allowing transparent access to remote HTTP content as if it were a local file. Downloaded data is cached locally in blocks, and only the required portions are fetched on demand. Partial download progress can be persisted to disk and resumed later.
func Open ¶
Open a given URL and return a file pointer that will run partial downloads when reads are needed. Downloaded data will be stored in the system temp directory, and will be removed at the end if download is incomplete.
func (*File) Close ¶
Close will close the file and make sure data is synced on the disk if the download is still partial.
func (*File) Complete ¶ added in v0.0.15
Complete will download the whole file locally, returning errors in case of failure.
func (*File) InvalidateRange ¶ added in v0.2.1
InvalidateRange marks all blocks overlapping the byte range [start, end) as not downloaded, causing them to be re-downloaded on next read access or by the idle background downloader. This is useful when a checksum verification detects corrupted data.
func (*File) Read ¶
Read will read data from the file at the current position after checking it was successfully downloaded.
func (*File) ReadAt ¶
ReadAt will read data from the disk at a specified offset after checking it was successfully downloaded.
func (*File) SavePart ¶
SavePart triggers an immediate save of the download status to a .part file on disk, allowing resume to happen if the program terminates and is opened again.
func (*File) Seek ¶
Seek in file for next Read() operation. If you use io.SeekEnd but the file download hasn't started, a HEAD request will be made to obtain the file's size.
func (*File) SetSize ¶ added in v0.0.13
SetSize manually sets the file size for incomplete files. This is useful when the file size is known in advance but the server doesn't provide a Content-Length header. Has no effect on complete files.
func (*File) Stat ¶ added in v0.0.13
Stat() will obtain the information of the underlying file after checking its size matches that of the file to download.
func (*File) Verify ¶ added in v0.2.1
Verify downloads the entire file if needed, then computes its SHA-256 hash and compares it to expected. If the hashes do not match, all blocks are invalidated and ErrChecksumMismatch is returned.
func (*File) VerifyRange ¶ added in v0.2.1
VerifyRange downloads the blocks covering [start, end) if needed, then computes the SHA-256 hash of that byte range and compares it to expected. If the hashes do not match, the blocks in the range are invalidated and ErrChecksumMismatch is returned.