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)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user