mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-12 16:03:56 +08:00
* feat: show upload progress for file tasks * feat: show upload progress for batch file tasks * style: distinguish download and upload phases * style: mark successful task completion * fix: preserve storage save error context * fix: serialize single-file progress updates * fix: stabilize batch upload progress reporting Correct batch upload totals, completion state, actual file sizes, and synchronized progress snapshots. Add regression coverage for concurrent updates and phase transitions. * feat: show per-file transfer progress Format single-file and batch download and upload states with Telegram entities, blockquotes, speeds, transferred sizes, progress bars, concise counters, and accurate confirmation handling. Cover upload retries plus final, error, and cancellation messages with regression tests. * chore: remove transfer progress tests * test: restore critical transfer progress coverage * test: cover interleaved batch transfers * fix: declare progress styles in locale templates
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package ioutil
|
|
|
|
import (
|
|
"io"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var _ io.ReadSeeker = (*ProgressReadSeeker)(nil)
|
|
|
|
// ProgressReadSeeker wraps an io.ReadSeeker and tracks read progress
|
|
type ProgressReadSeeker struct {
|
|
reader io.ReadSeeker
|
|
total atomic.Int64
|
|
read atomic.Int64
|
|
onProgress func(read int64, total int64)
|
|
}
|
|
|
|
// Seek implements io.ReadSeeker.
|
|
func (pr *ProgressReadSeeker) Seek(offset int64, whence int) (int64, error) {
|
|
position, err := pr.reader.Seek(offset, whence)
|
|
if err == nil {
|
|
pr.read.Store(position)
|
|
}
|
|
return position, err
|
|
}
|
|
|
|
// NewProgressReader creates a new ProgressReader
|
|
func NewProgressReader(rs io.ReadSeeker, total int64, onProgress func(read int64, total int64)) *ProgressReadSeeker {
|
|
prs := &ProgressReadSeeker{
|
|
reader: rs,
|
|
total: atomic.Int64{},
|
|
read: atomic.Int64{},
|
|
onProgress: onProgress,
|
|
}
|
|
prs.total.Store(total)
|
|
return prs
|
|
}
|
|
|
|
// Read implements io.Reader
|
|
func (pr *ProgressReadSeeker) Read(p []byte) (int, error) {
|
|
n, err := pr.reader.Read(p)
|
|
if n > 0 {
|
|
pr.read.Add(int64(n))
|
|
read := pr.read.Load()
|
|
|
|
if pr.onProgress != nil {
|
|
pr.onProgress(read, pr.total.Load())
|
|
}
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
// Progress returns the current progress as a float64 between 0 and 1
|
|
func (pr *ProgressReadSeeker) Progress() float64 {
|
|
if pr.total.Load() <= 0 {
|
|
return 0
|
|
}
|
|
return float64(pr.read.Load()) / float64(pr.total.Load())
|
|
}
|
|
|
|
// BytesRead returns the current tracked reader position.
|
|
func (pr *ProgressReadSeeker) BytesRead() int64 {
|
|
return pr.read.Load()
|
|
}
|
|
|
|
// Total returns the total number of bytes
|
|
func (pr *ProgressReadSeeker) Total() int64 {
|
|
return pr.total.Load()
|
|
}
|