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/cdn #179

Merged
merged 6 commits into from
Jul 19, 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
101 changes: 79 additions & 22 deletions cdn-aliyunoss/aliyunoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ import (
"github.com/apache/incubator-answer/ui"
"github.com/segmentfault/pacman/log"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
)

var staticPath = os.Getenv("ANSWER_STATIC_PATH")
var (
staticPath = os.Getenv("ANSWER_STATIC_PATH")
enable = false
)

//go:embed info.yaml
var Info embed.FS
Expand Down Expand Up @@ -99,41 +103,64 @@ func (c *CDN) Info() plugin.Info {

// GetStaticPrefix get static prefix
func (c *CDN) GetStaticPrefix() string {
if !enable {
return ""
}
return c.Config.VisitUrlPrefix + c.Config.ObjectKeyPrefix
}

// scanFiles scan all the static files in the build directory
func (c *CDN) scanFiles() {
if staticPath == "" {
c.scanEmbedFiles("build")
log.Info("complete scan embed files")
err := c.scanEmbedFiles("build")
if err != nil {
enable = false
log.Error("failed: scan embed files")
return
}
log.Info("complete: scan embed files")
enable = true
return
}
c.scanStaticPathFiles(staticPath)
log.Info("complete scan static path files")

err := c.scanStaticPathFiles(staticPath)
if err != nil {
enable = false
log.Error("fialed: scan static path files")
return
}
enable = true
log.Info("complete: scan static path files")
}

// scanStaticPathFiles scan static path files
func (c *CDN) scanStaticPathFiles(fileName string) {
func (c *CDN) scanStaticPathFiles(fileName string) (err error) {
if len(fileName) == 0 {
return
}
// scan static path files
entry, err := os.ReadDir(fileName)
if err != nil {
log.Error("read static dir failed: %v", err)
log.Error("read static dir failed: ", err, fileName)
return
}
for _, info := range entry {
if info.IsDir() {
c.scanStaticPathFiles(filepath.Join(fileName, info.Name()))
err = c.scanStaticPathFiles(filepath.Join(fileName, info.Name()))
if err != nil {
return
}
continue
}

var file *os.File
filePath := filepath.Join(fileName, info.Name())
fi, _ := info.Info()
size := fi.Size()
file, err := os.Open(filePath)
file, err = os.Open(filePath)
if err != nil {
log.Error("open file failed: %v", err)
continue
return
}

suffix := staticPath[:1]
Expand All @@ -145,85 +172,114 @@ func (c *CDN) scanStaticPathFiles(fileName string) {
// rebuild custom io.Reader
ns := strings.Split(info.Name(), ".")
if info.Name() == "asset-manifest.json" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"\"/static": "",
}), size)
if err != nil {
return
}
continue
}
if ns[0] == "main" {
ext := strings.ToLower(filepath.Ext(filePath))
if ext == ".js" || ext == ".map" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"\"static": "",
"=\"/\",": "=\"\",",
}), size)

if err != nil {
return
}
continue
}

if ext == ".css" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"url(/static": "url(../../static",
}), size)

if err != nil {
return
}
continue
}
}

c.Upload(filePath, file, size)
err = c.Upload(filePath, file, size)
if err != nil {
return
}
}
return nil
}

func (c *CDN) scanEmbedFiles(fileName string) {
func (c *CDN) scanEmbedFiles(fileName string) (err error) {
entry, err := ui.Build.ReadDir(fileName)
if err != nil {
log.Error("read static dir failed: %v", err)
return
}
for _, info := range entry {
if info.IsDir() {
c.scanEmbedFiles(filepath.Join(fileName, info.Name()))
err = c.scanEmbedFiles(filepath.Join(fileName, info.Name()))
if err != nil {
return
}
continue
}

var file fs.File
filePath := filepath.Join(fileName, info.Name())
fi, _ := info.Info()
size := fi.Size()
file, err := ui.Build.Open(filePath)
file, err = ui.Build.Open(filePath)
defer file.Close()
if err != nil {
log.Error("open file failed: %v", err)
continue
return
}

filePath = strings.TrimPrefix(filePath, "build/")

// rebuild custom io.Reader
ns := strings.Split(info.Name(), ".")
if info.Name() == "asset-manifest.json" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"\"/static": "",
}), size)
if err != nil {
return
}
continue
}
if ns[0] == "main" {
ext := strings.ToLower(filepath.Ext(filePath))
if ext == ".js" || ext == ".map" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"\"static": "",
"=\"/\",": "=\"\",",
}), size)
if err != nil {
return
}
continue
}

if ext == ".css" {
c.Upload(filePath, c.rebuildReader(file, map[string]string{
err = c.Upload(filePath, c.rebuildReader(file, map[string]string{
"url(/static": "url(../../static",
}), size)
if err != nil {
return
}
continue
}
}

c.Upload(filePath, file, size)
}
return nil
}

func (c *CDN) rebuildReader(file io.Reader, replaceMap map[string]string) io.Reader {
Expand Down Expand Up @@ -254,7 +310,7 @@ func (c *CDN) rebuildReader(file io.Reader, replaceMap map[string]string) io.Rea
return strings.NewReader(res)
}

func (c *CDN) Upload(filePath string, file io.Reader, size int64) {
func (c *CDN) Upload(filePath string, file io.Reader, size int64) (err error) {
client, err := oss.New(c.Config.Endpoint, c.Config.AccessKeyID, c.Config.AccessKeySecret)
if err != nil {
log.Error(plugin.MakeTranslator(i18n.ErrMisStorageConfig), err)
Expand Down Expand Up @@ -287,6 +343,7 @@ func (c *CDN) Upload(filePath string, file io.Reader, size int64) {
return
}
defer respBody.Close()
return nil
}

func (c *CDN) createObjectKey(filePath string) string {
Expand Down
Loading
Loading