1 Star 0 Fork 0

J-star / BaiduPCS-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
download.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
konica 提交于 2018-08-04 22:27 . 更新错误处理, 增加搜索文件
package baidupcs
import (
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
"net/http"
"net/url"
)
type (
// DownloadFunc 下载文件处理函数
DownloadFunc func(downloadURL string, jar http.CookieJar) error
// URLInfo 下载链接详情
URLInfo struct {
URLs []struct {
URL string `json:"url"`
} `json:"urls"`
}
locateDownloadJSON struct {
*pcserror.PCSErrInfo
URLInfo
}
)
// URLStrings 返回下载链接数组
func (ui *URLInfo) URLStrings(https bool) (urls []*url.URL) {
urls = make([]*url.URL, 0, len(ui.URLs))
for k := range ui.URLs {
thisURL, err := url.Parse(ui.URLs[k].URL)
if err != nil {
continue
}
thisURL.Scheme = GetHTTPScheme(https)
urls = append(urls, thisURL)
}
return urls
}
// SingleURL 返回单条下载链接
func (ui *URLInfo) SingleURL(https bool) *url.URL {
urls := ui.URLStrings(https)
if len(urls) < 1 {
return nil
}
return urls[0]
}
// DownloadFile 下载单个文件
func (pcs *BaiduPCS) DownloadFile(path string, downloadFunc DownloadFunc) (err error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("file", "download", map[string]string{
"path": path,
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationDownloadFile, pcsURL)
return downloadFunc(pcsURL.String(), pcs.client.Jar)
}
// DownloadStreamFile 下载流式文件
func (pcs *BaiduPCS) DownloadStreamFile(path string, downloadFunc DownloadFunc) (err error) {
pcs.lazyInit()
pcsURL := pcs.generatePCSURL("stream", "download", map[string]string{
"path": path,
})
baiduPCSVerbose.Infof("%s URL: %s\n", OperationDownloadStreamFile, pcsURL)
return downloadFunc(pcsURL.String(), pcs.client.Jar)
}
// LocateDownload 提取下载链接
func (pcs *BaiduPCS) LocateDownload(pcspath string) (info *URLInfo, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareLocateDownload(pcspath)
if dataReadCloser != nil {
defer dataReadCloser.Close()
}
if pcsError != nil {
return nil, pcsError
}
errInfo := pcserror.NewPCSErrorInfo(OperationLocateDownload)
jsonData := locateDownloadJSON{
PCSErrInfo: errInfo,
}
pcsError = handleJSONParse(OperationLocateDownload, dataReadCloser, &jsonData)
if pcsError != nil {
return
}
return &jsonData.URLInfo, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/j-star/BaiduPCS-Go.git
git@gitee.com:j-star/BaiduPCS-Go.git
j-star
BaiduPCS-Go
BaiduPCS-Go
v3.5.6

搜索帮助

344bd9b3 5694891 D2dac590 5694891