-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve cmd of downloading and uploading #90
Changes from 12 commits
869f8c8
6ca2e8a
ba40f9e
0c7a475
362b65c
c317307
280e7bb
bb81e2d
e8d25c6
fd66db9
9ced606
7eb10c5
5905819
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,34 +28,36 @@ import ( | |
) | ||
|
||
const ( | ||
Version = "v0.1.0" | ||
maxFileSize = 10 * 1024 * 1024 * 1024 | ||
publicReadType = "public-read" | ||
privateType = "private" | ||
inheritType = "inherit" | ||
effectAllow = "allow" | ||
effectDeny = "deny" | ||
primarySPFlag = "primarySP" | ||
chargeQuotaFlag = "chargedQuota" | ||
visibilityFlag = "visibility" | ||
paymentFlag = "paymentAddress" | ||
secondarySPFlag = "secondarySPs" | ||
contentTypeFlag = "contentType" | ||
startOffsetFlag = "start" | ||
endOffsetFlag = "end" | ||
recursiveFlag = "recursive" | ||
addMemberFlag = "addMembers" | ||
removeMemberFlag = "removeMembers" | ||
renewMemberFlag = "renewMembers" | ||
groupOwnerFlag = "groupOwner" | ||
groupMemberExpireFlag = "expireTime" | ||
groupIDFlag = "groupId" | ||
granteeFlag = "grantee" | ||
actionsFlag = "actions" | ||
effectFlag = "effect" | ||
expireTimeFlag = "expire" | ||
IdFlag = "id" | ||
DestChainIdFlag = "destChainId" | ||
Version = "v0.1.0" | ||
maxFileSize = 64 * 1024 * 1024 * 1024 | ||
maxPutWithoutResumeSize = 2 * 1024 * 1024 * 1024 | ||
publicReadType = "public-read" | ||
privateType = "private" | ||
inheritType = "inherit" | ||
effectAllow = "allow" | ||
effectDeny = "deny" | ||
primarySPFlag = "primarySP" | ||
chargeQuotaFlag = "chargedQuota" | ||
visibilityFlag = "visibility" | ||
paymentFlag = "paymentAddress" | ||
secondarySPFlag = "secondarySPs" | ||
contentTypeFlag = "contentType" | ||
startOffsetFlag = "start" | ||
endOffsetFlag = "end" | ||
recursiveFlag = "recursive" | ||
bypassSealFlag = "bypassSeal" | ||
addMemberFlag = "addMembers" | ||
removeMemberFlag = "removeMembers" | ||
renewMemberFlag = "renewMembers" | ||
groupOwnerFlag = "groupOwner" | ||
groupMemberExpireFlag = "expireTime" | ||
groupIDFlag = "groupId" | ||
granteeFlag = "grantee" | ||
actionsFlag = "actions" | ||
effectFlag = "effect" | ||
expireTimeFlag = "expire" | ||
IdFlag = "id" | ||
DestChainIdFlag = "destChainId" | ||
|
||
ownerAddressFlag = "owner" | ||
addressFlag = "address" | ||
|
@@ -103,6 +105,7 @@ const ( | |
|
||
noBalanceErr = "key not found" | ||
maxListMemberNum = 1000 | ||
objectLargerSize = 10 * 1024 * 1024 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a little bit confused the naming There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
) | ||
|
||
var ( | ||
|
@@ -536,3 +539,110 @@ func parseFileByArg(ctx *cli.Context, argIndex int) (int64, error) { | |
} | ||
return objectSize, nil | ||
} | ||
|
||
type ProgressReader struct { | ||
io.Reader | ||
Total int64 | ||
Current int64 | ||
StartTime time.Time | ||
LastPrinted time.Time | ||
LastPrintedStr string | ||
} | ||
|
||
func (pr *ProgressReader) Read(p []byte) (int, error) { | ||
n, err := pr.Reader.Read(p) | ||
pr.Current += int64(n) | ||
pr.printProgress() | ||
return n, err | ||
} | ||
|
||
func (pr *ProgressReader) printProgress() { | ||
progress := float64(pr.Current) / float64(pr.Total) * 100 | ||
now := time.Now() | ||
elapsed := now.Sub(pr.StartTime) | ||
uploadSpeed := float64(pr.Current) / elapsed.Seconds() | ||
|
||
if now.Sub(pr.LastPrinted) >= time.Second { // print rate every second | ||
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 | ||
} | ||
} | ||
|
||
type ProgressWriter struct { | ||
io.Writer | ||
Total int64 | ||
Current int64 | ||
StartTime time.Time | ||
LastPrinted time.Time | ||
} | ||
|
||
func (pw *ProgressWriter) Write(p []byte) (int, error) { | ||
n, err := pw.Writer.Write(p) | ||
pw.Current += int64(n) | ||
pw.printProgress() | ||
return n, err | ||
} | ||
|
||
func (pw *ProgressWriter) printProgress() { | ||
progress := float64(pw.Current) / float64(pw.Total) * 100 | ||
now := time.Now() | ||
|
||
elapsed := now.Sub(pw.StartTime) | ||
downloadedBytes := pw.Current | ||
downloadSpeed := float64(downloadedBytes) / elapsed.Seconds() | ||
|
||
if now.Sub(pw.LastPrinted) >= time.Second { // print rate every second | ||
fmt.Printf("\rdownloding progress: %.2f%% [ %s / %s ], rate: %s ", | ||
progress, getConvertSize(pw.Current), getConvertSize(pw.Total), getConvertRate(downloadSpeed)) | ||
pw.LastPrinted = now | ||
} | ||
} | ||
|
||
func getConvertSize(fileSize int64) string { | ||
var convertedSize string | ||
if fileSize > 1<<30 { | ||
convertedSize = fmt.Sprintf("%.2fG", float64(fileSize)/(1<<30)) | ||
} else if fileSize > 1<<20 { | ||
convertedSize = fmt.Sprintf("%.2fM", float64(fileSize)/(1<<20)) | ||
} else if fileSize > 1<<10 { | ||
convertedSize = fmt.Sprintf("%.2fK", float64(fileSize)/(1<<10)) | ||
} else { | ||
convertedSize = fmt.Sprintf("%dB", fileSize) | ||
} | ||
return convertedSize | ||
} | ||
|
||
func getConvertRate(rate float64) string { | ||
const ( | ||
KB = 1024 | ||
MB = 1024 * KB | ||
) | ||
|
||
switch { | ||
case rate >= MB: | ||
return fmt.Sprintf("%.2f MB/s", rate/MB) | ||
case rate >= KB: | ||
return fmt.Sprintf("%.2f KB/s", rate/KB) | ||
default: | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this file does not exist, it will be created for the user using CREAT mode open, so there is no need to return err. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it need to be that long?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After our discussion, we changed the seal printing strategy here. Users can manually cancel the wait for seal or set --bypassSeal now. If they do not cancel, they will wait until seal by default, so this time can actually be infinite.