Skip to content

Commit

Permalink
Add gitUploadCdn()
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeguy committed Oct 9, 2023
1 parent 7f89b04 commit aaab8f5
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
112 changes: 112 additions & 0 deletions run/cdn.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions run/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/beego/beego"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
)

func TestGitGetDiff(t *testing.T) {
Expand All @@ -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)
}
}
71 changes: 71 additions & 0 deletions storage/casdoor.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions storage/provider.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit aaab8f5

Please sign in to comment.