Skip to content

Commit

Permalink
fix #286, fix #336, 修复上传错误, 可自定义上传最大线程
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Aug 22, 2018
1 parent e403a71 commit ba09943
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/RELEASE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* 移动设备:
请选择对应的系统(android, darwin(ios系统)), 对应的CPU架构 (一般情况下是 arm, 除了少数手机的CPU架构要选 amd, 例如联想K800, 联想K900等), 对应的CPU或操作系统位数 (详见上表), 下载.

注意: Android5.0 以上的设备请不要下载使用linux版本的, 否则网络访问可能会出现问题.

## 注意

相关的关键词, 均能在文件名中找到.
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,20 @@ BaiduPCS-Go upload <本地文件/目录的路径1> <文件/目录2> <文件/目
BaiduPCS-Go u <本地文件/目录的路径1> <文件/目录2> <文件/目录3> ... <目标目录>
```

* 上传的文件将会保存到, <目标目录>.
* 上传默认采用分片上传的方式, 上传的文件将会保存到, <目标目录>.

* 遇到同名文件将会自动覆盖!!

* 当上传的文件名和网盘的目录名称相同时, 不会覆盖目录, 防止丢失数据.

* 注意: 在上传完成后的修复md5, 不一定能成功, 但文件本身是没问题的, 只是服务器记录的md5错误而已.

#### 注意:

* 分片上传之后, 服务器可能会记录到错误的文件md5, 程序会在上传完成后的修复md5, 修复md5不一定能成功, 但文件的完整性是没问题的.

* 禁用分片上传可以保证服务器记录到正确的md5.

* 禁用分片上传时只能使用单线程上传, 指定的单个文件上传最大线程数将会无效.

#### 例子:
```
Expand Down
4 changes: 2 additions & 2 deletions baidupcs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
const (
// MaxUploadBlockSize 最大上传的文件分片大小
MaxUploadBlockSize = 2 * converter.GB
// MinUploadBlockSize 推荐的上传的文件分片大小
MinUploadBlockSize = 128 * converter.MB
// MinUploadBlockSize 最小的上传的文件分片大小
MinUploadBlockSize = 4 * converter.MB
// RecommendUploadBlockSize 推荐的上传的文件分片大小
RecommendUploadBlockSize = 1 * converter.GB
// DefaultSliceMD5 默认的长度为32的slicemd5
Expand Down
3 changes: 0 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ RicePack() {
rice -i github.com/iikira/BaiduPCS-Go/internal/pcsweb append --exec "$output/$1/$2"
}

GOMIPS=softfloat Build $name-$version"-linux-mipsle" linux mipsle
exit

# Android
export NDK_INSTALL=$ANDROID_NDK_ROOT/bin
# CC=$NDK_INSTALL/arm-linux-androideabi-4.9/bin/arm-linux-androideabi-gcc ArmBuild $name-$version"-android-16-armv5" android arm 5
Expand Down
10 changes: 9 additions & 1 deletion internal/pcscommand/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/internal/pcsfunctions/pcsupload"
"github.com/iikira/BaiduPCS-Go/pcscache"
"github.com/iikira/BaiduPCS-Go/pcsutil"
Expand All @@ -28,6 +29,7 @@ const (

type (
UploadOptions struct {
Parallel int
NotRapidUpload bool
NotFixMD5 bool
NotSplitFile bool // 禁用分片上传
Expand Down Expand Up @@ -98,6 +100,11 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
opt = &UploadOptions{}
}

// 检测opt
if opt.Parallel <= 0 {
opt.Parallel = pcsconfig.Config.MaxParallel()
}

absSavePath, err := getAbsPath(savePath)
if err != nil {
fmt.Printf("警告: 上传文件, 获取网盘路径 %s 错误, %s\n", savePath, err)
Expand Down Expand Up @@ -308,7 +315,7 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
task.uploadInfo.SliceMD5Sum()

// 经测试, 文件的 crc32 值并非秒传文件所必需
// task.uploadInfo.crc32Sum()
// task.uploadInfo.Crc32Sum()

err = pcs.RapidUpload(task.savePath, hex.EncodeToString(task.uploadInfo.MD5), hex.EncodeToString(task.uploadInfo.SliceMD5), fmt.Sprint(task.uploadInfo.CRC32), task.uploadInfo.Length)
if err == nil {
Expand All @@ -328,6 +335,7 @@ func RunUpload(localPaths []string, savePath string, opt *UploadOptions) {
task.step = StepUploadUpload
{
muer := uploader.NewMultiUploader(pcsupload.NewPCSUpload(pcs, task.savePath), rio.NewFileReaderAtLen64(task.uploadInfo.File))
muer.SetParallel(opt.Parallel)

var blockSize int64
if opt.NotSplitFile {
Expand Down
12 changes: 7 additions & 5 deletions internal/pcsfunctions/pcsupload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ func (pu *PCSUpload) TmpFile(ctx context.Context, partseq int, partOffset int64,
resp, err = client.Req("POST", uploadURL, mr, nil)
doneChan <- struct{}{}

// 不可恢复的错误
switch resp.StatusCode {
case 413:
respErr = &uploader.MultiError{
Terminated: true,
if resp != nil {
// 不可恢复的错误
switch resp.StatusCode {
case 413:
respErr = &uploader.MultiError{
Terminated: true,
}
}
}
}()
Expand Down
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,15 @@ func main() {
Usage: "上传文件/目录",
UsageText: app.Name + " upload <本地文件/目录的路径1> <文件/目录2> <文件/目录3> ... <目标目录>",
Description: `
上传的文件将会保存到, <目标目录>.
上传默认采用分片上传的方式, 上传的文件将会保存到, <目标目录>.
遇到同名文件将会自动覆盖!!
当上传的文件名和网盘的目录名称相同时, 不会覆盖目录, 防止丢失数据.
注意: 在上传完成后的修复md5, 不一定能成功, 但文件本身是没问题的, 只是服务器记录的md5错误而已.
注意:
分片上传之后, 服务器可能会记录到错误的文件md5, 程序会在上传完成后的修复md5, 修复md5不一定能成功, 但文件的完整性是没问题的.
禁用分片上传可以保证服务器记录到正确的md5.
禁用分片上传时只能使用单线程上传, 指定的单个文件上传最大线程数将会无效.
示例:
Expand Down Expand Up @@ -1131,13 +1135,18 @@ func main() {
subArgs := c.Args()

pcscommand.RunUpload(subArgs[:c.NArg()-1], subArgs[c.NArg()-1], &pcscommand.UploadOptions{
Parallel: c.Int("p"),
NotRapidUpload: c.Bool("norapid"),
NotFixMD5: c.Bool("nofix"),
NotSplitFile: c.Bool("nosplit"),
})
return nil
},
Flags: []cli.Flag{
cli.IntFlag{
Name: "p",
Usage: "指定单个文件上传的最大线程数",
},
cli.BoolFlag{
Name: "norapid",
Usage: "不检测秒传",
Expand Down Expand Up @@ -1554,7 +1563,7 @@ func main() {
例子:
BaiduPCS-Go config set -appid=260149
BaiduPCS-Go config set -enable_https=false
BaiduPCS-Go config set -user_agent="chrome"
BaiduPCS-Go config set -user_agent="netdisk;1.0"
BaiduPCS-Go config set -cache_size 16384 -max_parallel 200 -savedir D:/download`,
Action: func(c *cli.Context) error {
if c.NumFlags() <= 0 || c.NArg() > 0 {
Expand Down Expand Up @@ -1609,12 +1618,12 @@ func main() {
Usage: "浏览器标识",
},
cli.IntFlag{
Name: "cache_size",
Usage: "下载缓存",
Name: "max_parallel",
Usage: "上传/下载网络连接的最大并发量",
},
cli.IntFlag{
Name: "max_parallel",
Usage: "下载最大并发量",
Name: "cache_size",
Usage: "下载缓存",
},
cli.IntFlag{
Name: "max_download_load",
Expand Down
17 changes: 15 additions & 2 deletions pcsverbose/pcsverbose.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func (pv *PCSVerbose) Warnf(format string, a ...interface{}) {
func Verbosef(format string, a ...interface{}) (n int, err error) {
if IsVerbose {
for _, Output := range Outputs {
n, err = fmt.Fprintf(Output, format, a...)
n1, err := fmt.Fprintf(Output, TimePrefix()+" "+format, a...)
n += n1
if err != nil {
return n, err
}
}
}
return
Expand All @@ -66,7 +70,16 @@ func Verbosef(format string, a ...interface{}) (n int, err error) {
func Verboseln(a ...interface{}) (n int, err error) {
if IsVerbose {
for _, Output := range Outputs {
n, err = fmt.Fprintln(Output, a...)
n1, err := fmt.Fprint(Output, TimePrefix()+" ")
n += n1
if err != nil {
return n, err
}
n2, err := fmt.Fprintln(Output, a...)
n += n2
if err != nil {
return n, err
}
}
}
return
Expand Down
5 changes: 5 additions & 0 deletions pcsverbose/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pcsverbose

import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"io"
"io/ioutil"
)
Expand All @@ -11,3 +12,7 @@ func PrintReader(r io.Reader) {
b, _ := ioutil.ReadAll(r)
fmt.Printf("%s\n", b)
}

func TimePrefix() string {
return "[" + pcstime.BeijingTimeOption("Refer") + "]"
}
8 changes: 6 additions & 2 deletions requester/uploader/multiupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func NewMultiUploader(multiUpload MultiUpload, file rio.ReaderAtLen64) *MultiUpl
return &MultiUploader{
multiUpload: multiUpload,
file: file,
blockSize: 1 * converter.GB,
parallel: 10,
}
}

Expand Down Expand Up @@ -76,6 +74,12 @@ func (muer *MultiUploader) lazyInit() {
if muer.updateInstanceStateChan == nil {
muer.updateInstanceStateChan = make(chan struct{}, 1)
}
if muer.parallel <= 0 {
muer.parallel = 10
}
if muer.blockSize <= 0 {
muer.blockSize = 1 * converter.GB
}
}

func (muer *MultiUploader) check() {
Expand Down

0 comments on commit ba09943

Please sign in to comment.