From d982495ba7348004c5bc49c4e9b6c901900e64da Mon Sep 17 00:00:00 2001 From: qjfoidnh Date: Tue, 7 Jan 2025 22:38:11 +0800 Subject: [PATCH] fix upload failed --- baidupcs/upload.go | 4 +--- .../pcsupload/upload_task_unit.go | 6 ++--- internal/pcsfunctions/pcsupload/utils.go | 16 ++++++++------ requester/uploader/multiuploader.go | 22 +++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/baidupcs/upload.go b/baidupcs/upload.go index dfaa569..4d3b47c 100644 --- a/baidupcs/upload.go +++ b/baidupcs/upload.go @@ -14,13 +14,11 @@ import ( const ( // MaxUploadBlockSize 最大上传的文件分片大小 - MaxUploadBlockSize = 2 * converter.GB + MaxUploadBlockSize = 16 * converter.MB // MinUploadBlockSize 最小的上传的文件分片大小 MinUploadBlockSize = 4 * converter.MB // MaxRapidUploadSize 秒传文件支持的最大文件大小 MaxRapidUploadSize = 20 * converter.GB - // RecommendUploadBlockSize 推荐的上传的文件分片大小 - RecommendUploadBlockSize = 1 * converter.GB // SliceMD5Size 计算 slice-md5 所需的长度 SliceMD5Size = 256 * converter.KB // EmptyContentMD5 空串的md5 diff --git a/internal/pcsfunctions/pcsupload/upload_task_unit.go b/internal/pcsfunctions/pcsupload/upload_task_unit.go index dbec62c..e86c25c 100644 --- a/internal/pcsfunctions/pcsupload/upload_task_unit.go +++ b/internal/pcsfunctions/pcsupload/upload_task_unit.go @@ -34,8 +34,8 @@ type ( PCS *baidupcs.BaiduPCS UploadingDatabase *UploadingDatabase // 数据库 Parallel int - NoRapidUpload bool // 禁用秒传 - NoSplitFile bool // 禁用分片上传 + NoRapidUpload bool // 禁用秒传 + NoSplitFile bool // 禁用分片上传 Policy string // 上传重名文件策略 UploadStatistic *UploadStatistic @@ -57,7 +57,7 @@ const ( ) const ( - StrUploadFailed = "上传文件失败" + StrUploadFailed = "上传文件失败" DefaultPrintFormat = "\r[%s] ↑ %s/%s %s/s in %s ............" DefaultContentSize = 4 * converter.KB ) diff --git a/internal/pcsfunctions/pcsupload/utils.go b/internal/pcsfunctions/pcsupload/utils.go index 85a6062..4ebd23e 100644 --- a/internal/pcsfunctions/pcsupload/utils.go +++ b/internal/pcsfunctions/pcsupload/utils.go @@ -4,19 +4,21 @@ import ( "crypto/md5" "fmt" "github.com/qjfoidnh/BaiduPCS-Go/baidupcs" + "github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter" "io" "strconv" ) func getBlockSize(fileSize int64) int64 { - blockNum := fileSize / baidupcs.MinUploadBlockSize - if blockNum > 999 { - return fileSize/999 + 1 + blockSize := baidupcs.MinUploadBlockSize + if fileSize > 1*converter.GB && fileSize < 4*converter.GB { + blockSize = 2 * baidupcs.MinUploadBlockSize + } else if fileSize >= 4*converter.GB { + blockSize = baidupcs.MaxUploadBlockSize } - return baidupcs.MinUploadBlockSize + return blockSize } - func creaetDataOffset(contentMD5 string, uk, dataTime, fileSize, subSize int64) (offset int64, err error) { h := md5.New() ts := strconv.FormatInt(dataTime, 10) @@ -27,10 +29,10 @@ func creaetDataOffset(contentMD5 string, uk, dataTime, fileSize, subSize int64) if err != nil { return } - if fileSize - subSize + 1 <= 1 { + if fileSize-subSize+1 <= 1 { offset = 0 return } offset = rawOffset % (fileSize - subSize + 1) return -} \ No newline at end of file +} diff --git a/requester/uploader/multiuploader.go b/requester/uploader/multiuploader.go index 3df3797..2a6c875 100644 --- a/requester/uploader/multiuploader.go +++ b/requester/uploader/multiuploader.go @@ -47,10 +47,10 @@ type ( // MultiUploaderConfig 多线程上传配置 MultiUploaderConfig struct { - Parallel int // 上传并发量 - BlockSize int64 // 上传分块 - MaxRate int64 // 限制最大上传速度 - Policy string // 文件重名策略 + Parallel int // 上传并发量 + BlockSize int64 // 上传分块 + MaxRate int64 // 限制最大上传速度 + Policy string // 文件重名策略 } ) @@ -85,7 +85,7 @@ func (muer *MultiUploader) lazyInit() { muer.config.Parallel = 4 } if muer.config.BlockSize <= 0 { - muer.config.BlockSize = 1 * converter.GB + muer.config.BlockSize = 16 * converter.MB } if muer.speedsStat == nil { muer.speedsStat = &speeds.Speeds{} @@ -167,32 +167,32 @@ func (muer *MultiUploader) Cancel() { close(muer.canceled) } -//OnExecute 设置开始上传事件 +// OnExecute 设置开始上传事件 func (muer *MultiUploader) OnExecute(onExecuteEvent requester.Event) { muer.onExecuteEvent = onExecuteEvent } -//OnSuccess 设置成功上传事件 +// OnSuccess 设置成功上传事件 func (muer *MultiUploader) OnSuccess(onSuccessEvent requester.Event) { muer.onSuccessEvent = onSuccessEvent } -//OnFinish 设置结束上传事件 +// OnFinish 设置结束上传事件 func (muer *MultiUploader) OnFinish(onFinishEvent requester.Event) { muer.onFinishEvent = onFinishEvent } -//OnCancel 设置取消上传事件 +// OnCancel 设置取消上传事件 func (muer *MultiUploader) OnCancel(onCancelEvent requester.Event) { muer.onCancelEvent = onCancelEvent } -//OnError 设置上传发生错误事件 +// OnError 设置上传发生错误事件 func (muer *MultiUploader) OnError(onErrorEvent requester.EventOnError) { muer.onErrorEvent = onErrorEvent } -//OnUploadStatusEvent 设置上传状态事件 +// OnUploadStatusEvent 设置上传状态事件 func (muer *MultiUploader) OnUploadStatusEvent(f UploadStatusFunc) { muer.onUploadStatusEvent = f }