diff --git a/api/oss.go b/api/oss.go new file mode 100644 index 0000000..23d2700 --- /dev/null +++ b/api/oss.go @@ -0,0 +1,156 @@ +package api + +import ( + "bytes" + "crypto/md5" + "encoding/hex" + "fmt" + "github.com/aliyun/aliyun-oss-go-sdk/oss" + ctxLogger "github.com/luizsuper/ctxLoggers" + "go.uber.org/zap" + "io" + "io/ioutil" + "net/http" + "os" + "strconv" + "strings" + "time" +) + +type DetailInfoUrls struct { + ID int `json:"id" gorm:"column:id"` + Image string `json:"image" gorm:"column:image"` + Cover string `json:"cover" gorm:"column:cover"` + Mycover string `json:"mycover" gorm:"column:mycover"` + Myimage string `json:"myimage" gorm:"column:myimage"` + Sid string `json:"sid" gorm:"column:sid"` + Sname string `json:"sname" gorm:"column:sname"` + Done int `json:"done" gorm:"column:done"` + ErrorInfo string `json:"errinfo" gorm:"column:errinfo"` + ErrorImage string `json:"errorimage" gorm:"column:errorimage"` +} + +func (m *DetailInfoUrls) TableName() string { + return "detail_info_urls" +} + +type Error struct { + Sid string `json:"sid" gorm:"column:sid"` + ID int `json:"id" gorm:"column:id"` + Url string `json:"url" gorm:"column:url"` + Type int `json:"type" gorm:"column:type"` + Msg string `json:"msg" gorm:"column:msg"` + CreateTime time.Time `json:"create_time" gorm:"column:create_time"` +} + +func (m *Error) TableName() string { + return "error" +} + +const EndPoint = `oss-cn-hangzhou.aliyuncs.com` + +func s(name, url string) error { + // 创建OSSClient实例。 + client, err := oss.New(EndPoint, "LTAI5tJHzAYoXL7ZNrZwKGoz", "QfL9kWUOMiswz7dk4PbTEy4jukm5YD") + if err != nil { + ctxLogger.Error(nil, "create oss client err", zap.String("err reason", err.Error())) + return err + } + + // 获取存储空间。 + bucket, err := client.Bucket("ganesh-pic") + if err != nil { + ctxLogger.Error(nil, "get bucket err", zap.String("err reason", err.Error())) + return err + } + + picBytes, err := getPic(url) + if err != nil { + ctxLogger.Error(nil, "get bucket err", zap.String("err reason", err.Error())) + return err + } + + err = bucket.PutObject(name, bytes.NewReader(picBytes)) + if err != nil { + ctxLogger.Error(nil, "get bucket err", zap.String("err reason", err.Error())) + return err + } + return nil +} + +func getPic(url string) ([]byte, error) { + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + resp, err := client.Do(req) + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + ctxLogger.Error(nil, "net body close error", zap.String("errReason", err.Error())) + } + }(resp.Body) + return ioutil.ReadAll(resp.Body) +} + +func k(start, end int) { + const ( + coverSuffix = `wujian` + contentSuffix = `content` + coverType = 0 + contentType = 1 + other = 2 + ) + //错误次数,等于30退出程序 + errNum := 0 + //数据源 + d := new([]DetailInfoUrls) + //给图片进行md5加密 + md5Name := func(name string) string { + h := md5.New() + h.Write([]byte(name)) + return hex.EncodeToString(h.Sum(nil)) + } + //insetError + insertErr := func(sid, url, msg string, types int) { + DB.Create(&Error{ + Sid: sid, + Url: url, + Type: types, + Msg: msg, + }) + errNum++ + } + if err := DB.Where("id >= ? and id <= ?", start, end).Find(d).Error; err != nil { + if errNum == 100 { + os.Exit(0) + } + insertErr("999", "", err.Error(), other) + } + for _, v := range *d { + myContent := `` + if errNum == 100 { + os.Exit(0) + } + md5Cover := md5Name(v.Sname + coverSuffix) + if err := s(md5Cover, v.Cover); err != nil { + insertErr(v.Sid, v.Cover, err.Error(), coverType) + } + if spilt := strings.Split(v.Image, `@`); len(spilt) != 0 { + for k, contentUrl := range spilt { + md5Content := md5Name(v.Sname + contentSuffix + strconv.Itoa(k)) + if err := s(md5Content, contentUrl); err == nil { + myContent = fmt.Sprintf("%v%v@", myContent, md5Content) + } else { + insertErr(v.Sid, contentUrl, err.Error(), contentType) + } + } + v.Myimage = myContent + } else { + insertErr(v.Sid, v.Image, "split len ==0", other) + } + DB.Debug().Model(&v).Updates(DetailInfoUrls{Done: 1, Mycover: md5Cover, Myimage: string([]byte(myContent)[0 : len([]byte(myContent))-1])}) + + } +} diff --git a/api/oss_test.go b/api/oss_test.go new file mode 100644 index 0000000..11c0747 --- /dev/null +++ b/api/oss_test.go @@ -0,0 +1,15 @@ +package api + +import ( + "testing" +) + +func TestOss(t *testing.T) { + //s("test.jpg","https://miquan-play.oss-cn-shenzhen.aliyuncs.com/platformScriptImg/29/1.jpg") + ossUrlSend() + +} + +func ossUrlSend() { + k(13906, 13906) +} diff --git a/go.mod b/go.mod index f4dd502..b3818ce 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module infoGetter go 1.17 require ( + github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible github.com/luizsuper/ctxLoggers v1.0.1 github.com/spf13/cast v1.4.1 go.uber.org/zap v1.19.1 @@ -11,6 +12,7 @@ require ( ) require ( + github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.7.7 // indirect github.com/go-playground/locales v0.13.0 // indirect @@ -28,10 +30,12 @@ require ( github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect github.com/pkg/errors v0.8.1 // indirect + github.com/satori/go.uuid v1.2.0 // indirect github.com/ugorji/go/codec v1.1.7 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect + golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index ee0e0bf..8fa389e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible h1:ht2+VfbXtNLGhCsnTMc6/N26nSTBK6qdhktjYyjJQkk= +github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= +github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA= +github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -53,6 +57,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -97,6 +103,8 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 h1:GZokNIeuVkl3aZHJchRrr13WCsols02MLUcz1U9is6M= +golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=