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

edit: 规范代码 #34

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 10 additions & 7 deletions auth/gitee.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
downloadPermissions = []string{"admin", "developer", "read"}
)

var contentType = "Content-Type"
var verifyLog = "verifyUser"

type giteeUser struct {
Permission string `json:"permission"`
}
Expand Down Expand Up @@ -107,7 +110,7 @@ func CheckRepoOwner(userInRepo UserInRepo) error {
} else {
path += fmt.Sprintf("?access_token=%s", defaultToken)
}
headers := http.Header{"Content-Type": []string{"application/json;charset=UTF-8"}}
headers := http.Header{contentType: []string{"application/json;charset=UTF-8"}}
repo := new(Repo)
err := getParsedResponse("GET", path, headers, nil, &repo)
if err != nil {
Expand Down Expand Up @@ -143,7 +146,7 @@ func getToken(username, password string) (string, error) {
form.Add("client_secret", clientSecret)

path := "https://gitee.com/oauth/token"
headers := http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}
headers := http.Header{contentType: []string{"application/x-www-form-urlencoded"}}
accessToken := new(AccessToken)
err := getParsedResponse("POST", path, headers, strings.NewReader(form.Encode()), &accessToken)
if err != nil {
Expand All @@ -167,12 +170,12 @@ func verifyUser(userInRepo UserInRepo) error {
} else {
path += fmt.Sprintf("?access_token=%s", defaultToken)
}
headers := http.Header{"Content-Type": []string{"application/json;charset=UTF-8"}}
headers := http.Header{contentType: []string{"application/json;charset=UTF-8"}}
giteeUser := new(giteeUser)
err := getParsedResponse("GET", path, headers, nil, &giteeUser)
if err != nil {
msg := err.Error() + ": verify user permission failed"
logrus.Error(fmt.Sprintf("verifyUser | %s", msg))
logrus.Error(fmt.Sprintf("%s | %s", verifyLog, msg))
return errors.New(msg)
}

Expand All @@ -187,7 +190,7 @@ func verifyUser(userInRepo UserInRepo) error {
remindMsg := " \n如果您正在向fork仓库上传大文件,请确认您已使用如下命令修改了本地仓库的配置:" +
"\n`git config --local lfs.url https://artifacts.openeuler.openatom.cn/{owner}/{repo}`" +
",\n其中{owner}/{repo}请改为您fork之后的仓库的名称"
logrus.Error(fmt.Sprintf("verifyUser | %s", msg))
logrus.Error(fmt.Sprintf("%s | %s", verifyLog, msg))
return errors.New(msg + remindMsg)
} else if userInRepo.Operation == "download" {
for _, v := range downloadPermissions {
Expand All @@ -196,11 +199,11 @@ func verifyUser(userInRepo UserInRepo) error {
}
}
msg := fmt.Sprintf("forbidden: user %s has no permission to download", userInRepo.Username)
logrus.Error(fmt.Sprintf("verifyUser | %s", msg))
logrus.Error(fmt.Sprintf("%s | %s", verifyLog, msg))
return errors.New(msg)
} else {
msg := "system_error: unknow operation"
logrus.Error(fmt.Sprintf("verifyUser | %s", msg))
logrus.Error(fmt.Sprintf("%s | %s", verifyLog, msg))
return errors.New(msg)
}
}
4 changes: 0 additions & 4 deletions auth/gitee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (s *SuiteGitee) SetupSuite() {
s.Owner = "src-openeuler"
}

// TearDownSuite used for testing
func (s *SuiteGitee) TearDownSuite() {
}

func (s *SuiteGitee) TestGetToken() {
// getToken fail
token, err := getToken("user", "wrong_pwd")
Expand Down
4 changes: 0 additions & 4 deletions auth/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (s *SuiteGithub) SetupSuite() {
s.Password = "password"
}

// TearDownSuite used for testing
func (s *SuiteGithub) TearDownSuite() {
}

func (s *SuiteGithub) TestStatic() {
// Static success
static := Static(s.Username, s.Password)
Expand Down
13 changes: 9 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

var ObsPutLimit int = 5*int(math.Pow10(9)) - 1 // 5GB - 1
var oidRegexp = regexp.MustCompile("^[a-f0-9]{64}$")
var contentType = "Content-Type"

type Options struct {
// required
Expand Down Expand Up @@ -114,7 +115,7 @@ func (s *server) key(oid string) string {
}

func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/vnd.git-lfs+json")
w.Header().Set(contentType, "application/vnd.git-lfs+json")
w.Header().Set("X-Content-Type-Options", "nosniff")

var req batch.Request
Expand Down Expand Up @@ -169,6 +170,8 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) {
s.downloadObject(&in, out)
case "upload":
s.uploadObject(&in, out)
default:
continue
}
}
must(json.NewEncoder(w).Encode(resp))
Expand Down Expand Up @@ -228,13 +231,15 @@ func (s *server) downloadObject(in *batch.RequestObject, out *batch.Object) {
Code: 422,
Message: "found object with wrong size",
}
} else {
logrus.Infof("Metadata check pass, Size check pass")
}
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"}
getObjectInput.Headers = map[string]string{contentType: "application/octet-stream"}
// 生成下载对象的带授权信息的URL
v := s.generateDownloadUrl(getObjectInput)

Expand All @@ -261,7 +266,7 @@ func (s *server) uploadObject(in *batch.RequestObject, out *batch.Object) {
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"}
putObjectInput.Headers = map[string]string{contentType: "application/octet-stream"}
putObjectOutput, err := s.client.CreateSignedUrl(putObjectInput)
if err != nil {
panic(err)
Expand Down Expand Up @@ -300,7 +305,7 @@ func (s *server) healthCheck(w http.ResponseWriter, r *http.Request) {
Data: "healthCheck success",
}

w.Header().Set("Content-Type", "application/json")
w.Header().Set(contentType, "application/json")
w.WriteHeader(http.StatusOK)
must(json.NewEncoder(w).Encode(response))
}
Expand Down
Loading