commit code
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 54s
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 54s
This commit is contained in:
@ -224,7 +224,11 @@ grpcurl -plaintext -d '{\"id\": \"da24a78c-ad51-4e82-8aa9-b35342f7ef30\"}' local
|
|||||||
- 获取图片URL
|
- 获取图片URL
|
||||||
- 下载图片
|
- 下载图片
|
||||||
|
|
||||||
/api/gallery/v1/
|
```dev
|
||||||
|
# 上传图片
|
||||||
|
grpcurl -d '{\"file_name\": \"1a.png\", \"category_id\": \"da24a78c-ad51-4e82-8aa9-b35342f7ef30\", \"title\": \"背景\", \"file_content\": \"iVBORw0KGgoAAAANSUhEUgAAAC8AAAAhCAYAAABJLfLcAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAVhSURBVFhH7ZZpbJRFGMd/8777Lu1u6UFpC9tDCqVFDkGOgFDlLHJIQUDQGCFgpOIHvnBpqZRbBEm1FFAwFUzEKxJjKoGABDQoJIggV6jUSmi70O1BW3bb7R7jh22lLPuuQRMbTH/JZLPzzPvMM/95npkRo0aNkjykKP4dDxMdwbcXHcG3F//34AXdJqxg+wfvs+utefQP9bffRbE8zcp3d7CjYD1zU42+TqGgquq/aorwn8mHwb/jfhSEMYSwsM5oTg0dPy1omCIiidQa0QBQSZy2mrWze6B4/9l1IhQPv3++mvXfluP1s+kqryVPJ+e9AvLz81g1oycaICJG8OqWfPK357M5awRRAoQQCCEAgdJGIkVVfQuVEq+USP+GQDFoaJqGZlARBBgjJdLrH/JdhN4NqyXPYcPa6SSo/hYf9Wf38ol1LPMnxKG0ehAGjJ0MKEg8zU7qL+9n/bZjVAaYQXtkBjm5s+llBMeFQnK2HqNSN06JDOBDN3glIoG0+AgUBJ0ff46sKSlo1afYu+coVq+CdDsJG7mYJRO66aZS05V9ZG88isOSgiWs7SiF6CfmkZWRhEFW89NHuzlS5mpjv4u3oYK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3ohA9aDITHzX7G+5BR3kQqgHF68YT0NqGkBh6JkUg3G5cTgf1NTXUNbrb5K+BrmmDSY02IB03OH/uBo57HATCRMKggSSZBO7qYs5erSLQOaYb/MOA7mnzMNARfHvREXx70RF8e/EnBBEDlIbPjKIAAAAASUVORK5CYII=\"}' -plaintext localhost:50000 gallery.Gallery/UploadImage
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
Name: gallery.rpc
|
Name: gallery.rpc
|
||||||
ListenOn: 0.0.0.0:50000
|
ListenOn: 0.0.0.0:50000
|
||||||
|
Mode: dev
|
||||||
|
|
||||||
DB:
|
DB:
|
||||||
DataSource: postgres://postgres:postgres@localhost:19732/godemo?sslmode=disable
|
DataSource: postgres://postgres:postgres@localhost:19732/godemo?sslmode=disable
|
||||||
|
|||||||
@ -2,12 +2,15 @@ package logic
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
"godemo/category/category"
|
"godemo/category/category"
|
||||||
"godemo/file/file"
|
"godemo/file/file"
|
||||||
"godemo/gallery/gallery"
|
"godemo/gallery/gallery"
|
||||||
|
"godemo/gallery/internal/model"
|
||||||
"godemo/gallery/internal/svc"
|
"godemo/gallery/internal/svc"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"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) {
|
func (l *UploadImageLogic) UploadImage(in *gallery.UploadImageRequest) (*gallery.UploadImageResponse, error) {
|
||||||
// 1. 根据 in.CategoryId 查询分类信息,获取分类名称
|
// 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,
|
Id: *in.CategoryId,
|
||||||
})
|
})
|
||||||
if getCategoryRpcErr != nil {
|
if getFullCategoryRpcErr != nil {
|
||||||
return nil, status.Error(codes.Internal, getCategoryRpcErr.Error())
|
return nil, status.Error(codes.Internal, getFullCategoryRpcErr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 调用 file rpc Upload 接口,上传文件到MinIO
|
// 2. 调用 file rpc Upload 接口,上传文件到MinIO
|
||||||
fileUploadRpcResp, fileUploadRpcErr := l.svcCtx.FileRpc.Upload(l.ctx, &file.UploadRequest{
|
fileUploadRpcResp, fileUploadRpcErr := l.svcCtx.FileRpc.Upload(l.ctx, &file.UploadRequest{
|
||||||
Filename: in.FileName,
|
Filename: in.FileName,
|
||||||
Content: in.FileContent,
|
Content: in.FileContent,
|
||||||
Folder: getCategoryRpcResp.Category.Name,
|
Folder: getFullCategoryRpcResp.FullCategoryName,
|
||||||
|
Bucket: "gallery",
|
||||||
})
|
})
|
||||||
|
if fileUploadRpcErr != nil {
|
||||||
|
return nil, status.Error(codes.Internal, fileUploadRpcErr.Error())
|
||||||
|
}
|
||||||
|
|
||||||
// 3. 图片信息存入数据库
|
// 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. 返回接口执行结果
|
// 4. 返回接口执行结果
|
||||||
return &gallery.UploadImageResponse{}, nil
|
return &gallery.UploadImageResponse{}, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user