代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。