Skip to content

Commit

Permalink
fix: 文件内容包含中文字符
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed May 22, 2019
1 parent 410ff31 commit 34a6aae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
33 changes: 20 additions & 13 deletions diamond.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aliacm

import (
"log"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -60,10 +59,10 @@ type Config struct {

// Diamond 提供了操作阿里云ACM的能力
type Diamond struct {
option Option
c *cast.Cast
units []Unit
longPullHook Hook
option Option
c *cast.Cast
units []Unit
errHook Hook
}

// New 产生Diamond实例
Expand Down Expand Up @@ -103,12 +102,7 @@ func (d *Diamond) Add(unit Unit) {
go func() {
for {
newContentMD5, err := d.LongPull(unit, contentMD5)
if err != nil {
if d.longPullHook != nil {
d.longPullHook(unit, err)
}
log.Println("LongPullErr: ", err.Error())
}
d.checkErr(unit, err)
if contentMD5 == "" &&
newContentMD5 != "" && unit.FetchOnce {
return
Expand All @@ -122,6 +116,9 @@ func (d *Diamond) Add(unit Unit) {
for {
select {
case config := <-unit.ch:
var err error
config.Content, err = GbkToUtf8(config.Content)
d.checkErr(unit, err)
unit.OnChange(config)
if unit.FetchOnce {
return
Expand All @@ -131,7 +128,17 @@ func (d *Diamond) Add(unit Unit) {
}()
}

// SetHook 用于提醒当长轮询发生错误的状况
// SetHook 用于提醒关键错误
func (d *Diamond) SetHook(h Hook) {
d.longPullHook = h
d.errHook = h
}

func (d *Diamond) checkErr(unit Unit, err error) {
if err == nil {
return
}
if d.errHook == nil {
return
}
d.errHook(unit, err)
}
19 changes: 19 additions & 0 deletions gbk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package aliacm

import (
"bytes"
"io/ioutil"

"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

// GbkToUtf8 converts gbk encoded bytes to utf-8 encoded bytes.
func GbkToUtf8(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
ret, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return ret, nil
}

0 comments on commit 34a6aae

Please sign in to comment.