Skip to content

Commit

Permalink
Updated fileSize description
Browse files Browse the repository at this point in the history
  • Loading branch information
Dliv3 committed Aug 21, 2019
1 parent 9b07c42 commit 11fbc7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions admin/dispather/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func SendDownloadCmd(peerNode *node.Node, remotePath string, localPath string) b
}

if downloadPacketRet.FileLen > 1024*1024*100 {
fmt.Print("this file is too large (>100M), do you still want to download it? (y/n) ")
fmt.Printf("this file is too large (%s), do you still want to download it? (y/n) ",
utils.GetFileSizeDescription(downloadPacketRet.FileLen))
var choise string
fmt.Scanf("%s", &choise)
if choise != "y" {
Expand Down Expand Up @@ -265,7 +266,8 @@ func SendUploadCmd(peerNode *node.Node, localPath string, remotePath string) boo
// 如果文件过大,提醒用户选择是否继续上次(过大的文件会影响其他命令数据的传输效率)
var fileSize = utils.GetFileSize(localPath)
if fileSize > 1024*1024*100 {
fmt.Print("this file is too large(>100M), do you still want to upload it? (y/n) ")
fmt.Printf("this file is too large (%s), do you still want to upload it? (y/n) ",
utils.GetFileSizeDescription(uint64(fileSize)))
var choise string
fmt.Scanf("%s", &choise)
if choise != "y" {
Expand Down
15 changes: 15 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ func GetFileSize(path string) int64 {
return fileSize
}

// GetFileSizeDescription 10.00G / 100.00M
func GetFileSizeDescription(fileSize uint64) string {
fileSizeFloat := float64(fileSize)
const K = 1024 * 1.0
const M = K * 1024
const G = M * 1024
if fileSizeFloat >= G {
return fmt.Sprintf("%0.2fG", fileSizeFloat/G)
}
if fileSizeFloat >= M {
return fmt.Sprintf("%0.2fM", fileSizeFloat/M)
}
return fmt.Sprintf("%0.2fK", fileSizeFloat/K)
}

/* -------------- Calculate structure size ----------------- */
func PacketSize(packet interface{}) (uint64, error) {

Expand Down

0 comments on commit 11fbc7c

Please sign in to comment.