Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix complexity problem #21

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 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,9 +157,12 @@ 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

别把batch的整体逻辑全拆出来了,可以把上传和下载两部分拆出两个方法。认证部分也可以拆出来

var resp batch.Response

for _, in := range req.Objects {
resp.Objects = append(resp.Objects, batch.Object{
OID: in.OID,
Expand Down Expand Up @@ -200,18 +203,7 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
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)
}
v := s.generateDownloadUrl(getObjectInput)

out.Actions = &batch.Actions{
Download: &batch.Action{
Expand Down Expand Up @@ -250,8 +242,25 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
}
}
}
return resp
}

must(json.NewEncoder(w).Encode(resp))
// 生成下载对象的带授权信息的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