✨ update generate_avatar.go
This commit is contained in:
3
go.mod
3
go.mod
@@ -84,6 +84,7 @@ require (
|
||||
github.com/google/pprof v0.0.0-20241009165004-a3522334989c // indirect
|
||||
github.com/gorilla/css v1.0.1 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/h2non/filetype v1.1.3 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/imroc/req/v3 v3.48.0 // indirect
|
||||
@@ -118,6 +119,7 @@ require (
|
||||
github.com/quic-go/quic-go v0.48.0 // indirect
|
||||
github.com/refraction-networking/utls v1.6.7 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rock-rabbit/rain v0.4.5 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
@@ -136,6 +138,7 @@ require (
|
||||
golang.org/x/net v0.30.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
golang.org/x/time v0.7.0 // indirect
|
||||
golang.org/x/tools v0.26.0 // indirect
|
||||
google.golang.org/protobuf v1.35.1 // indirect
|
||||
gorm.io/datatypes v1.2.2 // indirect
|
||||
|
6
go.sum
6
go.sum
@@ -174,6 +174,8 @@ github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTj
|
||||
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
|
||||
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
|
||||
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
|
||||
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
|
||||
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
@@ -300,6 +302,8 @@ github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B
|
||||
github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rock-rabbit/rain v0.4.5 h1:SNzv5oTN978STB477XQ/+3tctdqT+Cdf0pN+ru2mhM8=
|
||||
github.com/rock-rabbit/rain v0.4.5/go.mod h1:3weEuxzk4ZxQZF158l2Vb+0Y+s2jGcIdjwJJFUvRB5c=
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
@@ -470,6 +474,8 @@ golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
|
||||
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
|
@@ -5,54 +5,99 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"schisandra-cloud-album/global"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 获取网络图片
|
||||
var client = &http.Client{
|
||||
Timeout: 5 * time.Second, // 超时时间
|
||||
}
|
||||
const (
|
||||
numParts = 4 // 分成4块
|
||||
)
|
||||
|
||||
// GenerateAvatar 用于生成用户头像
|
||||
func GenerateAvatar(userId string) (baseImg string) {
|
||||
|
||||
path := "https://api.multiavatar.com/" + userId + ".png"
|
||||
|
||||
// 创建请求
|
||||
request, err := http.NewRequest("GET", path, nil)
|
||||
// 创建 HTTP 客户端并设置超时时间
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
// 发送 HEAD 请求获取图片大小
|
||||
headReq, err := http.NewRequest("HEAD", path, nil)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
// 发送请求并获取响应
|
||||
respImg, err := client.Do(request)
|
||||
respHead, err := client.Do(headReq)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return ""
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
}(respImg.Body)
|
||||
defer respHead.Body.Close()
|
||||
|
||||
// 读取图片数据
|
||||
imgByte, err := io.ReadAll(respImg.Body)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
// 获取图片大小
|
||||
contentLength := respHead.ContentLength
|
||||
if contentLength <= 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
imgChunks := make([][]byte, numParts) // 存储每个部分的图片数据
|
||||
|
||||
// 启动多个 goroutine 下载分块
|
||||
for i := 0; i < numParts; i++ {
|
||||
wg.Add(1)
|
||||
go func(part int) {
|
||||
defer wg.Done()
|
||||
start := (contentLength / int64(numParts)) * int64(part)
|
||||
end := start + (contentLength / int64(numParts)) - 1
|
||||
if part == numParts-1 {
|
||||
end = contentLength - 1 // 最后一部分下载到文件末尾
|
||||
}
|
||||
|
||||
// 创建 RANGE 请求
|
||||
rangeHeader := "bytes=" + strconv.FormatInt(start, 10) + "-" + strconv.FormatInt(end, 10)
|
||||
request, err := http.NewRequest("GET", path, nil)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
request.Header.Set("Range", rangeHeader)
|
||||
|
||||
respImg, err := client.Do(request)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
defer respImg.Body.Close()
|
||||
|
||||
// 读取图片数据
|
||||
imgByte, err := io.ReadAll(respImg.Body)
|
||||
if err != nil {
|
||||
global.LOG.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 存储分块
|
||||
imgChunks[part] = imgByte
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait() // 等待所有 goroutine 完成
|
||||
|
||||
// 合并所有部分
|
||||
var fullImg []byte
|
||||
for _, chunk := range imgChunks {
|
||||
fullImg = append(fullImg, chunk...)
|
||||
}
|
||||
|
||||
// 判断文件类型,生成一个前缀
|
||||
mimeType := http.DetectContentType(imgByte)
|
||||
mimeType := http.DetectContentType(fullImg)
|
||||
switch mimeType {
|
||||
case "image/png":
|
||||
baseImg = "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgByte)
|
||||
baseImg = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fullImg)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
||||
return baseImg
|
||||
}
|
||||
|
Reference in New Issue
Block a user