Documentation
¶
Overview ¶
Package transfer provides chunked file transfer with resume support.
Index ¶
- Constants
- Variables
- func CalculateFileChecksum(path string) (string, error)
- type ChecksumError
- type Chunk
- type ChunkReader
- type ChunkWriter
- type FileEntry
- type ProgressCallback
- type ProgressTracker
- func (t *ProgressTracker) GetSession(sessionID string) *UploadSession
- func (t *ProgressTracker) NotifyProgress(sessionID string)
- func (t *ProgressTracker) OnProgress(cb ProgressCallback)
- func (t *ProgressTracker) Start()
- func (t *ProgressTracker) Stop()
- func (t *ProgressTracker) Track(session *UploadSession)
- func (t *ProgressTracker) Untrack(sessionID string)
- type SpeedCalculator
- type UploadSession
- func (s *UploadSession) AddProgress(bytes int64, filePath string, offset int64)
- func (s *UploadSession) Cancel()
- func (s *UploadSession) Complete()
- func (s *UploadSession) Fail(err string)
- func (s *UploadSession) GetResumeOffset(filePath string) int64
- func (s *UploadSession) IsActive() bool
- func (s *UploadSession) Progress() protocol.UploadProgress
- func (s *UploadSession) Start()
Constants ¶
const DefaultChunkSize = 1024 * 1024
DefaultChunkSize is the default size for file chunks (1MB).
Variables ¶
var ErrChecksumMismatch = &ChecksumError{Message: "checksum mismatch"}
ErrChecksumMismatch is returned when chunk checksum verification fails.
Functions ¶
func CalculateFileChecksum ¶
CalculateFileChecksum calculates the SHA256 checksum of an entire file.
Types ¶
type ChecksumError ¶
type ChecksumError struct {
Message string
}
ChecksumError represents a checksum verification failure.
func (*ChecksumError) Error ¶
func (e *ChecksumError) Error() string
type Chunk ¶
type Chunk struct {
Offset int64 `json:"offset"`
Size int `json:"size"`
Data []byte `json:"data,omitempty"`
FilePath string `json:"filePath"`
Checksum string `json:"checksum,omitempty"`
}
Chunk represents a single chunk of data in a transfer.
type ChunkReader ¶
type ChunkReader struct {
// contains filtered or unexported fields
}
ChunkReader reads a file in chunks.
func NewChunkReader ¶
func NewChunkReader(path string, chunkSize int) (*ChunkReader, error)
NewChunkReader creates a new chunk reader for the given file.
func (*ChunkReader) FileSize ¶
func (r *ChunkReader) FileSize() int64
FileSize returns the total file size.
func (*ChunkReader) NextChunk ¶
func (r *ChunkReader) NextChunk() (*Chunk, error)
NextChunk reads and returns the next chunk, or nil if EOF.
func (*ChunkReader) Offset ¶
func (r *ChunkReader) Offset() int64
Offset returns the current read offset.
func (*ChunkReader) Remaining ¶
func (r *ChunkReader) Remaining() int64
Remaining returns the number of bytes remaining.
func (*ChunkReader) SeekTo ¶
func (r *ChunkReader) SeekTo(offset int64) error
SeekTo moves the reader to the given offset for resume support.
type ChunkWriter ¶
type ChunkWriter struct {
// contains filtered or unexported fields
}
ChunkWriter writes chunks to a file with resume support.
func NewChunkWriter ¶
func NewChunkWriter(basePath string, chunkSize int) *ChunkWriter
NewChunkWriter creates a new chunk writer.
func (*ChunkWriter) BasePath ¶
func (w *ChunkWriter) BasePath() string
BasePath returns the base path for writes.
func (*ChunkWriter) GetWrittenOffset ¶
func (w *ChunkWriter) GetWrittenOffset(filePath string) int64
GetWrittenOffset returns the last written offset for a file.
func (*ChunkWriter) WriteChunk ¶
func (w *ChunkWriter) WriteChunk(chunk *Chunk) error
WriteChunk writes a chunk to disk at the correct offset.
type ProgressCallback ¶
type ProgressCallback func(progress protocol.UploadProgress)
ProgressCallback is called when upload progress changes.
type ProgressTracker ¶
type ProgressTracker struct {
// contains filtered or unexported fields
}
ProgressTracker tracks upload progress and notifies callbacks.
func NewProgressTracker ¶
func NewProgressTracker(updateInterval time.Duration) *ProgressTracker
NewProgressTracker creates a new progress tracker.
func (*ProgressTracker) GetSession ¶
func (t *ProgressTracker) GetSession(sessionID string) *UploadSession
GetSession returns a tracked session by ID.
func (*ProgressTracker) NotifyProgress ¶
func (t *ProgressTracker) NotifyProgress(sessionID string)
NotifyProgress manually triggers a progress notification for a session.
func (*ProgressTracker) OnProgress ¶
func (t *ProgressTracker) OnProgress(cb ProgressCallback)
OnProgress registers a callback for progress updates.
func (*ProgressTracker) Start ¶
func (t *ProgressTracker) Start()
Start begins periodic progress notifications.
func (*ProgressTracker) Stop ¶
func (t *ProgressTracker) Stop()
Stop stops the progress tracker. Safe to call multiple times.
func (*ProgressTracker) Track ¶
func (t *ProgressTracker) Track(session *UploadSession)
Track starts tracking a session.
func (*ProgressTracker) Untrack ¶
func (t *ProgressTracker) Untrack(sessionID string)
Untrack stops tracking a session.
type SpeedCalculator ¶
type SpeedCalculator struct {
// contains filtered or unexported fields
}
SpeedCalculator calculates transfer speed.
func NewSpeedCalculator ¶
func NewSpeedCalculator(windowSize time.Duration, maxSamples int) *SpeedCalculator
NewSpeedCalculator creates a new speed calculator.
func (*SpeedCalculator) AddSample ¶
func (c *SpeedCalculator) AddSample(bytes int64)
AddSample adds a new byte count sample.
func (*SpeedCalculator) BytesPerSecond ¶
func (c *SpeedCalculator) BytesPerSecond() float64
BytesPerSecond returns the current transfer speed.
type UploadSession ¶
type UploadSession struct {
ID string `json:"id"`
Config protocol.UploadConfig `json:"config"`
Status protocol.UploadStatus `json:"status"`
TotalBytes int64 `json:"totalBytes"`
TransferredBytes int64 `json:"transferredBytes"`
Files []FileEntry `json:"files"`
CurrentFileIndex int `json:"currentFileIndex"`
StartedAt time.Time `json:"startedAt"`
UpdatedAt time.Time `json:"updatedAt"`
CompletedAt *time.Time `json:"completedAt,omitempty"`
Error string `json:"error,omitempty"`
ChunkOffsets map[string]int64 `json:"chunkOffsets"` // file -> last confirmed offset
// contains filtered or unexported fields
}
UploadSession tracks an active upload operation.
func NewUploadSession ¶
func NewUploadSession(id string, config protocol.UploadConfig, totalBytes int64, files []FileEntry) *UploadSession
NewUploadSession creates a new upload session.
func (*UploadSession) AddProgress ¶
func (s *UploadSession) AddProgress(bytes int64, filePath string, offset int64)
AddProgress adds bytes to the transferred count.
func (*UploadSession) Cancel ¶
func (s *UploadSession) Cancel()
Cancel marks the session as cancelled.
func (*UploadSession) Complete ¶
func (s *UploadSession) Complete()
Complete marks the session as completed.
func (*UploadSession) Fail ¶
func (s *UploadSession) Fail(err string)
Fail marks the session as failed with an error.
func (*UploadSession) GetResumeOffset ¶
func (s *UploadSession) GetResumeOffset(filePath string) int64
GetResumeOffset returns the offset to resume from for a file.
func (*UploadSession) IsActive ¶
func (s *UploadSession) IsActive() bool
IsActive returns true if the session is still active.
func (*UploadSession) Progress ¶
func (s *UploadSession) Progress() protocol.UploadProgress
Progress returns the current progress.
func (*UploadSession) Start ¶
func (s *UploadSession) Start()
Start marks the session as in progress.