Documentation
¶
Index ¶
- Constants
- Variables
- func ValidateReceivedRanges(ranges []ByteRange, expectedTotalSize int64) error
- type ByteRange
- type ChunkSession
- type ChunkSessionManager
- func (m *ChunkSessionManager) AbortSession(token string) error
- func (m *ChunkSessionManager) Close()
- func (m *ChunkSessionManager) Evict(token string) error
- func (m *ChunkSessionManager) Expirations() map[string]time.Time
- func (m *ChunkSessionManager) FinalizeSession(token string, destinationPath string) (string, error)
- func (m *ChunkSessionManager) StartSession(fileName string, totalSize int64) (*ChunkSession, error)
- func (m *ChunkSessionManager) SuggestedChunkSize() int64
- func (m *ChunkSessionManager) WriteChunk(token string, offset int64, data []byte) (int64, error)
- type ChunkSessionOption
Constants ¶
const ( // DefaultSuggestedChunkSize is the recommended chunk payload size (25MB). DefaultSuggestedChunkSize = 25 * 1024 * 1024 // DefaultMaxChunkSize is the maximum allowed single chunk payload (32MB). DefaultMaxChunkSize = 32 * 1024 * 1024 // DefaultSessionTTL is the duration after which an inactive chunk upload session expires. DefaultSessionTTL = 30 * time.Minute // DefaultCleanupInterval is the frequency at which the cleaner checks for expired sessions. DefaultCleanupInterval = 1 * time.Minute )
Variables ¶
var ( // ErrSessionNotFound is returned when a session token is not found or has expired. ErrSessionNotFound = errors.New("chunk upload session not found or expired") // ErrInvalidTotalSize is returned when the specified total file size is non-positive. ErrInvalidTotalSize = errors.New("total file size must be positive") // ErrEmptyChunkData is returned when an uploaded chunk contains empty data payload. ErrEmptyChunkData = errors.New("chunk data must not be empty") // ErrInvalidOffset is returned when the chunk byte offset is invalid. ErrInvalidOffset = errors.New("invalid chunk byte offset") // ErrChunkSizeTooLarge is returned when a single chunk exceeds the maximum permitted size limit. ErrChunkSizeTooLarge = errors.New("chunk size exceeds maximum allowed limit") )
Functions ¶
func ValidateReceivedRanges ¶
ValidateReceivedRanges validates that the given byte ranges completely cover [0, expectedTotalSize) without gaps or overlaps after sorting by start offset.
Types ¶
type ChunkSession ¶
type ChunkSession struct {
Token string
FileName string
TotalSize int64
ReceivedBytes int64
ReceivedRanges []ByteRange
TempFilePath string
TempFile *os.File
ExpiresAt time.Time
// contains filtered or unexported fields
}
ChunkSession represents an in-progress chunked upload session.
type ChunkSessionManager ¶
type ChunkSessionManager struct {
// contains filtered or unexported fields
}
ChunkSessionManager manages chunked upload sessions and temporary storage.
func NewChunkSessionManager ¶
func NewChunkSessionManager(uploadDir string, opts ...ChunkSessionOption) *ChunkSessionManager
NewChunkSessionManager creates a new ChunkSessionManager instance.
func (*ChunkSessionManager) AbortSession ¶
func (m *ChunkSessionManager) AbortSession(token string) error
AbortSession closes and deletes the temporary file and removes the session from the manager.
func (*ChunkSessionManager) Close ¶
func (m *ChunkSessionManager) Close()
Close stops the background cleaner and cleans up active sessions.
func (*ChunkSessionManager) Evict ¶
func (m *ChunkSessionManager) Evict(token string) error
Evict implements ttlcleaner.ExpirableTarget.
func (*ChunkSessionManager) Expirations ¶
func (m *ChunkSessionManager) Expirations() map[string]time.Time
Expirations implements ttlcleaner.ExpirableTarget.
func (*ChunkSessionManager) FinalizeSession ¶
func (m *ChunkSessionManager) FinalizeSession(token string, destinationPath string) (string, error)
FinalizeSession validates chunk completeness, closes the temporary file, moves it to destinationPath (if provided), and removes the session from the manager. If destinationPath is empty, it closes the file and returns the temp file path.
func (*ChunkSessionManager) StartSession ¶
func (m *ChunkSessionManager) StartSession(fileName string, totalSize int64) (*ChunkSession, error)
StartSession initializes a new chunk upload session and creates a temporary file to store chunks.
func (*ChunkSessionManager) SuggestedChunkSize ¶
func (m *ChunkSessionManager) SuggestedChunkSize() int64
SuggestedChunkSize returns the recommended chunk size in bytes.
func (*ChunkSessionManager) WriteChunk ¶
WriteChunk writes a chunk of data at the specified byte offset to the session's temporary file.
type ChunkSessionOption ¶
type ChunkSessionOption func(*ChunkSessionManager)
ChunkSessionOption is a functional option for configuring ChunkSessionManager.
func WithMaxChunkSize ¶
func WithMaxChunkSize(maxChunkSize int64) ChunkSessionOption
WithMaxChunkSize configures the maximum permitted single chunk size in bytes.
func WithSessionTTL ¶
func WithSessionTTL(ttl time.Duration) ChunkSessionOption
WithSessionTTL configures the session expiration TTL.
func WithSuggestedChunkSize ¶
func WithSuggestedChunkSize(suggestedChunkSize int64) ChunkSessionOption
WithSuggestedChunkSize configures the recommended chunk size in bytes.
func WithTokenGenerator ¶
func WithTokenGenerator(generator idgenerator.IDGenerator) ChunkSessionOption
WithTokenGenerator configures the token generator for session tokens.