Skip to content

Commit

Permalink
fix: fix download check exist file
Browse files Browse the repository at this point in the history
  • Loading branch information
flywukong committed Sep 18, 2023
1 parent 26fb8e8 commit 0adb378
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
19 changes: 9 additions & 10 deletions cmd/cmd_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,20 +557,19 @@ func getObject(ctx *cli.Context) error {
}
}

st, err := os.Stat(filePath)
if err == nil {
// If the destination exists and is a directory.
if st.IsDir() {
fmt.Printf("file:%s is a directory\n", filePath)
filePath = filePath + "/" + objectName
}
fmt.Printf("download file:%s already exist\n", filePath)
return nil
filePath, err = checkIfDownloadFileExist(filePath, objectName)
if err != nil {
return toCmdErr(err)
}

dir := filepath.Dir(filePath)
fileName := "." + filepath.Base(filePath) + ".tmp"
tempFilePath := filepath.Join(dir, fileName)

tempFilePath, err = checkIfDownloadFileExist(tempFilePath, objectName)
if err != nil {
return toCmdErr(err)
}

// download to the temp file firstly
fd, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_WRONLY, 0660)
if err != nil {
Expand Down
19 changes: 16 additions & 3 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ 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 ], : %s",
fmt.Printf("\ruploading progress: %.2f%% [ %s / %s ], rate: %10s",
progress, getConvertSize(pr.Current), getConvertSize(pr.Total), getConvertRate(uploadSpeed))
pr.LastPrinted = now
}
Expand Down Expand Up @@ -591,7 +591,7 @@ func (pw *ProgressWriter) printProgress() {
downloadSpeed := float64(downloadedBytes) / elapsed.Seconds()

if now.Sub(pw.LastPrinted) >= time.Second { // print rate every second
fmt.Printf("\rdownloding progress: %.2f%% [ %s / %s ], : %s",
fmt.Printf("\rdownloding progress: %.2f%% [ %s / %s ], rate: %s ",
progress, getConvertSize(pw.Current), getConvertSize(pw.Total), getConvertRate(downloadSpeed))
pw.LastPrinted = now
}
Expand Down Expand Up @@ -623,6 +623,19 @@ func getConvertRate(rate float64) string {
case rate >= KB:
return fmt.Sprintf("%.2f KB/s", rate/KB)
default:
return fmt.Sprintf("%.2f Byte /s", rate)
return fmt.Sprintf("%.2f Byte/s", rate)
}
}

func checkIfDownloadFileExist(filePath, objectName string) (string, error) {
st, err := os.Stat(filePath)
if err == nil {
// If the destination exists and is a directory.
if st.IsDir() {
filePath = filePath + "/" + objectName
return filePath, nil
}
return filePath, fmt.Errorf("download file:%s already exist\n", filePath)
}
return filePath, nil
}

0 comments on commit 0adb378

Please sign in to comment.