Skip to content

Commit

Permalink
fix #175
Browse files Browse the repository at this point in the history
  • Loading branch information
qjfoidnh committed Feb 18, 2022
1 parent 9c97d34 commit 426c7ad
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ iikira/BaiduPCS-Go was largely inspired by [GangZhuo/BaiduPCS](https://github.co
[离线下载](#离线下载), 支持http/https/ftp/电驴/磁力链协议.

# 版本更新
**2022.2.18** v3.8.7:
- fix #175, 在正式上传前即进行文件大小检测

**2022.2.14** v3.8.6:
- fix #160 #173, 修复上传出现空文件的bug
- fix #165, 支持自带提取码的转存链接
Expand Down
16 changes: 8 additions & 8 deletions baidupcs/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (pcs *BaiduPCS) prepareRapidUpload(targetPath, contentMD5, sliceMD5, crc32
// PrepareRapidUpload 秒传文件, 只返回服务器响应数据和错误信息
func (pcs *BaiduPCS) PrepareRapidUpload(targetPath, contentMD5, sliceMD5, crc32 string, length int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationRapidUpload, targetPath, "", length)
pcsError = pcs.CheckIsdir(OperationRapidUpload, targetPath, "", length)
if pcsError != nil {
return nil, pcsError
}
Expand All @@ -299,7 +299,7 @@ func (pcs *BaiduPCS) PrepareRapidUpload(targetPath, contentMD5, sliceMD5, crc32
// PrepareRapidUploadV2 秒传文件, 新接口
func (pcs *BaiduPCS) PrepareRapidUploadV2(targetPath, contentMD5 string, length int64) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationRapidUpload, targetPath, "", length)
pcsError = pcs.CheckIsdir(OperationRapidUpload, targetPath, "", length)
if pcsError != nil {
return nil, pcsError
}
Expand Down Expand Up @@ -399,12 +399,12 @@ func (pcs *BaiduPCS) PrepareLocatePanAPIDownload(fidList ...int64) (dataReadClos
}

// PrepareUpload 上传单个文件, 只返回服务器响应数据和错误信息(分片上传中的预上传部分)
func (pcs *BaiduPCS) PrepareUpload(fileSize int64, policy string, targetPath string, uploadFunc UploadFunc) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
func (pcs *BaiduPCS) PrepareUpload(policy string, targetPath string, uploadFunc UploadFunc) (dataReadCloser io.ReadCloser, pcsError pcserror.Error) {
pcs.lazyInit()
pcsError = pcs.checkIsdir(OperationUpload, targetPath, policy, fileSize)
if pcsError != nil {
return nil, pcsError
}
//pcsError = pcs.checkIsdir(OperationUpload, targetPath, policy)
//if pcsError != nil {
// return nil, pcsError
//}

pcsURL := pcs.generatePCSURL("file", "upload", map[string]string{
"path": targetPath,
Expand Down Expand Up @@ -462,7 +462,7 @@ func (pcs *BaiduPCS) PrepareUploadCreateSuperFile(policy string, checkDir bool,

if checkDir {
// 检查是否为目录
pcsError = pcs.checkIsdir(OperationUploadCreateSuperFile, targetPath, "", 0)
pcsError = pcs.CheckIsdir(OperationUploadCreateSuperFile, targetPath, "", 0)
if pcsError != nil {
return nil, pcsError
}
Expand Down
4 changes: 2 additions & 2 deletions baidupcs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ func (pcs *BaiduPCS) RapidUploadNoCheckDir(targetPath, contentMD5, sliceMD5, crc
}

// Upload 上传单个文件
func (pcs *BaiduPCS) Upload(fileSize int64, policy, targetPath string, uploadFunc UploadFunc) (pcsError pcserror.Error, newpath string) {
dataReadCloser, pcsError := pcs.PrepareUpload(fileSize, policy, targetPath, uploadFunc)
func (pcs *BaiduPCS) Upload(policy, targetPath string, uploadFunc UploadFunc) (pcsError pcserror.Error, newpath string) {
dataReadCloser, pcsError := pcs.PrepareUpload(policy, targetPath, uploadFunc)
if pcsError != nil {
return pcsError, ""
}
Expand Down
2 changes: 1 addition & 1 deletion baidupcs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (pcs *BaiduPCS) Isdir(pcspath string) (fileSize int64, isdir bool, pcsError
return f.Size, f.Isdir, nil
}

func (pcs *BaiduPCS) checkIsdir(op string, targetPath string, policy string, fileSize int64) pcserror.Error {
func (pcs *BaiduPCS) CheckIsdir(op string, targetPath string, policy string, fileSize int64) pcserror.Error {
// 检测文件是否存在于网盘路径
// 很重要, 如果文件存在会直接覆盖!!! 即使是根目录!
onlineSize, isdir, pcsError := pcs.Isdir(targetPath)
Expand Down
11 changes: 6 additions & 5 deletions internal/pcsfunctions/pcsupload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ func (pu *PCSUpload) lazyInit() {
}
}

// Precreate 检查网盘的目标路径是否已存在同名文件
func (pu *PCSUpload) Precreate() (err error) {
return nil
// Precreate 检查网盘的目标路径是否已存在同名文件及路径合法性
func (pu *PCSUpload) Precreate(fileSize int64, policy string) pcserror.Error {
pcsError := pu.pcs.CheckIsdir(baidupcs.OperationUpload, pu.targetPath, policy, fileSize)
return pcsError
}

func (pu *PCSUpload) TmpFile(ctx context.Context, partseq int, partOffset int64, r rio.ReaderLen64) (checksum string, uperr error) {
Expand Down Expand Up @@ -98,12 +99,12 @@ func (pu *PCSUpload) TmpFile(ctx context.Context, partseq int, partOffset int64,
return checksum, pcsError
}

func (pu *PCSUpload) CreateSuperFile(fileSize int64, policy string, checksumList ...string) (err error) {
func (pu *PCSUpload) CreateSuperFile(policy string, checksumList ...string) (err error) {
pu.lazyInit()
//newpath := ""
// 先在网盘目标位置, 上传一个空文件
// 防止出现file does not exist
pcsError, newpath := pu.pcs.Upload(fileSize, policy, pu.targetPath, func(uploadURL string, jar http.CookieJar) (resp *http.Response, err error) {
pcsError, newpath := pu.pcs.Upload(policy, pu.targetPath, func(uploadURL string, jar http.CookieJar) (resp *http.Response, err error) {
mr := multipartreader.NewMultipartReader()
mr.AddFormFile("file", "file", &EmptyReaderLen64{})
mr.CloseMultipart()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (

var (
// Version 版本号
Version = "v3.8.6-devel"
Version = "v3.8.7-devel"

historyFilePath = filepath.Join(pcsconfig.GetConfigDir(), "pcs_command_history.txt")
reloadFn = func(c *cli.Context) error {
Expand Down
5 changes: 3 additions & 2 deletions requester/uploader/multiuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uploader

import (
"context"
"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/pcserror"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil"
"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
"github.com/qjfoidnh/BaiduPCS-Go/requester"
Expand All @@ -14,9 +15,9 @@ import (
type (
// MultiUpload 支持多线程的上传, 可用于断点续传
MultiUpload interface {
Precreate() (err error)
Precreate(fileSize int64, policy string) (err pcserror.Error)
TmpFile(ctx context.Context, partseq int, partOffset int64, readerlen64 rio.ReaderLen64) (checksum string, terr error)
CreateSuperFile(fileSize int64, policy string, checksumList ...string) (cerr error)
CreateSuperFile(policy string, checksumList ...string) (cerr error)
}

// MultiUploader 多线程上传
Expand Down
6 changes: 5 additions & 1 deletion requester/uploader/multiworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (werl *workerList) Readed() int64 {
}

func (muer *MultiUploader) upload() (uperr error) {
err := muer.multiUpload.Precreate(muer.file.Len(), muer.config.Policy)
if err != nil {
return err
}
var (
uploadDeque = lane.NewDeque()
)
Expand Down Expand Up @@ -120,7 +124,7 @@ func (muer *MultiUploader) upload() (uperr error) {
default:
}

cerr := muer.multiUpload.CreateSuperFile(muer.file.Len(), muer.config.Policy, muer.workers.CheckSumList()...)
cerr := muer.multiUpload.CreateSuperFile(muer.config.Policy, muer.workers.CheckSumList()...)
if cerr != nil {
return cerr
}
Expand Down

0 comments on commit 426c7ad

Please sign in to comment.