2021-12-03 16:19:25 +08:00

157 lines
4.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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])})
}
}