Skip to content

Commit

Permalink
fix(cdn): check cdn visit is available or not
Browse files Browse the repository at this point in the history
  • Loading branch information
kumfo committed Jul 22, 2024
1 parent 0f695b0 commit ecc12a7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
27 changes: 25 additions & 2 deletions cdn-aliyun/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/segmentfault/pacman/log"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
28 changes: 25 additions & 3 deletions cdn-s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/segmentfault/pacman/log"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ecc12a7

Please sign in to comment.