Skip to content

Commit

Permalink
fix(cdn): aws s3 cdn add sync status check
Browse files Browse the repository at this point in the history
  • Loading branch information
kumfo committed Jul 19, 2024
1 parent 3dba163 commit 2a39829
Showing 1 changed file with 81 additions and 24 deletions.
105 changes: 81 additions & 24 deletions cdn-s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/apache/incubator-answer/ui"
"github.com/segmentfault/pacman/log"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
Expand All @@ -39,7 +40,10 @@ import (
"github.com/apache/incubator-answer/plugin"
)

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 @@ -88,22 +92,42 @@ 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
}

err := c.scanStaticPathFiles(staticPath)
if err != nil {
enable = false
log.Error("fialed: scan static path files")
return
}
c.scanStaticPathFiles(staticPath)
log.Info("complete scan static path files")
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 {
Expand All @@ -112,17 +136,20 @@ func (c *CDN) scanStaticPathFiles(fileName string) {
}
for _, info := range entry {
if info.IsDir() {
c.scanStaticPathFiles(filepath.Join(fileName, info.Name()))
continue
err = c.scanStaticPathFiles(filepath.Join(fileName, info.Name()))
if err != nil {
return
}
}

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 @@ -134,86 +161,116 @@ 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
}

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, c.rebuildReader(file, nil), size)
err = c.Upload(filePath, c.rebuildReader(file, nil), size)
if err != nil {
return
}
}
return
}

func (c *CDN) rebuildReader(file io.Reader, replaceMap map[string]string) io.ReadSeeker {
Expand Down Expand Up @@ -246,7 +303,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.ReadSeeker, size int64) {
func (c *CDN) Upload(filePath string, file io.ReadSeeker, size int64) (err error) {

if !c.CheckFileType(filePath) {
log.Error(plugin.MakeTranslator(i18n.ErrUnsupportedFileType), filePath)
Expand All @@ -260,7 +317,7 @@ func (c *CDN) Upload(filePath string, file io.ReadSeeker, size int64) {

objectKey := c.createObjectKey(filePath)

err := c.Client.PutObject(objectKey, strings.ToLower(filepath.Ext(filePath)), file)
err = c.Client.PutObject(objectKey, strings.ToLower(filepath.Ext(filePath)), file)
if err != nil {
log.Error(plugin.MakeTranslator(i18n.ErrUploadFileFailed), err)
return
Expand Down

0 comments on commit 2a39829

Please sign in to comment.