encapsulate object storage service operations

This commit is contained in:
2025-01-17 18:42:36 +08:00
parent e31f95b943
commit eab806fb9b
78 changed files with 4178 additions and 5275 deletions

View File

@@ -414,6 +414,40 @@ type (
)
service auth {
@handler uploadImage
post /upload (UploadRequest)
post /phone/upload (UploadRequest)
}
// 文件上传配置请求参数
type (
StorageConfigRequest {
Type string `json:"type"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Endpoint string `json:"endpoint"`
Bucket string `json:"bucket"`
Region string `json:"region"`
}
)
// 文件上传
@server (
group: storage // 微服务分组
prefix: /api/auth/storage // 微服务前缀
timeout: 10s // 超时时间
maxBytes: 104857600 // 最大请求大小
signature: false // 是否开启签名验证
middleware: SecurityHeadersMiddleware,CasbinVerifyMiddleware,AuthorizationMiddleware,NonceMiddleware // 注册中间件
MaxConns: true // 是否开启最大连接数限制
Recover: true // 是否开启自动恢复
jwt: Auth // 是否开启jwt验证
)
service auth {
// 上传文件
@handler uploadFile
post /uploads returns (string)
// 设置存储配置
@handler setStorageConfig
post /config (StorageConfigRequest) returns (string)
}

View File

@@ -96,6 +96,8 @@ Encrypt:
Key: p3380puliiep184buh8d5dvujeerqtem
# 向量 (16)
IV: spb7er04k2vz3dtk
PublicKey: api/etc/rsa_public_key.pem
PrivateKey: api/etc/rsa_private_key.pem
# Redis 配置
Redis:
# Redis 地址

View File

@@ -0,0 +1,29 @@
package file
import (
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/logic/storage"
"github.com/zeromicro/go-zero/rest/httpx"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/common/xhttp"
)
func SetStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.StorageConfigRequest
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := storage.NewSetStorageConfigLogic(r.Context(), svcCtx)
resp, err := l.SetStorageConfig(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,21 @@
package file
import (
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/logic/storage"
"schisandra-album-cloud-microservices/common/xhttp"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
)
func UploadFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := storage.NewUploadFileLogic(r.Context(), svcCtx)
resp, err := l.UploadFile(r)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -12,6 +12,7 @@ import (
comment "schisandra-album-cloud-microservices/app/auth/api/internal/handler/comment"
oauth "schisandra-album-cloud-microservices/app/auth/api/internal/handler/oauth"
sms "schisandra-album-cloud-microservices/app/auth/api/internal/handler/sms"
storage "schisandra-album-cloud-microservices/app/auth/api/internal/handler/storage"
token "schisandra-album-cloud-microservices/app/auth/api/internal/handler/token"
upscale "schisandra-album-cloud-microservices/app/auth/api/internal/handler/upscale"
user "schisandra-album-cloud-microservices/app/auth/api/internal/handler/user"
@@ -183,6 +184,28 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
rest.WithMaxBytes(1048576),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.SecurityHeadersMiddleware, serverCtx.CasbinVerifyMiddleware, serverCtx.AuthorizationMiddleware, serverCtx.NonceMiddleware},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/config",
Handler: storage.SetStorageConfigHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/uploads",
Handler: storage.UploadFileHandler(serverCtx),
},
}...,
),
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
rest.WithPrefix("/api/auth/storage"),
rest.WithTimeout(10000*time.Millisecond),
rest.WithMaxBytes(104857600),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.SecurityHeadersMiddleware, serverCtx.CasbinVerifyMiddleware, serverCtx.NonceMiddleware},
@@ -206,7 +229,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodPost,
Path: "/upload",
Path: "/phone/upload",
Handler: upscale.UploadImageHandler(serverCtx),
},
}...,

View File

@@ -0,0 +1,29 @@
package storage
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"schisandra-album-cloud-microservices/app/auth/api/internal/logic/storage"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/common/xhttp"
)
func SetStorageConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.StorageConfigRequest
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := storage.NewSetStorageConfigLogic(r.Context(), svcCtx)
resp, err := l.SetStorageConfig(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,21 @@
package storage
import (
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/logic/storage"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/common/xhttp"
)
func UploadFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := storage.NewUploadFileLogic(r.Context(), svcCtx)
resp, err := l.UploadFile(r)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -97,11 +97,11 @@ func (l *WechatOffiaccountCallbackLogic) WechatOffiaccountCallback(r *http.Reque
// SendMessage 发送消息到客户端
func (l *WechatOffiaccountCallbackLogic) SendMessage(openId string, clientId string) error {
encryptClientId, err := encrypt.Encrypt(clientId, l.svcCtx.Config.Encrypt.Key, l.svcCtx.Config.Encrypt.IV)
encryptClientId, err := encrypt.Encrypt(clientId, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return err
}
encryptOpenId, err := encrypt.Encrypt(openId, l.svcCtx.Config.Encrypt.Key, l.svcCtx.Config.Encrypt.IV)
encryptOpenId, err := encrypt.Encrypt(openId, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return err
}

View File

@@ -0,0 +1,55 @@
package storage
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/app/auth/api/internal/types"
"schisandra-album-cloud-microservices/app/auth/model/mysql/model"
"schisandra-album-cloud-microservices/common/encrypt"
)
type SetStorageConfigLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSetStorageConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetStorageConfigLogic {
return &SetStorageConfigLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SetStorageConfigLogic) SetStorageConfig(req *types.StorageConfigRequest) (resp string, err error) {
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return "", errors.New("user_id not found")
}
accessKey, err := encrypt.Encrypt(req.AccessKey, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return "", err
}
secretKey, err := encrypt.Encrypt(req.SecretKey, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return "", err
}
ossConfig := &model.ScaStorageConfig{
UserID: uid,
Type: req.Type,
Endpoint: req.Endpoint,
Bucket: req.Bucket,
AccessKey: accessKey,
SecretKey: secretKey,
Region: req.Region,
}
err = l.svcCtx.DB.ScaStorageConfig.Create(ossConfig)
if err != nil {
return "", err
}
return "success", nil
}

View File

@@ -0,0 +1,75 @@
package storage
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"schisandra-album-cloud-microservices/app/auth/api/internal/svc"
"schisandra-album-cloud-microservices/common/encrypt"
"schisandra-album-cloud-microservices/common/storage/config"
)
type UploadFileLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUploadFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadFileLogic {
return &UploadFileLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UploadFileLogic) UploadFile(r *http.Request) (resp string, err error) {
uid, ok := l.ctx.Value("user_id").(string)
if !ok {
return "", errors.New("user_id not found")
}
file, header, err := r.FormFile("file")
if err != nil {
return "", errors.New("file not found")
}
defer file.Close()
//formValue := r.PostFormValue("result")
//
//var result types.File
//err = json.Unmarshal([]byte(formValue), &result)
//if err != nil {
// return "", errors.New("invalid result")
//}
//fmt.Println(result)
ossConfig := l.svcCtx.DB.ScaStorageConfig
dbConfig, err := ossConfig.Where(ossConfig.UserID.Eq(uid)).First()
if err != nil {
return "", errors.New("oss config not found")
}
accessKey, err := encrypt.Decrypt(dbConfig.AccessKey, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return "", errors.New("decrypt access key failed")
}
secretKey, err := encrypt.Decrypt(dbConfig.SecretKey, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return "", errors.New("decrypt secret key failed")
}
storageConfig := &config.StorageConfig{
Provider: dbConfig.Type,
Endpoint: dbConfig.Endpoint,
AccessKey: accessKey,
SecretKey: secretKey,
BucketName: dbConfig.Bucket,
Region: dbConfig.Region,
}
service, err := l.svcCtx.StorageManager.GetStorage(uid, storageConfig)
if err != nil {
return "", errors.New("get storage failed")
}
result, err := service.UploadFileSimple(l.ctx, dbConfig.Bucket, header.Filename, file, map[string]string{})
if err != nil {
return "", errors.New("upload file failed")
}
return *result.ContentMD5, nil
}

View File

@@ -36,7 +36,7 @@ func NewWechatOffiaccountLoginLogic(ctx context.Context, svcCtx *svc.ServiceCont
}
func (l *WechatOffiaccountLoginLogic) WechatOffiaccountLogin(r *http.Request, req *types.WechatOffiaccountLoginRequest) (resp *types.LoginResponse, err error) {
decryptedClientId, err := encrypt.Decrypt(req.ClientId, l.svcCtx.Config.Encrypt.Key, l.svcCtx.Config.Encrypt.IV)
decryptedClientId, err := encrypt.Decrypt(req.ClientId, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return nil, err
}
@@ -44,7 +44,7 @@ func (l *WechatOffiaccountLoginLogic) WechatOffiaccountLogin(r *http.Request, re
if clientId == "" {
return nil, errors2.New(http.StatusUnauthorized, i18n.FormatText(l.ctx, "login.loginFailed"))
}
Openid, err := encrypt.Decrypt(req.Openid, l.svcCtx.Config.Encrypt.Key, l.svcCtx.Config.Encrypt.IV)
Openid, err := encrypt.Decrypt(req.Openid, l.svcCtx.Config.Encrypt.Key)
if err != nil {
return nil, err
}

View File

@@ -20,6 +20,8 @@ import (
"schisandra-album-cloud-microservices/common/ip2region"
"schisandra-album-cloud-microservices/common/redisx"
"schisandra-album-cloud-microservices/common/sensitivex"
"schisandra-album-cloud-microservices/common/storage"
storage2 "schisandra-album-cloud-microservices/common/storage/manager"
"schisandra-album-cloud-microservices/common/wechat_official"
)
@@ -38,6 +40,7 @@ type ServiceContext struct {
RotateCaptcha rotate.Captcha
SlideCaptcha slide.Captcha
Sensitive *sensitive.Manager
StorageManager *storage2.Manager
}
func NewServiceContext(c config.Config) *ServiceContext {
@@ -59,5 +62,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
SlideCaptcha: initialize.NewSlideCaptcha(),
MongoClient: mongodb.NewMongoDB(c.Mongo.Uri, c.Mongo.Username, c.Mongo.Password, c.Mongo.AuthSource, c.Mongo.Database),
Sensitive: sensitivex.NewSensitive(),
StorageManager: storage.InitStorageManager(),
}
}

View File

@@ -0,0 +1,14 @@
package types
// File represents a file uploaded by the user.
type File struct {
UID string `json:"uid"`
FileName string `json:"fileName"`
FileType string `json:"fileType"`
DetectionResult struct {
IsAnime bool `json:"isAnime"`
HasFace bool `json:"hasFace"`
ObjectArray []string `json:"objectArray"`
Landscape string `json:"landscape"`
}
}

View File

@@ -174,6 +174,15 @@ type SmsSendRequest struct {
Key string `json:"key"`
}
type StorageConfigRequest struct {
Type string `json:"type"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Endpoint string `json:"endpoint"`
Bucket string `json:"bucket"`
Region string `json:"region"`
}
type UploadRequest struct {
Image string `json:"image"`
AccessToken string `json:"access_token"`

View File

@@ -1,6 +1,6 @@
# to build this docker image:
# docker build -f Dockerfile -t schisandra-cloud-album-server .
# docker build --build-arg OPENCV_VERSION="4.x" --build-arg OPENCV_FILE="https://github.com/opencv/opencv/archive/refs/heads/4.x.zip" --build-arg OPENCV_CONTRIB_FILE="https://github.com/opencv/opencv_contrib/archive/refs/heads/4.x.zip" -f Dockerfile -t schisandra-cloud-album-server .
# docker build -f opencv.Dockerfile -t schisandra-cloud-album-server .
# docker build --build-arg OPENCV_VERSION="4.x" --build-arg OPENCV_FILE="https://github.com/opencv/opencv/archive/refs/heads/4.x.zip" --build-arg OPENCV_CONTRIB_FILE="https://github.com/opencv/opencv_contrib/archive/refs/heads/4.x.zip" -f opencv.Dockerfile -t schisandra-cloud-album-server .
FROM ubuntu:20.04 AS opencv-builder