transfer

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 8 Imported by: 0

Documentation

Overview

Package transfer provides chunked file transfer with resume support.

Index

Constants

View Source
const DefaultChunkSize = 1024 * 1024

DefaultChunkSize is the default size for file chunks (1MB).

Variables

View Source
var ErrChecksumMismatch = &ChecksumError{Message: "checksum mismatch"}

ErrChecksumMismatch is returned when chunk checksum verification fails.

Functions

func CalculateFileChecksum

func CalculateFileChecksum(path string) (string, error)

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) Close

func (r *ChunkReader) Close() error

Close closes the underlying 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 FileEntry

type FileEntry struct {
	RelativePath string `json:"relativePath"`
	Size         int64  `json:"size"`
}

FileEntry represents a file in the upload.

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.

func (*SpeedCalculator) ETA

func (c *SpeedCalculator) ETA(remainingBytes int64) time.Duration

ETA estimates time remaining based on current speed.

func (*SpeedCalculator) Reset

func (c *SpeedCalculator) Reset()

Reset clears all samples.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL