Skip to content

Commit

Permalink
fix(189pc): large file upload error (close AlistGo#4417 in AlistGo#4438)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxorcat authored May 27, 2023
1 parent b2f5757 commit 2c1f70f
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 114 deletions.
17 changes: 9 additions & 8 deletions drivers/189pc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"net/http"
"strings"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
Expand All @@ -19,8 +18,7 @@ type Cloud189PC struct {

identity string

client *resty.Client
putClient *resty.Client
client *resty.Client

loginParam *LoginParam
tokenInfo *AppSessionResp
Expand Down Expand Up @@ -51,9 +49,6 @@ func (y *Cloud189PC) Init(ctx context.Context) (err error) {
"Referer": WEB_URL,
})
}
if y.putClient == nil {
y.putClient = base.NewRestyClient().SetTimeout(120 * time.Second)
}

// 避免重复登陆
identity := utils.GetMD5Encode(y.Username + y.Password)
Expand Down Expand Up @@ -266,8 +261,14 @@ func (y *Cloud189PC) Remove(ctx context.Context, obj model.Obj) error {
}

func (y *Cloud189PC) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
if y.RapidUpload {
switch y.UploadMethod {
case "stream":
return y.CommonUpload(ctx, dstDir, stream, up)
case "old":
return y.OldUpload(ctx, dstDir, stream, up)
case "rapid":
return y.FastUpload(ctx, dstDir, stream, up)
default:
return y.CommonUpload(ctx, dstDir, stream, up)
}
return y.CommonUpload(ctx, dstDir, stream, up)
}
16 changes: 16 additions & 0 deletions drivers/189pc/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"encoding/pem"
"fmt"
"math"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -131,3 +132,18 @@ func BoolToNumber(b bool) int {
}
return 0
}

// 计算分片大小
// 对分片数量有限制
// 10MIB 20 MIB 999片
// 50MIB 60MIB 70MIB 80MIB ∞MIB 1999片
func partSize(size int64) int64 {
const DEFAULT = 1024 * 1024 * 10 // 10MIB
if size > DEFAULT*2*999 {
return int64(math.Max(math.Ceil((float64(size)/1999) /*=单个切片大小*/ /float64(DEFAULT)) /*=倍率*/, 5) * DEFAULT)
}
if size > DEFAULT*999 {
return DEFAULT * 2 // 20MIB
}
return DEFAULT
}
3 changes: 2 additions & 1 deletion drivers/189pc/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ type Addition struct {
OrderDirection string `json:"order_direction" type:"select" options:"asc,desc" default:"asc"`
Type string `json:"type" type:"select" options:"personal,family" default:"personal"`
FamilyID string `json:"family_id"`
RapidUpload bool `json:"rapid_upload"`
UploadMethod string `json:"upload_method" type:"select" options:"stream,rapid,old" default:"stream"`
NoUseOcr bool `json:"no_use_ocr"`
}

var config = driver.Config{
Name: "189CloudPC",
DefaultRoot: "-11",
CheckStatus: true,
}

func init() {
Expand Down
86 changes: 82 additions & 4 deletions drivers/189pc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,62 @@ import (

// 居然有四种返回方式
type RespErr struct {
ResCode string `json:"res_code"`
ResCode any `json:"res_code"` // int or string
ResMessage string `json:"res_message"`

Error_ string `json:"error"`

XMLName xml.Name `xml:"error"`
Code string `json:"code" xml:"code"`
Message string `json:"message" xml:"message"`

// Code string `json:"code"`
Msg string `json:"msg"`
Msg string `json:"msg"`

ErrorCode string `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
}

func (e *RespErr) HasError() bool {
switch v := e.ResCode.(type) {
case int, int64, int32:
return v != 0
case string:
return e.ResCode != ""
}
return (e.Code != "" && e.Code != "SUCCESS") || e.ErrorCode != "" || e.Error_ != ""
}

func (e *RespErr) Error() string {
switch v := e.ResCode.(type) {
case int, int64, int32:
if v != 0 {
return fmt.Sprintf("res_code: %d ,res_msg: %s", v, e.ResMessage)
}
case string:
if e.ResCode != "" {
return fmt.Sprintf("res_code: %s ,res_msg: %s", e.ResCode, e.ResMessage)
}
}

if e.Code != "" && e.Code != "SUCCESS" {
if e.Msg != "" {
return fmt.Sprintf("code: %s ,msg: %s", e.Code, e.Msg)
}
if e.Message != "" {
return fmt.Sprintf("code: %s ,msg: %s", e.Code, e.Message)
}
return "code: " + e.Code
}

if e.ErrorCode != "" {
return fmt.Sprintf("err_code: %s ,err_msg: %s", e.ErrorCode, e.ErrorMsg)
}

if e.Error_ != "" {
return fmt.Sprintf("error: %s ,message: %s", e.ErrorCode, e.Message)
}
return ""
}

// 登陆需要的参数
type LoginParam struct {
// 加密后的用户名和密码
Expand Down Expand Up @@ -218,6 +260,42 @@ type Part struct {
RequestHeader string `json:"requestHeader"`
}

/* 第二种上传方式 */
type CreateUploadFileResp struct {
// 上传文件请求ID
UploadFileId int64 `json:"uploadFileId"`
// 上传文件数据的URL路径
FileUploadUrl string `json:"fileUploadUrl"`
// 上传文件完成后确认路径
FileCommitUrl string `json:"fileCommitUrl"`
// 文件是否已存在云盘中,0-未存在,1-已存在
FileDataExists int `json:"fileDataExists"`
}

type GetUploadFileStatusResp struct {
CreateUploadFileResp

// 已上传的大小
DataSize int64 `json:"dataSize"`
Size int64 `json:"size"`
}

func (r *GetUploadFileStatusResp) GetSize() int64 {
return r.DataSize + r.Size
}

type CommitUploadFileResp struct {
XMLName xml.Name `xml:"file"`
Id string `xml:"id"`
Name string `xml:"name"`
Size string `xml:"size"`
Md5 string `xml:"md5"`
CreateDate string `xml:"createDate"`
Rev string `xml:"rev"`
UserId string `xml:"userId"`
}

/* query 加密参数*/
type Params map[string]string

func (p Params) Set(k, v string) {
Expand Down
Loading

0 comments on commit 2c1f70f

Please sign in to comment.