feat(task): enhance download progress reporting and add speed calculation

This commit is contained in:
krau
2025-02-12 11:06:25 +08:00
parent a32bf43cdc
commit a17492d4ae
3 changed files with 19 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/celestix/gotgproto/ext" "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg" "github.com/gotd/td/tg"
@@ -23,7 +24,7 @@ func processPendingTask(task *types.Task) error {
ctx := task.Ctx.(*ext.Context) ctx := task.Ctx.(*ext.Context)
ctx.EditMessage(task.ChatID, &tg.MessagesEditMessageRequest{ ctx.EditMessage(task.ChatID, &tg.MessagesEditMessageRequest{
Message: "正在下载: " + task.String(), Message: "正在下载: " + task.FileName(),
ID: task.ReplyMessageID, ID: task.ReplyMessageID,
}) })
@@ -67,8 +68,10 @@ func processPendingTask(task *types.Task) error {
if task.File.FileSize < 1024*1024*50 || int(progress)%(100/barTotalCount) != 0 { if task.File.FileSize < 1024*1024*50 || int(progress)%(100/barTotalCount) != 0 {
return return
} }
text := fmt.Sprintf("正在下载: %s\n[%s] %.2f%%", text := fmt.Sprintf("正在处理下载任务\n文件名: %s\n保存路径: %s\n平均速度: %s\n当前进度: [%s] %.2f%%",
task.String(), task.FileName(),
fmt.Sprintf("[%s]:%s", task.Storage, task.StoragePath),
getSpeed(bytesRead, task.StartTime),
getProgressBar(progress, barTotalCount), getProgressBar(progress, barTotalCount),
progress, progress,
) )
@@ -91,7 +94,7 @@ func processPendingTask(task *types.Task) error {
return fmt.Errorf("Failed to create file: %w", err) return fmt.Errorf("Failed to create file: %w", err)
} }
defer dest.Close() defer dest.Close()
task.StartTime = time.Now()
if _, err := io.CopyN(dest, readCloser, task.File.FileSize); err != nil { if _, err := io.CopyN(dest, readCloser, task.File.FileSize); err != nil {
return fmt.Errorf("Failed to download file: %w", err) return fmt.Errorf("Failed to download file: %w", err)
} }

View File

@@ -52,7 +52,7 @@ func cleanCacheFile(destPath string) {
func calculateBarTotalCount(fileSize int64) int { func calculateBarTotalCount(fileSize int64) int {
barTotalCount := 5 barTotalCount := 5
if fileSize > 1024*1024*1000 { if fileSize > 1024*1024*1000 {
barTotalCount = 50 barTotalCount = 40
} else if fileSize > 1024*1024*500 { } else if fileSize > 1024*1024*500 {
barTotalCount = 20 barTotalCount = 20
} else if fileSize > 1024*1024*200 { } else if fileSize > 1024*1024*200 {
@@ -60,3 +60,12 @@ func calculateBarTotalCount(fileSize int64) int {
} }
return barTotalCount return barTotalCount
} }
func getSpeed(bytesRead int64, startTime time.Time) string {
if startTime.IsZero() {
return "0MB/s"
}
elapsed := time.Since(startTime)
speed := float64(bytesRead) / 1024 / 1024 / elapsed.Seconds()
return fmt.Sprintf("%.2fMB/s", speed)
}

View File

@@ -3,6 +3,7 @@ package types
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"github.com/gotd/td/tg" "github.com/gotd/td/tg"
) )
@@ -34,6 +35,7 @@ type Task struct {
File *File File *File
Storage StorageType Storage StorageType
StoragePath string StoragePath string
StartTime time.Time
MessageID int MessageID int
ChatID int64 ChatID int64