commit code
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 54s

This commit is contained in:
2025-06-01 11:02:33 +08:00
parent ca373ad91f
commit 0d65a890f6
3 changed files with 30 additions and 5 deletions

View File

@ -2,12 +2,15 @@ package logic
import (
"context"
"database/sql"
"godemo/category/category"
"godemo/file/file"
"godemo/gallery/gallery"
"godemo/gallery/internal/model"
"godemo/gallery/internal/svc"
"github.com/google/uuid"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -30,21 +33,38 @@ func NewUploadImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Uploa
// 图片上传
func (l *UploadImageLogic) UploadImage(in *gallery.UploadImageRequest) (*gallery.UploadImageResponse, error) {
// 1. 根据 in.CategoryId 查询分类信息,获取分类名称
getCategoryRpcResp, getCategoryRpcErr := l.svcCtx.CategoryRpc.GetCategory(l.ctx, &category.GetCategoryRequest{
getFullCategoryRpcResp, getFullCategoryRpcErr := l.svcCtx.CategoryRpc.GetFullCategories(l.ctx, &category.GetCategoryRequest{
Id: *in.CategoryId,
})
if getCategoryRpcErr != nil {
return nil, status.Error(codes.Internal, getCategoryRpcErr.Error())
if getFullCategoryRpcErr != nil {
return nil, status.Error(codes.Internal, getFullCategoryRpcErr.Error())
}
// 2. 调用 file rpc Upload 接口上传文件到MinIO
fileUploadRpcResp, fileUploadRpcErr := l.svcCtx.FileRpc.Upload(l.ctx, &file.UploadRequest{
Filename: in.FileName,
Content: in.FileContent,
Folder: getCategoryRpcResp.Category.Name,
Folder: getFullCategoryRpcResp.FullCategoryName,
Bucket: "gallery",
})
if fileUploadRpcErr != nil {
return nil, status.Error(codes.Internal, fileUploadRpcErr.Error())
}
// 3. 图片信息存入数据库
insertResult, insertErr := l.svcCtx.ImagesModel.Insert(l.ctx, &model.Images{
ImageId: uuid.New().String(),
StorageKey: *in.Title,
FileName: fileUploadRpcResp.FileId,
MimeType: "image/jpeg",
FileSize: 10,
CategoryId: sql.NullString{String: *in.CategoryId, Valid: true},
})
if insertErr != nil {
return nil, status.Error(codes.Internal, insertErr.Error())
}
print(insertResult.LastInsertId())
// 4. 返回接口执行结果
return &gallery.UploadImageResponse{}, nil