From aaab8f5c276be2037a3eba5b236125a833608a18 Mon Sep 17 00:00:00 2001 From: Gucheng Wang Date: Mon, 9 Oct 2023 22:59:16 +0800 Subject: [PATCH] Add gitUploadCdn() --- run/cdn.go | 112 ++++++++++++++++++++++++++++++++++++++++++++ run/git_test.go | 22 +++++++++ storage/casdoor.go | 71 ++++++++++++++++++++++++++++ storage/provider.go | 38 +++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 run/cdn.go create mode 100644 storage/casdoor.go create mode 100644 storage/provider.go diff --git a/run/cdn.go b/run/cdn.go new file mode 100644 index 0000000..ce93f1b --- /dev/null +++ b/run/cdn.go @@ -0,0 +1,112 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package run + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/beego/beego" + "github.com/casbin/caswaf/storage" + "github.com/casbin/caswaf/util" +) + +func filterFiles(filenames []string, folder string) []string { + res := []string{} + for _, filename := range filenames { + if !strings.HasSuffix(filename, folder) { + continue + } + + if strings.Contains(filename, ".chunk.js") { + continue + } + + res = append(res, filename) + } + return res +} + +func uploadFolder(provider storage.StorageProvider, appName string, siteName string, buildDir string, folder string) (string, error) { + domainUrl := "" + + path := filepath.Join(buildDir, "static", folder) + filenames := util.ListFiles(path) + filteredFilenames := filterFiles(filenames, folder) + for _, filename := range filteredFilenames { + data, err := os.ReadFile(filepath.Join(path, filename)) + if err != nil { + return "", err + } + fileBuffer := bytes.NewBuffer(data) + + objectKey := strings.ReplaceAll(filepath.Join("static", folder, filename), "\\", "/") + fileUrl, err := provider.PutObject(appName, siteName, objectKey, fileBuffer) + if err != nil { + return "", err + } + + fmt.Printf("uploadFolder(): Uploaded [%s] to [%s]\n", filepath.Join(path, filename), objectKey) + + index := strings.Index(fileUrl, "/static") + if index == -1 { + return "", fmt.Errorf("uploadFolder() error, fileUrl should contain \"/static/\", fileUrl = %s", fileUrl) + } + + domainUrl = fileUrl[:index+len("/static")] + "/" + } + + return domainUrl, nil +} + +func updateHtml(domainUrl string, buildDir string) { + htmlPath := filepath.Join(buildDir, "index.html") + html := util.ReadStringFromPath(htmlPath) + html = strings.Replace(html, "\"/static/", fmt.Sprintf("\"%s", domainUrl), -1) + util.WriteStringToPath(html, htmlPath) + + fmt.Printf("Updated HTML to: [%s]\n", html) +} + +func gitUploadCdn(providerName string, siteName string) error { + fmt.Printf("gitUploadCdn(): [%s]\n", siteName) + + path := GetRepoPath(siteName) + buildDir := filepath.Join(path, "web/build") + + provider, err := storage.GetStorageProvider(providerName) + if err != nil { + return err + } + + appName := beego.AppConfig.String("dbName") + + var domainUrl string + domainUrl, err = uploadFolder(provider, appName, siteName, buildDir, "js") + if err != nil { + return err + } + + _, err = uploadFolder(provider, appName, siteName, buildDir, "css") + if err != nil { + return err + } + + updateHtml(domainUrl, buildDir) + return nil +} diff --git a/run/git_test.go b/run/git_test.go index 5d60a07..303bafd 100644 --- a/run/git_test.go +++ b/run/git_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/beego/beego" + "github.com/casdoor/casdoor-go-sdk/casdoorsdk" ) func TestGitGetDiff(t *testing.T) { @@ -39,3 +40,24 @@ func TestGitGetDiff(t *testing.T) { println(pid) } + +var JwtPublicKey string + +func TestUploadCdn(t *testing.T) { + err := beego.LoadAppConfig("ini", "../conf/app.conf") + if err != nil { + panic(err) + } + + casdoorEndpoint := beego.AppConfig.String("casdoorEndpoint") + clientId := beego.AppConfig.String("clientId") + clientSecret := beego.AppConfig.String("clientSecret") + casdoorOrganization := beego.AppConfig.String("casdoorOrganization") + casdoorApplication := beego.AppConfig.String("casdoorApplication") + casdoorsdk.InitConfig(casdoorEndpoint, clientId, clientSecret, JwtPublicKey, casdoorOrganization, casdoorApplication) + + err = gitUploadCdn("provider_storage_aliyun_oss", "casdoor") + if err != nil { + panic(err) + } +} diff --git a/storage/casdoor.go b/storage/casdoor.go new file mode 100644 index 0000000..41c488a --- /dev/null +++ b/storage/casdoor.go @@ -0,0 +1,71 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import ( + "bytes" + "fmt" + + "github.com/beego/beego" + "github.com/casdoor/casdoor-go-sdk/casdoorsdk" +) + +type CasdoorProvider struct { + providerName string +} + +func NewCasdoorProvider(providerName string) (*CasdoorProvider, error) { + if providerName == "" { + return nil, fmt.Errorf("storage provider name: [%s] doesn't exist", providerName) + } + + return &CasdoorProvider{providerName: providerName}, nil +} + +func (p *CasdoorProvider) ListObjects(prefix string) ([]*Object, error) { + casdoorOrganization := beego.AppConfig.String("casdoorOrganization") + casdoorApplication := beego.AppConfig.String("casdoorApplication") + resources, err := casdoorsdk.GetResources(casdoorOrganization, casdoorApplication, "provider", p.providerName, "Direct", prefix) + if err != nil { + return nil, err + } + + res := []*Object{} + for _, resource := range resources { + res = append(res, &Object{ + Key: resource.Name, + LastModified: resource.CreatedTime, + Size: int64(resource.FileSize), + Url: resource.Url, + }) + } + return res, nil +} + +func (p *CasdoorProvider) PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) (string, error) { + fileUrl, _, err := casdoorsdk.UploadResource(user, "Casibase", parent, fmt.Sprintf("Direct/%s/%s", p.providerName, key), fileBuffer.Bytes()) + if err != nil { + return "", err + } + return fileUrl, nil +} + +func (p *CasdoorProvider) DeleteObject(key string) error { + _, err := casdoorsdk.DeleteResource(fmt.Sprintf("Direct/%s/%s", p.providerName, key)) + if err != nil { + return err + } + return nil +} diff --git a/storage/provider.go b/storage/provider.go new file mode 100644 index 0000000..603d850 --- /dev/null +++ b/storage/provider.go @@ -0,0 +1,38 @@ +// Copyright 2023 The casbin Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import "bytes" + +type Object struct { + Key string + LastModified string + Size int64 + Url string +} + +type StorageProvider interface { + ListObjects(prefix string) ([]*Object, error) + PutObject(user string, parent string, key string, fileBuffer *bytes.Buffer) (string, error) + DeleteObject(key string) error +} + +func GetStorageProvider(providerName string) (StorageProvider, error) { + p, err := NewCasdoorProvider(providerName) + if err != nil { + return nil, err + } + return p, nil +}