You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by ku...@apache.org on 2024/07/22 04:13:50 UTC

(incubator-answer-plugins) 01/01: fix(cdn): check cdn visit is available or not

This is an automated email from the ASF dual-hosted git repository.

kumfo pushed a commit to branch fix/cdn
in repository https://gitbox.apache.org/repos/asf/incubator-answer-plugins.git

commit 1574beddf53e93280630a24fa13674bf9e39b437
Author: kumfo <ku...@sifou.com>
AuthorDate: Mon Jul 22 12:13:41 2024 +0800

    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 e89d81a..661de9d 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 ccd7c8a..6835dde 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 {