Skip to content

Commit

Permalink
Fix complexity problem (#21)
Browse files Browse the repository at this point in the history
* fix: fix complexity problem

fix complexity problem

* fix: fix complexity problem

fix complexity problem
generateDownloadUrl

* fix: fix complexity problem

fix complexity problem
generateDownloadUrl

* add: fix-complexity-problem

add downloadObject uploadObject

* add: fix-complexity-problem

fix ci error

---------

Co-authored-by: Zherphy <[email protected]>
  • Loading branch information
Zherphy and Zherphy authored Nov 8, 2024
1 parent 8277cc9 commit da8f4fa
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 77 deletions.
10 changes: 6 additions & 4 deletions batch/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
158 changes: 85 additions & 73 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"math"
"net/http"
"net/url"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"github.com/metalogical/BigFiles/auth"
"github.com/metalogical/BigFiles/batch"
"github.com/sirupsen/logrus"
)

var ObsPutLimit int = 5*int(math.Pow10(9)) - 1 // 5GB - 1
Expand Down Expand Up @@ -157,10 +157,9 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
return
}
}

var resp batch.Response

for _, in := range req.Objects {
for i := 0; i < len(req.Objects); i++ {
in := req.Objects[i]
resp.Objects = append(resp.Objects, batch.Object{
OID: in.OID,
Size: in.Size,
Expand All @@ -177,81 +176,94 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {

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
getObjectOutput, err := s.client.CreateSignedUrl(getObjectInput)
if err != nil {
panic(err)
}
v, err := url.Parse(getObjectOutput.SignedUrl)
if err == nil {
v.Host = s.cdnDomain
v.Scheme = "https"
} else {
logrus.Infof("%s cannot be parsed", getObjectOutput.SignedUrl)
panic(err)
}

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
}

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)
}

out.Actions = &batch.Actions{
Upload: &batch.Action{
HRef: putObjectOutput.SignedUrl,
Header: putObjectInput.Headers,
ExpiresIn: int(s.ttl / time.Second),
},
}
}

// 生成下载对象的带授权信息的URL
func (s *server) generateDownloadUrl(getObjectInput *obs.CreateSignedUrlInput) *url.URL {
// 生成下载对象的带授权信息的URL
getObjectOutput, err := s.client.CreateSignedUrl(getObjectInput)
if err != nil {
panic(err)
}
v, err := url.Parse(getObjectOutput.SignedUrl)
if err == nil {
v.Host = s.cdnDomain
v.Scheme = "https"
} else {
logrus.Infof("%s cannot be parsed", getObjectOutput.SignedUrl)
panic(err)
}
return v
}

func (s *server) healthCheck(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit da8f4fa

Please sign in to comment.