mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-14 00:44:01 +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
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package batchtfile
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// UploadProgressTracker optionally extends a batch progress tracker with a
|
|
// distinct aggregate upload phase.
|
|
type UploadProgressTracker interface {
|
|
OnUploadStart(ctx context.Context, info TaskInfo, total int64)
|
|
OnUploadProgress(ctx context.Context, info TaskInfo, uploaded, total int64)
|
|
}
|
|
|
|
func (t *Task) startUpload(ctx context.Context) {
|
|
tracker, ok := t.Progress.(UploadProgressTracker)
|
|
if !ok {
|
|
return
|
|
}
|
|
t.uploadMu.Lock()
|
|
defer t.uploadMu.Unlock()
|
|
t.uploadOnce.Do(func() {
|
|
tracker.OnUploadStart(ctx, t, t.uploadTotalSize.Load())
|
|
})
|
|
}
|
|
|
|
func (t *Task) uploadCallback(ctx context.Context, id string) func(uploaded, total int64) {
|
|
return func(uploaded, total int64) {
|
|
tracker, ok := t.Progress.(UploadProgressTracker)
|
|
if !ok || uploaded < 0 {
|
|
return
|
|
}
|
|
t.startUpload(ctx)
|
|
if total > 0 && uploaded > total {
|
|
uploaded = total
|
|
}
|
|
|
|
t.uploadMu.Lock()
|
|
defer t.uploadMu.Unlock()
|
|
becameConfirming := t.recordItemUpload(id, uploaded, total, time.Now())
|
|
if t.uploaded == nil {
|
|
t.uploaded = make(map[string]int64)
|
|
}
|
|
previous, tracked := t.uploaded[id]
|
|
if !tracked || uploaded > previous {
|
|
t.uploaded[id] = uploaded
|
|
}
|
|
var aggregate int64
|
|
for _, current := range t.uploaded {
|
|
aggregate += current
|
|
}
|
|
uploadTotal := t.uploadTotalSize.Load()
|
|
if aggregate > uploadTotal {
|
|
aggregate = uploadTotal
|
|
}
|
|
tracker.OnUploadProgress(ctx, t, aggregate, uploadTotal)
|
|
if becameConfirming {
|
|
t.notifyStateChange(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *Task) recordDownloadComplete(id string, uploadSize int64) {
|
|
t.uploadMu.Lock()
|
|
defer t.uploadMu.Unlock()
|
|
if uploadSize > 0 {
|
|
t.uploadTotalSize.Add(uploadSize)
|
|
}
|
|
t.recordItemDownloaded(id, uploadSize)
|
|
}
|