From ecc12a7a71456c6bee9e8e08b78121834b6e57b0 Mon Sep 17 00:00:00 2001 From: kumfo Date: Mon, 22 Jul 2024 12:13:41 +0800 Subject: [PATCH] fix(cdn): check cdn visit is available or not --- cdn-aliyun/aliyun.go | 27 +++++++++++++++++++++++++-- cdn-s3/s3.go | 28 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cdn-aliyun/aliyun.go b/cdn-aliyun/aliyun.go index e89d81ac..661de9d2 100644 --- a/cdn-aliyun/aliyun.go +++ b/cdn-aliyun/aliyun.go @@ -22,6 +22,7 @@ package aliyun import ( "embed" "encoding/json" + "fmt" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/apache/incubator-answer-plugins/cdn-aliyun/i18n" "github.com/apache/incubator-answer-plugins/util" @@ -30,6 +31,7 @@ import ( "github.com/segmentfault/pacman/log" "io" "io/fs" + "net/http" "os" "path/filepath" "strconv" @@ -115,7 +117,7 @@ func (c *CDN) scanFiles() { err := c.scanEmbedFiles("build") if err != nil { enable = false - log.Error("failed: scan embed files") + log.Error("failed: scan embed files:", err) return } log.Info("complete: scan embed files") @@ -126,7 +128,7 @@ func (c *CDN) scanFiles() { err := c.scanStaticPathFiles(staticPath) if err != nil { enable = false - log.Error("fialed: scan static path files") + log.Error("fialed: scan static path files:", err) return } enable = true @@ -344,6 +346,27 @@ func (c *CDN) Upload(filePath string, file io.Reader, size int64) (err error) { return } defer respBody.Close() + return c.checkCDNAvailable(objectKey) +} + +func (c *CDN) checkCDNAvailable(objectKey string) error { + url := c.Config.VisitUrlPrefix + objectKey + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } + + client := &http.Client{} + response, err := client.Do(req) + if err != nil { + return err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + log.Error("check error:", url) + return fmt.Errorf("failed to get object, %s", response.Status) + } return nil } diff --git a/cdn-s3/s3.go b/cdn-s3/s3.go index ccd7c8a7..6835dde5 100644 --- a/cdn-s3/s3.go +++ b/cdn-s3/s3.go @@ -31,6 +31,7 @@ import ( "github.com/segmentfault/pacman/log" "io" "io/fs" + "net/http" "os" "path/filepath" "strconv" @@ -104,7 +105,7 @@ func (c *CDN) scanFiles() { err := c.scanEmbedFiles("build") if err != nil { enable = false - log.Error("failed: scan embed files") + log.Error("failed: scan embed files: ", err) return } log.Info("complete: scan embed files") @@ -115,7 +116,7 @@ func (c *CDN) scanFiles() { err := c.scanStaticPathFiles(staticPath) if err != nil { enable = false - log.Error("fialed: scan static path files") + log.Error("fialed: scan static path files: ", err) return } enable = true @@ -323,7 +324,28 @@ func (c *CDN) Upload(filePath string, file io.ReadSeeker, size int64) (err error log.Error(plugin.MakeTranslator(i18n.ErrUploadFileFailed), err) return } - return + return c.checkCDNAvailable(objectKey) +} + +func (c *CDN) checkCDNAvailable(objectKey string) error { + url := c.Config.VisitUrlPrefix + objectKey + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } + + client := &http.Client{} + response, err := client.Do(req) + if err != nil { + return err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + log.Error("check error:", url) + return fmt.Errorf("failed to get object, %s", response.Status) + } + return nil } func (c *CDN) createObjectKey(filePath string) string {