代码拉取完成,页面将自动刷新
package pcsupload
import (
"context"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/requester/multipartreader"
"github.com/iikira/BaiduPCS-Go/requester/rio"
"github.com/iikira/BaiduPCS-Go/requester/uploader"
"net/http"
)
type (
PCSUpload struct {
pcs *baidupcs.BaiduPCS
targetPath string
}
)
func NewPCSUpload(pcs *baidupcs.BaiduPCS, targetPath string) uploader.MultiUpload {
return &PCSUpload{
pcs: pcs,
targetPath: targetPath,
}
}
func (pu *PCSUpload) lazyInit() {
if pu.pcs == nil {
pu.pcs = &baidupcs.BaiduPCS{}
}
}
// Precreate do nothing
func (pu *PCSUpload) Precreate() (err error) {
return nil
}
func (pu *PCSUpload) TmpFile(ctx context.Context, partseq int, partOffset int64, r rio.ReaderLen64) (checksum string, uperr error) {
pu.lazyInit()
var respErr *uploader.MultiError
checksum, pcsError := pu.pcs.UploadTmpFile(func(uploadURL string, jar http.CookieJar) (resp *http.Response, err error) {
client := pcsconfig.Config.HTTPClient()
client.SetCookiejar(jar)
client.SetTimeout(0)
mr := multipartreader.NewMultipartReader()
mr.AddFormFile("uploadedfile", "", r)
mr.CloseMultipart()
doneChan := make(chan struct{}, 1)
go func() {
resp, err = client.Req("POST", uploadURL, mr, nil)
doneChan <- struct{}{}
if resp != nil {
// 不可恢复的错误
switch resp.StatusCode {
case 413:
respErr = &uploader.MultiError{
Terminated: true,
}
}
}
}()
select {
case <-ctx.Done(): // 取消
// 返回, 让那边关闭连接
return resp, ctx.Err()
case <-doneChan:
// return
}
return
})
if respErr != nil {
respErr.Err = pcsError
return checksum, respErr
}
return checksum, pcsError
}
func (pu *PCSUpload) CreateSuperFile(checksumList ...string) (err error) {
pu.lazyInit()
return pu.pcs.UploadCreateSuperFile(pu.targetPath, checksumList...)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。