From 888cd7a2034cdee1cc0f91067d6318153c3cb373 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Fri, 18 Aug 2023 16:19:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=AC=E5=85=B1=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/storage/object.go | 19 ++++++---------- api/storage/storage_test.go | 4 ++-- models/models.go | 43 ++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/api/storage/object.go b/api/storage/object.go index 2a7bdd1..c6ceae6 100644 --- a/api/storage/object.go +++ b/api/storage/object.go @@ -7,6 +7,7 @@ import ( "strings" "gitlink.org.cn/cloudream/common/consts/errorcode" + "gitlink.org.cn/cloudream/common/models" myhttp "gitlink.org.cn/cloudream/common/utils/http" "gitlink.org.cn/cloudream/common/utils/serder" ) @@ -48,19 +49,13 @@ func (c *Client) ObjectDownload(req ObjectDownloadReq) (io.ReadCloser, error) { } type ObjectUploadReq struct { - UserID int64 `json:"userID"` - BucketID int64 `json:"bucketID"` - FileSize int64 `json:"fileSize"` - ObjectName string `json:"objectName"` - Redundancy MyRedundancyInfo `json:"redundancy"` - File io.Reader `json:"-"` + UserID int64 `json:"userID"` + BucketID int64 `json:"bucketID"` + FileSize int64 `json:"fileSize"` + ObjectName string `json:"objectName"` + Redundancy models.TypedRedundancyInfo `json:"redundancy"` + File io.Reader `json:"-"` } - -type MyRedundancyInfo struct { - Type string `json:"type"` - Info any `json:"config"` -} - type ObjectUploadResp struct { ObjectID int64 `json:"objectID,string"` } diff --git a/api/storage/storage_test.go b/api/storage/storage_test.go index 2df94c0..fb34c77 100644 --- a/api/storage/storage_test.go +++ b/api/storage/storage_test.go @@ -24,7 +24,7 @@ func Test_Object(t *testing.T) { BucketID: 1, FileSize: 4096, ObjectName: uuid.NewString(), - Redundancy: MyRedundancyInfo{ + Redundancy: models.TypedRedundancyInfo{ Type: models.RedundancyRep, Info: models.NewRepRedundancyInfo(1), }, @@ -65,7 +65,7 @@ func Test_Storage(t *testing.T) { BucketID: 1, FileSize: 4096, ObjectName: uuid.NewString(), - Redundancy: MyRedundancyInfo{ + Redundancy: models.TypedRedundancyInfo{ Type: models.RedundancyRep, Info: models.NewRepRedundancyInfo(1), }, diff --git a/models/models.go b/models/models.go index 92f34d6..1b62849 100644 --- a/models/models.go +++ b/models/models.go @@ -1,5 +1,7 @@ package models +import "gitlink.org.cn/cloudream/common/utils/serder" + /// TODO 将分散在各处的公共结构体定义集中到这里来 const ( @@ -11,7 +13,7 @@ const ( // 注:如果在mq中的消息结构体使用了此类型,记得使用RegisterTypeSet注册相关的类型。 type RedundancyInfo interface{} type RedundancyInfoConst interface { - RepRedundancyInfo | ECRedundancyInfo | RedundancyInfo + RedundancyInfo | RepRedundancyInfo | ECRedundancyInfo } type RepRedundancyInfo struct { RepCount int `json:"repCount"` @@ -24,4 +26,43 @@ func NewRepRedundancyInfo(repCount int) RepRedundancyInfo { } type ECRedundancyInfo struct { + ECName string `json:"ecName"` +} + +func NewECRedundancyInfo(ecName string) ECRedundancyInfo { + return ECRedundancyInfo{ + ECName: ecName, + } +} + +type TypedRedundancyInfo struct { + Type string `json:"type"` + Info RedundancyInfo `json:"info"` +} + +func NewTypedRedundancyInfo[T RedundancyInfoConst](typ string, info T) TypedRedundancyInfo { + return TypedRedundancyInfo{ + Type: typ, + Info: info, + } +} +func NewTypedRepRedundancyInfo(repCount int) TypedRedundancyInfo { + return TypedRedundancyInfo{ + Type: RedundancyRep, + Info: RepRedundancyInfo{ + RepCount: repCount, + }, + } +} + +func (i *TypedRedundancyInfo) ToRepInfo() (RepRedundancyInfo, error) { + var info RepRedundancyInfo + err := serder.AnyToAny(i.Info, &info) + return info, err +} + +func (i *TypedRedundancyInfo) ToECInfo() (ECRedundancyInfo, error) { + var info ECRedundancyInfo + err := serder.AnyToAny(i.Info, &info) + return info, err }