Skip to content

Commit

Permalink
下载支持以流式文件的方式下载, 尝试修复#184, #174
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed May 18, 2018
1 parent 1706635 commit 8f944b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions internal/pcscommand/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type DownloadOption struct {
IsOverwrite bool
IsShareDownload bool
IsLocateDownload bool
IsStreaming bool
SaveTo string
Parallel int
}
Expand Down Expand Up @@ -355,7 +356,7 @@ func RunDownload(paths []string, option DownloadOption) {
setupHTTPClient(client)
err = download(task.ID, dlink, task.savePath, dlinks, client, cfg, option.IsPrintStatus, option.IsExecutedPermission)
} else {
err = pcs.DownloadFile(task.path, func(downloadURL string, jar *cookiejar.Jar) error {
dfunc := func(downloadURL string, jar *cookiejar.Jar) error {
h := requester.NewHTTPClient()
h.SetCookiejar(jar)
h.SetKeepAlive(true)
Expand All @@ -368,7 +369,12 @@ func RunDownload(paths []string, option DownloadOption) {
}

return nil
})
}
if option.IsStreaming {
err = pcs.DownloadStreamFile(task.path, dfunc)
} else {
err = pcs.DownloadFile(task.path, dfunc)
}
}

if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ func main() {
return
})

fmt.Printf("提示: 方向键上下可切换历史命令.\n")
fmt.Printf("提示: Ctrl + A 跳转命令首.\n")
fmt.Printf("提示: Ctrl + E 跳转命令尾.\n")
fmt.Printf("提示: 输入 help 获取帮助.\n")

for {
Expand Down Expand Up @@ -936,6 +939,7 @@ func main() {
IsOverwrite: c.Bool("ow"),
IsShareDownload: c.Bool("share"),
IsLocateDownload: c.Bool("locate"),
IsStreaming: c.Bool("stream"),
SaveTo: saveTo,
Parallel: c.Int("p"),
})
Expand Down Expand Up @@ -966,6 +970,10 @@ func main() {
Name: "x",
Usage: "为文件加上执行权限, (windows系统无效)",
},
cli.BoolFlag{
Name: "stream",
Usage: "以流式文件的方式下载",
},
cli.BoolFlag{
Name: "share",
Usage: "以分享文件的方式获取下载链接来下载",
Expand Down
8 changes: 5 additions & 3 deletions requester/downloader/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (wer *Worker) Execute() {
}

// 已完成
if wer.wrange.Len() == 0 {
if wer.wrange.Len() <= 0 {
wer.status.statusCode = StatusCodeSuccessed
return
}
Expand Down Expand Up @@ -333,7 +333,8 @@ func (wer *Worker) Execute() {
// 初始化数据
var readErr error
n = 0
// 线程未被分配

// 读取数据
for n < len(buf) && readErr == nil && (single || wer.wrange.Len() > 0) {
nn, readErr = resp.Body.Read(buf[n:])
nn64 = int64(nn)
Expand Down Expand Up @@ -399,8 +400,9 @@ func (wer *Worker) Execute() {
fallthrough
case readErr == io.EOF:
fallthrough
case wer.wrange.Len() == 0:
case wer.wrange.Len() <= 0:
// 下载完成
// 小于0可能是因为 worker 被 duplicate
wer.status.statusCode = StatusCodeSuccessed
return
default:
Expand Down

0 comments on commit 8f944b3

Please sign in to comment.