fix: api descript
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
Name: file-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
|
||||
FileRpcConf:
|
||||
Target: k8s://juwan/objectstory-rpc-svc:8080
|
||||
|
||||
@@ -3,11 +3,15 @@
|
||||
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Logger struct {
|
||||
FileRpcConf zrpc.RpcClientConf
|
||||
Logger struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ package file
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/objectstory/api/internal/logic/file"
|
||||
"juwan-backend/app/objectstory/api/internal/svc"
|
||||
"juwan-backend/app/objectstory/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// 文件获取接口 (如果是私有文件,通过此接口获取或重定向)
|
||||
@@ -22,11 +23,11 @@ func GetFileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
l := file.NewGetFileLogic(r.Context(), svcCtx)
|
||||
err := l.GetFile(&req)
|
||||
url, err := l.GetFile(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.Ok(w)
|
||||
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,11 @@ package file
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/objectstory/api/internal/logic/file"
|
||||
"juwan-backend/app/objectstory/api/internal/svc"
|
||||
"juwan-backend/app/objectstory/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// 文件上传接口
|
||||
@@ -22,7 +23,7 @@ func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
|
||||
l := file.NewUploadLogic(r.Context(), svcCtx)
|
||||
resp, err := l.Upload(&req)
|
||||
resp, err := l.Upload(&req, r)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
|
||||
@@ -5,9 +5,13 @@ package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"juwan-backend/app/objectstory/api/internal/svc"
|
||||
"juwan-backend/app/objectstory/api/internal/types"
|
||||
"juwan-backend/app/objectstory/rpc/fileservice"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +31,26 @@ func NewGetFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileLo
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetFileLogic) GetFile(req *types.GetFileReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *GetFileLogic) GetFile(req *types.GetFileReq) (string, error) {
|
||||
if req == nil || req.FileId == "" {
|
||||
return "", errors.New("file id is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
userID, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return "", contextx.ERRILLEGALUSER
|
||||
}
|
||||
|
||||
rpcResp, err := l.svcCtx.FileRpc.GetFileUrl(l.ctx, &fileservice.GetFileUrlReq{
|
||||
FileId: req.FileId,
|
||||
UserId: strconv.FormatInt(userID, 10),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if rpcResp.GetUrl() == "" {
|
||||
return "", errors.New("file url is empty")
|
||||
}
|
||||
|
||||
return rpcResp.GetUrl(), nil
|
||||
}
|
||||
|
||||
@@ -5,9 +5,15 @@ package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"juwan-backend/app/objectstory/api/internal/svc"
|
||||
"juwan-backend/app/objectstory/api/internal/types"
|
||||
"juwan-backend/app/objectstory/rpc/fileservice"
|
||||
"juwan-backend/common/utils/contextx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +33,43 @@ func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogi
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UploadLogic) Upload(req *types.UploadReq) (resp *types.UploadResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *UploadLogic) Upload(req *types.UploadReq, r *http.Request) (resp *types.UploadResp, err error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("invalid request")
|
||||
}
|
||||
|
||||
return
|
||||
file, fileHeader, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
return nil, errors.New("file is required")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileData, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, errors.New("read file failed")
|
||||
}
|
||||
if len(fileData) == 0 {
|
||||
return nil, errors.New("empty file is not allowed")
|
||||
}
|
||||
|
||||
userID, err := contextx.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, contextx.ERRILLEGALUSER
|
||||
}
|
||||
|
||||
rpcResp, err := l.svcCtx.FileRpc.Upload(l.ctx, &fileservice.UploadFileMetadataReq{
|
||||
FileName: fileHeader.Filename,
|
||||
FileSize: fileHeader.Size,
|
||||
FileType: req.Type,
|
||||
UserId: strconv.FormatInt(userID, 10),
|
||||
FileData: fileData,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rpcResp.GetUrl() == "" {
|
||||
return nil, errors.New("upload failed")
|
||||
}
|
||||
|
||||
return &types.UploadResp{Url: rpcResp.GetUrl()}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
const maxUploadSizeBytes int64 = 20 << 20
|
||||
|
||||
type FileSizeLimitMiddleware struct {
|
||||
}
|
||||
|
||||
@@ -14,9 +16,10 @@ func NewFileSizeLimitMiddleware() *FileSizeLimitMiddleware {
|
||||
|
||||
func (m *FileSizeLimitMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO generate middleware implement function, delete after code implementation
|
||||
if r.Method == http.MethodPost && r.URL != nil && r.URL.Path == "/api/v1/upload" {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSizeBytes)
|
||||
}
|
||||
|
||||
// Passthrough to next handler if need
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,19 +4,24 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"juwan-backend/app/objectstory/api/internal/config"
|
||||
"juwan-backend/app/objectstory/api/internal/middleware"
|
||||
"juwan-backend/app/objectstory/rpc/fileservice"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
FileSizeLimit rest.Middleware
|
||||
FileRpc fileservice.FileService
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
FileSizeLimit: middleware.NewFileSizeLimitMiddleware().Handle,
|
||||
FileRpc: fileservice.NewFileService(zrpc.MustNewClient(c.FileRpcConf)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,40 @@
|
||||
Name: file.rpc
|
||||
ListenOn: 0.0.0.0:8080
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: file.rpc
|
||||
|
||||
Prometheus:
|
||||
Host: 0.0.0.0
|
||||
Port: 4001
|
||||
Path: /metrics
|
||||
|
||||
# Target: k8s://juwan/<service name>.<namespace>:8080
|
||||
#S3Conf:
|
||||
# Endpoint: "${S3_ENDPOINT}"
|
||||
# AccessKey: "${S3_ACCESS_KEY}"
|
||||
# SecretKey: "${S3_SECRET_KEY}"
|
||||
# Bucket: "${S3_BUCKET_NAME}"
|
||||
# Region: "${S3_REGION}"
|
||||
|
||||
S3Conf:
|
||||
Endpoint: "https://cn-nb1.rains3.com"
|
||||
AccessKey: "mfgGnaAcUDP2zYAi"
|
||||
SecretKey: "ZfKkbhUvsAchiKlxzIXrDHrSyskyRj"
|
||||
Bucket: "juwan-dev-image-zj"
|
||||
Region: auto
|
||||
UsePathStyle: true
|
||||
|
||||
DB:
|
||||
Master: "postgresql://${PD_USERNAME}:${DB_PASSWORD}@user-db-rw.juwan:${DB_PORT}/${DB_NAME}?sslmode=disable"
|
||||
Slave: "postgresql://${PD_USERNAME}:${DB_PASSWORD}@user-db-ro.juwan:${DB_PORT}/${DB_NAME}?sslmode=disable"
|
||||
|
||||
CacheConf:
|
||||
- Host: "${REDIS_M_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
- Host: "${REDIS_S_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
|
||||
Log:
|
||||
Level: info
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
import (
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
S3Conf S3ObjectConf
|
||||
}
|
||||
|
||||
type S3ObjectConf struct {
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
Bucket string
|
||||
Endpoint string
|
||||
Region string
|
||||
UsePathStyle bool
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/objectstory/rpc/internal/svc"
|
||||
"juwan-backend/app/objectstory/rpc/pb"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -25,7 +28,26 @@ func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFil
|
||||
|
||||
// 获取文件访问链接(处理私有文件的鉴权)
|
||||
func (l *GetFileUrlLogic) GetFileUrl(in *pb.GetFileUrlReq) (*pb.GetFileUrlResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if in == nil || in.GetFileId() == "" {
|
||||
return nil, errors.New("file id is required")
|
||||
}
|
||||
|
||||
return &pb.GetFileUrlResp{}, nil
|
||||
_, err := l.svcCtx.S3.HeadObject(l.ctx, &s3.HeadObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &in.FileId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
presignClient := s3.NewPresignClient(l.svcCtx.S3)
|
||||
presigned, err := presignClient.PresignGetObject(l.ctx, &s3.GetObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &in.FileId,
|
||||
}, s3.WithPresignExpires(15*time.Minute))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.GetFileUrlResp{Url: presigned.URL}, nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/objectstory/rpc/internal/svc"
|
||||
"juwan-backend/app/objectstory/rpc/pb"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/google/uuid"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -25,7 +33,40 @@ func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogi
|
||||
|
||||
// 简单上传(适合小文件,或保存元数据)
|
||||
func (l *UploadLogic) Upload(in *pb.UploadFileMetadataReq) (*pb.UploadFileResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
if in == nil {
|
||||
return nil, errors.New("invalid request")
|
||||
}
|
||||
if len(in.GetFileData()) == 0 {
|
||||
return nil, errors.New("file data is required")
|
||||
}
|
||||
if in.GetFileName() == "" {
|
||||
return nil, errors.New("file name is required")
|
||||
}
|
||||
if in.GetFileType() == "" {
|
||||
return nil, errors.New("file type is required")
|
||||
}
|
||||
|
||||
return &pb.UploadFileResp{}, nil
|
||||
fileID := uuid.NewString()
|
||||
ext := strings.ToLower(filepath.Ext(in.GetFileName()))
|
||||
if len(ext) > 10 {
|
||||
ext = ""
|
||||
}
|
||||
objectKey := fmt.Sprintf("%s/%s/%s%s", in.GetFileType(), in.GetUserId(), fileID, ext)
|
||||
|
||||
_, err := l.svcCtx.S3.PutObject(l.ctx, &s3.PutObjectInput{
|
||||
Bucket: &l.svcCtx.Config.S3Conf.Bucket,
|
||||
Key: &objectKey,
|
||||
Body: bytes.NewReader(in.GetFileData()),
|
||||
ContentLength: aws.Int64(int64(len(in.GetFileData()))),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := strings.TrimRight(l.svcCtx.Config.S3Conf.Endpoint, "/") + "/" + l.svcCtx.Config.S3Conf.Bucket + "/" + objectKey
|
||||
|
||||
return &pb.UploadFileResp{
|
||||
Url: url,
|
||||
FileId: objectKey,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/objectstory/rpc/internal/config"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awsConfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type ObjectStorageSvc struct {
|
||||
c config.S3ObjectConf
|
||||
}
|
||||
|
||||
func NewObjectStorage(c config.S3ObjectConf) *ObjectStorageSvc {
|
||||
return &ObjectStorageSvc{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ObjectStorageSvc) MustNews3Client(ctx context.Context) *s3.Client {
|
||||
awsCfg, err := awsConfig.LoadDefaultConfig(ctx,
|
||||
awsConfig.WithRegion(s.c.Region),
|
||||
awsConfig.WithCredentialsProvider(
|
||||
credentials.NewStaticCredentialsProvider(
|
||||
s.c.AccessKey,
|
||||
s.c.SecretKey,
|
||||
"",
|
||||
),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return s3.NewFromConfig(awsCfg, func(o *s3.Options) {
|
||||
o.BaseEndpoint = aws.String(s.c.Endpoint)
|
||||
o.UsePathStyle = s.c.UsePathStyle
|
||||
})
|
||||
}
|
||||
@@ -1,13 +1,22 @@
|
||||
package svc
|
||||
|
||||
import "juwan-backend/app/objectstory/rpc/internal/config"
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/objectstory/rpc/internal/config"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
S3 *s3.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
s3obj := NewObjectStorage(c.S3Conf)
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
S3: s3obj.MustNews3Client(context.Background()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc v5.29.6
|
||||
// protoc v3.19.4
|
||||
// source: objectstory.proto
|
||||
|
||||
package pb
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.1
|
||||
// - protoc v5.29.6
|
||||
// - protoc v3.19.4
|
||||
// source: objectstory.proto
|
||||
|
||||
package pb
|
||||
|
||||
Reference in New Issue
Block a user