This commit is contained in:
孟帅
2023-04-05 12:29:33 +08:00
parent c709073ed7
commit 075a12ce7e
27 changed files with 703 additions and 168 deletions

View File

@@ -5,8 +5,8 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gtime"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/net"
"hotgo/internal/library/location"
"hotgo/internal/model"
"hotgo/internal/service"

View File

@@ -8,6 +8,7 @@ package common
import (
"context"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
@@ -15,6 +16,9 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/grand"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
"github.com/tencentyun/cos-go-sdk-v5"
ufile "github.com/ufilesdk-dev/ufile-gosdk"
"hotgo/internal/consts"
"hotgo/internal/dao"
@@ -24,8 +28,10 @@ import (
"hotgo/utility/encrypt"
f "hotgo/utility/file"
"hotgo/utility/format"
"hotgo/utility/url"
utilityurl "hotgo/utility/url"
"hotgo/utility/validate"
"net/http"
"net/url"
"strconv"
"strings"
"time"
@@ -68,6 +74,12 @@ func (s *sCommonUpload) UploadFile(ctx context.Context, file *ghttp.UploadFile)
return s.UploadLocal(ctx, conf, file, meta)
case consts.UploadDriveUCloud:
return s.UploadUCloud(ctx, conf, file, meta)
case consts.UploadDriveCos:
return s.UploadCOS(ctx, conf, file, meta)
case consts.UploadDriveOss:
return s.UploadOSS(ctx, conf, file, meta)
case consts.UploadDriveQiNiu:
return s.UploadQiNiu(ctx, conf, file, meta)
default:
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
}
@@ -103,6 +115,12 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
return s.UploadLocal(ctx, conf, file, meta)
case consts.UploadDriveUCloud:
return s.UploadUCloud(ctx, conf, file, meta)
case consts.UploadDriveCos:
return s.UploadCOS(ctx, conf, file, meta)
case consts.UploadDriveOss:
return s.UploadOSS(ctx, conf, file, meta)
case consts.UploadDriveQiNiu:
return s.UploadQiNiu(ctx, conf, file, meta)
default:
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
}
@@ -110,13 +128,7 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
// UploadLocal 上传本地
func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
}
if result != nil {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
@@ -159,13 +171,7 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfi
// UploadUCloud 上传UCloud对象存储
func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
}
if result != nil {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
@@ -218,6 +224,171 @@ func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConf
return
}
// UploadCOS 上传腾讯云对象存储
func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.CosPath == "" {
err = gerror.New("COS存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.CosPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
u, _ := url.Parse(conf.CosBucketURL)
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: conf.CosSecretId,
SecretKey: conf.CosSecretKey,
},
})
_, err = c.Object.Put(ctx, fullPath, f2, nil)
if err != nil {
return nil, err
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveCos)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// UploadOSS 上传阿里云云对象存储
func (s *sCommonUpload) UploadOSS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.OssPath == "" {
err = gerror.New("OSS存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.OssPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
client, err := oss.New(conf.OssEndpoint, conf.OssSecretId, conf.OssSecretKey)
if err != nil {
return nil, err
}
bucket, err := client.Bucket(conf.OssBucket)
if err != nil {
return nil, err
}
if err = bucket.PutObject(fullPath, f2); err != nil {
return nil, err
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveOss)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// UploadQiNiu 上传七牛云对象存储
func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.QiNiuPath == "" {
err = gerror.New("七牛云存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.QiNiuPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
putPolicy := storage.PutPolicy{
Scope: conf.QiNiuBucket,
}
token := putPolicy.UploadToken(qbox.NewMac(conf.QiNiuAccessKey, conf.QiNiuSecretKey))
cfg := storage.Config{}
// 是否使用https域名
cfg.UseHTTPS = true
// 上传是否使用CDN上传加速
cfg.UseCdnDomains = false
// 空间对应的机房
cfg.Region, err = storage.GetRegion(conf.QiNiuAccessKey, conf.QiNiuBucket)
if err != nil {
return
}
if err = storage.NewFormUploader(&cfg).Put(ctx, &storage.PutRet{}, token, fullPath, f2, file.Size, &storage.PutExtra{}); err != nil {
return
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveQiNiu)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// LastUrl 根据驱动获取最终文件访问地址
func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, fullPath, drive string) string {
if validate.IsURL(fullPath) {
@@ -226,14 +397,30 @@ func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, f
switch drive {
case consts.UploadDriveLocal:
return url.GetAddr(ctx) + "/" + fullPath
return utilityurl.GetAddr(ctx) + "/" + fullPath
case consts.UploadDriveUCloud:
return conf.UCloudEndpoint + "/" + fullPath
case consts.UploadDriveCos:
return conf.CosBucketURL + "/" + fullPath
case consts.UploadDriveOss:
return conf.OssBucketURL + "/" + fullPath
case consts.UploadDriveQiNiu:
return conf.QiNiuDomain + "/" + fullPath
default:
return fullPath
}
}
// HasFile 文件是否存在
func (s *sCommonUpload) HasFile(ctx context.Context, md5 string) (bool, error) {
result, err := dao.SysAttachment.GetMd5File(ctx, md5)
if err != nil {
return false, err
}
return result != nil, nil
}
// fileMeta 上传文件元数据
func (s *sCommonUpload) fileMeta(file *ghttp.UploadFile) (meta *sysin.UploadFileMeta, err error) {
meta = new(sysin.UploadFileMeta)

View File

@@ -18,10 +18,7 @@ import (
// ApiAuth API鉴权中间件
func (s *sMiddleware) ApiAuth(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
// 替换掉模块前缀
routerPrefix := g.Cfg().MustGet(ctx, "router.api.prefix", "/api")

View File

@@ -97,9 +97,7 @@ func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
// Addon 插件中间件
func (s *sMiddleware) Addon(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
if contexts.Get(ctx).Module == "" {
g.Log().Warning(ctx, "application module is not initialized.")

View File

@@ -16,7 +16,7 @@ func (s *sMiddleware) Blacklist(r *ghttp.Request) {
response.JsonExit(r, gcode.CodeServerBusy.Code(), "请求异常,已被封禁,如有疑问请联系管理员!")
}
} else {
g.Log().Warningf(r.Context(), "blacklists uninitialized")
g.Log().Infof(r.Context(), "blacklists uninitialized")
}
r.Middleware.Next()

View File

@@ -18,10 +18,7 @@ import (
// WebSocketToken 检查ws连接token
func (s *sMiddleware) WebSocketToken(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
// 替换掉模块前缀
routerPrefix := g.Cfg().MustGet(ctx, "router.ws.prefix", "/socket")

View File

@@ -255,7 +255,7 @@ func (s *sSysLog) Delete(ctx context.Context, in sysin.LogDeleteInp) (err error)
// List 列表
func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.LogListModel, totalCount int, err error) {
mod := dao.SysLog.Ctx(ctx)
mod := dao.SysLog.Ctx(ctx).FieldsEx("get_data", "header_data", "post_data")
// 访问路径
if in.Url != "" {

View File

@@ -69,6 +69,9 @@ func (s *sTCPAuth) Stop(ctx context.Context) {
// IsLogin 是否已登录认证
func (s *sTCPAuth) IsLogin() bool {
if s.client == nil {
return false
}
return s.client.IsLogin
}