From caeb4bae8722fb81dbce7b69d8cd329d817172a9 Mon Sep 17 00:00:00 2001 From: Zherphy Date: Fri, 8 Nov 2024 16:08:03 +0800 Subject: [PATCH] add: fix-complexity-problem add downloadObject uploadObject --- batch/types.go | 10 ++-- server/server.go | 128 ++++++++++++++++++++++++----------------------- 2 files changed, 71 insertions(+), 67 deletions(-) diff --git a/batch/types.go b/batch/types.go index 9d97f6e..fc58c13 100644 --- a/batch/types.go +++ b/batch/types.go @@ -8,10 +8,12 @@ type Request struct { Ref struct { Name string `json:"name"` } `json:"ref"` - Objects []struct { - OID string `json:"oid"` - Size int `json:"size"` - } `json:"objects"` + Objects []RequestObject `json:"objects"` +} + +type RequestObject struct { + OID string `json:"oid"` + Size int `json:"size"` } type SuccessResponse struct { diff --git a/server/server.go b/server/server.go index b2aef97..26bbb67 100644 --- a/server/server.go +++ b/server/server.go @@ -157,11 +157,6 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) { return } } - resp := s.operateRequestObject(req) - must(json.NewEncoder(w).Encode(resp)) -} - -func (s *server) operateRequestObject(req batch.Request) batch.Response { var resp batch.Response for _, in := range req.Objects { resp.Objects = append(resp.Objects, batch.Object{ @@ -180,69 +175,76 @@ func (s *server) operateRequestObject(req batch.Request) batch.Response { switch req.Operation { case "download": - getObjectMetadataInput := &obs.GetObjectMetadataInput{ - Bucket: s.bucket, - Key: s.key(in.OID), - } - if metadata, err := s.client.GetObjectMetadata(getObjectMetadataInput); err != nil { - out.Error = &batch.ObjectError{ - Code: 404, - Message: err.Error(), - } - continue - } else if in.Size != int(metadata.ContentLength) { - out.Error = &batch.ObjectError{ - Code: 422, - Message: "found object with wrong size", - } - } - getObjectInput := &obs.CreateSignedUrlInput{} - getObjectInput.Method = obs.HttpMethodGet - getObjectInput.Bucket = s.bucket - getObjectInput.Key = s.key(in.OID) - getObjectInput.Expires = int(s.ttl / time.Second) - getObjectInput.Headers = map[string]string{"Content-Type": "application/octet-stream"} - // 生成下载对象的带授权信息的URL - v := s.generateDownloadUrl(getObjectInput) - - out.Actions = &batch.Actions{ - Download: &batch.Action{ - HRef: v.String(), - Header: getObjectInput.Headers, - ExpiresIn: int(s.ttl / time.Second), - }, - } - + s.downloadObject(&in, out) case "upload": - if out.Size > ObsPutLimit { - out.Error = &batch.ObjectError{ - Code: 422, - Message: "cannot upload objects larger than 5GB to S3 via LFS basic transfer adapter", - } - continue - } + s.uploadObject(&in, out) + } + } + must(json.NewEncoder(w).Encode(resp)) +} - putObjectInput := &obs.CreateSignedUrlInput{} - putObjectInput.Method = obs.HttpMethodPut - putObjectInput.Bucket = s.bucket - putObjectInput.Key = s.key(in.OID) - putObjectInput.Expires = int(s.ttl / time.Second) - putObjectInput.Headers = map[string]string{"Content-Type": "application/octet-stream"} - putObjectOutput, err := s.client.CreateSignedUrl(putObjectInput) - if err != nil { - panic(err) - } +func (s *server) downloadObject(in *batch.RequestObject, out *batch.Object) { + getObjectMetadataInput := &obs.GetObjectMetadataInput{ + Bucket: s.bucket, + Key: s.key(in.OID), + } + if metadata, err := s.client.GetObjectMetadata(getObjectMetadataInput); err != nil { + out.Error = &batch.ObjectError{ + Code: 404, + Message: err.Error(), + } + return + } else if in.Size != int(metadata.ContentLength) { + out.Error = &batch.ObjectError{ + Code: 422, + Message: "found object with wrong size", + } + } + getObjectInput := &obs.CreateSignedUrlInput{} + getObjectInput.Method = obs.HttpMethodGet + getObjectInput.Bucket = s.bucket + getObjectInput.Key = s.key(in.OID) + getObjectInput.Expires = int(s.ttl / time.Second) + getObjectInput.Headers = map[string]string{"Content-Type": "application/octet-stream"} + // 生成下载对象的带授权信息的URL + v := s.generateDownloadUrl(getObjectInput) + + out.Actions = &batch.Actions{ + Download: &batch.Action{ + HRef: v.String(), + Header: getObjectInput.Headers, + ExpiresIn: int(s.ttl / time.Second), + }, + } +} - out.Actions = &batch.Actions{ - Upload: &batch.Action{ - HRef: putObjectOutput.SignedUrl, - Header: putObjectInput.Headers, - ExpiresIn: int(s.ttl / time.Second), - }, - } +func (s *server) uploadObject(in *batch.RequestObject, out *batch.Object) { + if out.Size > ObsPutLimit { + out.Error = &batch.ObjectError{ + Code: 422, + Message: "cannot upload objects larger than 5GB to S3 via LFS basic transfer adapter", } + return + } + + putObjectInput := &obs.CreateSignedUrlInput{} + putObjectInput.Method = obs.HttpMethodPut + putObjectInput.Bucket = s.bucket + putObjectInput.Key = s.key(in.OID) + putObjectInput.Expires = int(s.ttl / time.Second) + putObjectInput.Headers = map[string]string{"Content-Type": "application/octet-stream"} + putObjectOutput, err := s.client.CreateSignedUrl(putObjectInput) + if err != nil { + panic(err) + } + + out.Actions = &batch.Actions{ + Upload: &batch.Action{ + HRef: putObjectOutput.SignedUrl, + Header: putObjectInput.Headers, + ExpiresIn: int(s.ttl / time.Second), + }, } - return resp } // 生成下载对象的带授权信息的URL