mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-12 12:13:51 +08:00
发布v2.18.6版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
@@ -1,17 +1,24 @@
|
||||
// Package common
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"hotgo/internal/library/storager"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/file"
|
||||
"hotgo/utility/format"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
|
||||
type sCommonUpload struct{}
|
||||
@@ -60,3 +67,38 @@ func (s *sCommonUpload) UploadPart(ctx context.Context, in *sysin.UploadPartInp)
|
||||
res.UploadPartModel = data
|
||||
return
|
||||
}
|
||||
|
||||
// ImageTransferStorage 图片链接转存
|
||||
func (s *sCommonUpload) ImageTransferStorage(ctx context.Context, in *sysin.ImageTransferStorageInp) (res *sysin.ImageTransferStorageModel, err error) {
|
||||
if !gstr.HasPrefix(in.Url, "http://") && !gstr.HasPrefix(in.Url, "https://") {
|
||||
return nil, gerror.New("仅支持 HTTP/HTTPS 协议的图片链接")
|
||||
}
|
||||
|
||||
resp, err := g.Client().SetTimeout(time.Second*30).Get(ctx, in.Url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, gerror.Newf("请求图片资源失败, StatusCode:%v", resp.StatusCode)
|
||||
}
|
||||
|
||||
contentType := gstr.ToLower(resp.Header.Get("Content-Type"))
|
||||
if !gstr.HasPrefix(contentType, "image/") {
|
||||
return nil, gerror.New("资源不是图片类型")
|
||||
}
|
||||
|
||||
content := resp.ReadAll()
|
||||
if len(content) == 0 {
|
||||
return nil, gerror.New("图片内容为空")
|
||||
}
|
||||
|
||||
res = new(sysin.ImageTransferStorageModel)
|
||||
fileHeader, err := file.NewMultipartFileHeader("its-"+grand.Letters(8)+".png", content)
|
||||
if err != nil {
|
||||
return nil, gerror.Newf("创建文件头失败:%v", err)
|
||||
}
|
||||
res.AttachmentListModel, err = s.UploadFile(ctx, storager.KindImg, &ghttp.UploadFile{FileHeader: fileHeader})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user