From 06b0a4fc5862bfbee26f8ec848ce45b5e9cad0ac Mon Sep 17 00:00:00 2001 From: SoY0ung <752143070@qq.com> Date: Fri, 12 Jan 2024 20:55:29 +0800 Subject: [PATCH 1/3] fix(chaoxing):fix JSON parsing error in `content` field --- drivers/chaoxing/types.go | 96 +++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/drivers/chaoxing/types.go b/drivers/chaoxing/types.go index a1ce13c3019..20a225c0f9b 100644 --- a/drivers/chaoxing/types.go +++ b/drivers/chaoxing/types.go @@ -1,7 +1,9 @@ package chaoxing import ( + "encoding/json" "fmt" + "strconv" "time" "github.com/alist-org/alist/v3/internal/model" @@ -88,44 +90,69 @@ type UserAuth struct { } `json:"operationAuth"` } +// 手机端学习通上传的文件的json内容(content字段)与网页端上传的有所不同 +// 网页端json `"puid": 54321, "size": 12345` +// 手机端json `"puid": "54321". "size": "12345"` +type int_str int + +// UnmarshalJSON 实现 json.Unmarshaler 接口 +func (ios *int_str) UnmarshalJSON(data []byte) error { + // 解析为int + var intValue int + if err := json.Unmarshal(data, &intValue); err == nil { + *ios = int_str(intValue) + return nil + } + // 解析string再转int + var strValue string + if err := json.Unmarshal(data, &strValue); err == nil { + intValue, err := strconv.Atoi(strValue) + if err != nil { + return fmt.Errorf("json: cannot unmarshal data into Go struct field .list.content._ of type int_string") + } + *ios = int_str(intValue) + } + return nil +} + type File struct { Cataid int `json:"cataid"` Cfid int `json:"cfid"` Content struct { - Cfid int `json:"cfid"` - Pid int `json:"pid"` - FolderName string `json:"folderName"` - ShareType int `json:"shareType"` - Preview string `json:"preview"` - Filetype string `json:"filetype"` - PreviewURL string `json:"previewUrl"` - IsImg bool `json:"isImg"` - ParentPath string `json:"parentPath"` - Icon string `json:"icon"` - Suffix string `json:"suffix"` - Duration int `json:"duration"` - Pantype string `json:"pantype"` - Puid int `json:"puid"` - Filepath string `json:"filepath"` - Crc string `json:"crc"` - Isfile bool `json:"isfile"` - Residstr string `json:"residstr"` - ObjectID string `json:"objectId"` - Extinfo string `json:"extinfo"` - Thumbnail string `json:"thumbnail"` - Creator int `json:"creator"` - ResTypeValue int `json:"resTypeValue"` - UploadDateFormat string `json:"uploadDateFormat"` - DisableOpt bool `json:"disableOpt"` - DownPath string `json:"downPath"` - Sort int `json:"sort"` - Topsort int `json:"topsort"` - Restype string `json:"restype"` - Size int `json:"size"` - UploadDate string `json:"uploadDate"` - FileSize string `json:"fileSize"` - Name string `json:"name"` - FileID string `json:"fileId"` + Cfid int `json:"cfid"` + Pid int `json:"pid"` + FolderName string `json:"folderName"` + ShareType int `json:"shareType"` + Preview string `json:"preview"` + Filetype string `json:"filetype"` + PreviewURL string `json:"previewUrl"` + IsImg bool `json:"isImg"` + ParentPath string `json:"parentPath"` + Icon string `json:"icon"` + Suffix string `json:"suffix"` + Duration int `json:"duration"` + Pantype string `json:"pantype"` + Puid int_str `json:"puid"` + Filepath string `json:"filepath"` + Crc string `json:"crc"` + Isfile bool `json:"isfile"` + Residstr string `json:"residstr"` + ObjectID string `json:"objectId"` + Extinfo string `json:"extinfo"` + Thumbnail string `json:"thumbnail"` + Creator int `json:"creator"` + ResTypeValue int `json:"resTypeValue"` + UploadDateFormat string `json:"uploadDateFormat"` + DisableOpt bool `json:"disableOpt"` + DownPath string `json:"downPath"` + Sort int `json:"sort"` + Topsort int `json:"topsort"` + Restype string `json:"restype"` + Size int_str `json:"size"` + UploadDate string `json:"uploadDate"` + FileSize string `json:"fileSize"` + Name string `json:"name"` + FileID string `json:"fileId"` } `json:"content"` CreatorID int `json:"creatorId"` DesID string `json:"des_id"` @@ -204,7 +231,6 @@ type UploadFileDataRsp struct { } `json:"data"` } - type UploadDoneParam struct { Cataid string `json:"cataid"` Key string `json:"key"` From e5e544d8ab0910924105d72b4bf46215df771faf Mon Sep 17 00:00:00 2001 From: SoY0ung <752143070@qq.com> Date: Sat, 13 Jan 2024 10:16:15 +0800 Subject: [PATCH 2/3] fix(chaoxing): optimizing `UnmarshalJSON` implementation --- drivers/chaoxing/types.go | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/drivers/chaoxing/types.go b/drivers/chaoxing/types.go index 20a225c0f9b..ba636ec1dd0 100644 --- a/drivers/chaoxing/types.go +++ b/drivers/chaoxing/types.go @@ -1,7 +1,7 @@ package chaoxing import ( - "encoding/json" + "bytes" "fmt" "strconv" "time" @@ -95,23 +95,13 @@ type UserAuth struct { // 手机端json `"puid": "54321". "size": "12345"` type int_str int -// UnmarshalJSON 实现 json.Unmarshaler 接口 +// json 字符串数字和纯数字解析 func (ios *int_str) UnmarshalJSON(data []byte) error { - // 解析为int - var intValue int - if err := json.Unmarshal(data, &intValue); err == nil { - *ios = int_str(intValue) - return nil - } - // 解析string再转int - var strValue string - if err := json.Unmarshal(data, &strValue); err == nil { - intValue, err := strconv.Atoi(strValue) - if err != nil { - return fmt.Errorf("json: cannot unmarshal data into Go struct field .list.content._ of type int_string") - } - *ios = int_str(intValue) + intValue, err := strconv.Atoi(string(bytes.Trim(data, "\""))) + if err != nil { + return err } + *ios = int_str(intValue) return nil } From 7e36cd621a272c45de7cc9dcdf4fa3e9026f47e0 Mon Sep 17 00:00:00 2001 From: SoY0ung <752143070@qq.com> Date: Sat, 13 Jan 2024 10:56:31 +0800 Subject: [PATCH 3/3] fix(chaoxing): use `objectID` when is empty --- drivers/chaoxing/util.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/chaoxing/util.go b/drivers/chaoxing/util.go index 2e34994dd90..b6725804e6c 100644 --- a/drivers/chaoxing/util.go +++ b/drivers/chaoxing/util.go @@ -79,7 +79,7 @@ func (d *ChaoXing) GetFiles(parent string) ([]File, error) { return nil, err } if resp.Result != 1 { - msg:=fmt.Sprintf("error code is:%d", resp.Result) + msg := fmt.Sprintf("error code is:%d", resp.Result) return nil, errors.New(msg) } if len(resp.List) > 0 { @@ -97,8 +97,12 @@ func (d *ChaoXing) GetFiles(parent string) ([]File, error) { if err != nil { return nil, err } - if len(resps.List) > 0 { - files = append(files, resps.List...) + for _, file := range resps.List { + // 手机端超星上传的文件没有fileID字段,但ObjectID与fileID相同,可代替 + if file.Content.FileID == "" { + file.Content.FileID = file.Content.ObjectID + } + files = append(files, file) } return files, nil }