2024-03-13 00:57:16 +08:00
|
|
|
// Package chunkedreader provides functionality for reading a stream in chunks.
|
2018-01-22 01:33:58 +08:00
|
|
|
package chunkedreader
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2018-02-18 22:10:15 +08:00
|
|
|
"errors"
|
2018-01-22 01:33:58 +08:00
|
|
|
"io"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-01-22 01:33:58 +08:00
|
|
|
)
|
|
|
|
|
2018-02-18 22:10:15 +08:00
|
|
|
// io related errors returned by ChunkedReader
|
|
|
|
var (
|
|
|
|
ErrorFileClosed = errors.New("file already closed")
|
|
|
|
ErrorInvalidSeek = errors.New("invalid seek position")
|
|
|
|
)
|
|
|
|
|
2024-03-13 00:57:16 +08:00
|
|
|
// ChunkedReader describes what a chunked reader can do.
|
|
|
|
type ChunkedReader interface {
|
|
|
|
io.Reader
|
|
|
|
io.Seeker
|
|
|
|
io.Closer
|
|
|
|
fs.RangeSeeker
|
|
|
|
Open() (ChunkedReader, error)
|
2018-01-22 01:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a ChunkedReader for the Object.
|
|
|
|
//
|
2020-05-20 18:39:20 +08:00
|
|
|
// An initialChunkSize of <= 0 will disable chunked reading.
|
2018-02-18 22:10:15 +08:00
|
|
|
// If maxChunkSize is greater than initialChunkSize, the chunk size will be
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
// doubled after each chunk read with a maximum of maxChunkSize.
|
2018-01-22 01:33:58 +08:00
|
|
|
// A Seek or RangeSeek will reset the chunk size to it's initial value
|
2024-03-13 00:57:16 +08:00
|
|
|
func New(ctx context.Context, o fs.Object, initialChunkSize int64, maxChunkSize int64, streams int) ChunkedReader {
|
2018-02-18 22:10:15 +08:00
|
|
|
if initialChunkSize <= 0 {
|
|
|
|
initialChunkSize = -1
|
|
|
|
}
|
|
|
|
if maxChunkSize != -1 && maxChunkSize < initialChunkSize {
|
|
|
|
maxChunkSize = initialChunkSize
|
2018-01-22 01:33:58 +08:00
|
|
|
}
|
2024-03-13 00:57:16 +08:00
|
|
|
if streams < 0 {
|
|
|
|
streams = 0
|
2018-01-22 01:33:58 +08:00
|
|
|
}
|
2024-03-13 00:57:16 +08:00
|
|
|
if streams <= 1 || o.Size() < 0 {
|
|
|
|
return newSequential(ctx, o, initialChunkSize, maxChunkSize)
|
2018-02-18 22:10:15 +08:00
|
|
|
}
|
2024-03-13 00:57:16 +08:00
|
|
|
return newParallel(ctx, o, initialChunkSize, streams)
|
2018-01-22 01:33:58 +08:00
|
|
|
}
|