Skip to content

Commit

Permalink
fix: fix upload progress
Browse files Browse the repository at this point in the history
  • Loading branch information
flywukong committed Sep 18, 2023
1 parent 0adb378 commit 8e9269e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
12 changes: 8 additions & 4 deletions cmd/cmd_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,14 @@ func uploadFile(bucketName, objectName, filePath, urlInfo string, ctx *cli.Conte
defer reader.Close()

progressReader := &ProgressReader{
Reader: reader,
Total: objectSize,
StartTime: time.Now(),
LastPrinted: time.Now(),
Reader: reader,
Total: objectSize,
StartTime: time.Now(),
}
if objectSize > objectLargerSize {
progressReader.LastPrinted = time.Now().Add(time.Second * 2)
} else {
progressReader.LastPrinted = time.Now()
}

// if the file is more than 2G , it needs to force use resume uploading
Expand Down
19 changes: 14 additions & 5 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const (

noBalanceErr = "key not found"
maxListMemberNum = 1000
objectLargerSize = 10 * 1024 * 1024
)

var (
Expand Down Expand Up @@ -541,10 +542,11 @@ func parseFileByArg(ctx *cli.Context, argIndex int) (int64, error) {

type ProgressReader struct {
io.Reader
Total int64
Current int64
StartTime time.Time
LastPrinted time.Time
Total int64
Current int64
StartTime time.Time
LastPrinted time.Time
LastPrintedStr string
}

func (pr *ProgressReader) Read(p []byte) (int, error) {
Expand All @@ -561,8 +563,15 @@ func (pr *ProgressReader) printProgress() {
uploadSpeed := float64(pr.Current) / elapsed.Seconds()

if now.Sub(pr.LastPrinted) >= time.Second { // print rate every second
fmt.Printf("\ruploading progress: %.2f%% [ %s / %s ], rate: %10s",
progressStr := fmt.Sprintf("uploading progress: %.2f%% [ %s / %s ], rate: %s",
progress, getConvertSize(pr.Current), getConvertSize(pr.Total), getConvertRate(uploadSpeed))

// Clear current line
fmt.Print("\r", strings.Repeat(" ", len(pr.LastPrintedStr)), "\r")

// Print new progress
fmt.Print(progressStr)

pr.LastPrinted = now
}
}
Expand Down

0 comments on commit 8e9269e

Please sign in to comment.