From b1934579c0c7f0f69e432ddfb2c4ad9cf2a146db Mon Sep 17 00:00:00 2001 From: konjacpotato Date: Wed, 4 Jun 2025 20:43:15 +0800 Subject: [PATCH] commit code --- gallery/etc/gallery.yaml | 4 +++- gallery/internal/config/config.go | 1 + gallery/internal/logic/uploadimagelogic.go | 20 +++++++++++++++----- sql/images.sql | 4 ++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gallery/etc/gallery.yaml b/gallery/etc/gallery.yaml index 92d5d95..f57f4df 100644 --- a/gallery/etc/gallery.yaml +++ b/gallery/etc/gallery.yaml @@ -23,4 +23,6 @@ CategoryRpc: Etcd: Hosts: - 127.0.0.1:2379 - Key: category.rpc \ No newline at end of file + Key: category.rpc + +BucketName: gallery \ No newline at end of file diff --git a/gallery/internal/config/config.go b/gallery/internal/config/config.go index b190516..96d19ec 100644 --- a/gallery/internal/config/config.go +++ b/gallery/internal/config/config.go @@ -12,4 +12,5 @@ type Config struct { MaxIdleConns int ConnMaxLifetime int } + BucketName string } diff --git a/gallery/internal/logic/uploadimagelogic.go b/gallery/internal/logic/uploadimagelogic.go index f05ef12..5013e43 100644 --- a/gallery/internal/logic/uploadimagelogic.go +++ b/gallery/internal/logic/uploadimagelogic.go @@ -11,6 +11,7 @@ import ( "godemo/gallery/internal/svc" "github.com/google/uuid" + "github.com/zeromicro/go-zero/core/logc" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -45,17 +46,18 @@ func (l *UploadImageLogic) UploadImage(in *gallery.UploadImageRequest) (*gallery Filename: in.FileName, Content: in.FileContent, Folder: getFullCategoryRpcResp.FullCategoryName, - Bucket: "gallery", + Bucket: l.svcCtx.Config.BucketName, }) if fileUploadRpcErr != nil { return nil, status.Error(codes.Internal, fileUploadRpcErr.Error()) } // 3. 图片信息存入数据库 + storageKey := l.svcCtx.Config.BucketName + "/" + fileUploadRpcResp.FileId insertResult, insertErr := l.svcCtx.ImagesModel.Insert(l.ctx, &model.Images{ ImageId: uuid.New().String(), - StorageKey: *in.Title, - FileName: fileUploadRpcResp.FileId, + StorageKey: storageKey, + FileName: *in.Title, MimeType: "image/jpeg", FileSize: 10, CategoryId: sql.NullString{String: *in.CategoryId, Valid: true}, @@ -63,9 +65,17 @@ func (l *UploadImageLogic) UploadImage(in *gallery.UploadImageRequest) (*gallery if insertErr != nil { return nil, status.Error(codes.Internal, insertErr.Error()) } + logc.Infof(l.ctx, "insertResult: %v", insertResult) - print(insertResult.LastInsertId()) + // 4. 调用 file rpc GenerateImageVersions 接口,生成缩略图和预览图 (异步) + _, generateImageVersionsRpcErr := l.svcCtx.FileRpc.GenerateImageVersions(l.ctx, &file.GenerateImageVersionsRequest{ + FileId: storageKey, + Versions: []file.ImageVersion{file.ImageVersion_thumbnail, file.ImageVersion_preview}, + }) + if generateImageVersionsRpcErr != nil { + return nil, status.Error(codes.Internal, generateImageVersionsRpcErr.Error()) + } - // 4. 返回接口执行结果 + // 5. 返回接口执行结果 return &gallery.UploadImageResponse{}, nil } diff --git a/sql/images.sql b/sql/images.sql index 47f834d..6cef130 100644 --- a/sql/images.sql +++ b/sql/images.sql @@ -4,8 +4,8 @@ CREATE TABLE images ( image_id VARCHAR(36) PRIMARY KEY DEFAULT gen_random_uuid()::varchar, -- 存储元数据 - storage_key TEXT NOT NULL, -- 文件显示名称 - file_name TEXT NOT NULL, -- 文件存储名称,可以包含路径 + storage_key TEXT NOT NULL, -- 文件存储标识。 bucket/object_id + file_name TEXT NOT NULL, -- 文件显示名称 mime_type VARCHAR(32) NOT NULL CHECK (mime_type LIKE 'image/%'), file_size BIGINT NOT NULL CHECK (file_size > 0), width INT CHECK (width > 0),