fix: api descript
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user