commit code

This commit is contained in:
2025-05-31 21:44:34 +08:00
parent bfca5d7d0b
commit ca373ad91f
19 changed files with 496 additions and 126 deletions

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.6
// protoc v3.20.3
// protoc v3.19.4
// source: rpc/file.proto
package file
@ -27,7 +27,8 @@ type UploadRequest struct {
Filename string `protobuf:"bytes,1,opt,name=filename,proto3" json:"filename,omitempty"` // 文件名(建议客户端传原始名,服务端会生成唯一名)
ContentType string `protobuf:"bytes,2,opt,name=content_type,json=contentType,proto3" json:"content_type,omitempty"` // 文件类型(如 image/jpeg
Content []byte `protobuf:"bytes,3,opt,name=content,proto3" json:"content,omitempty"` // 文件二进制内容base64 编码由客户端完成)
Folder string `protobuf:"bytes,4,opt,name=folder,proto3" json:"folder,omitempty"` // 可选,文件夹或分类路径,如 "avatars"、"wallpapers"
Folder string `protobuf:"bytes,4,opt,name=folder,proto3" json:"folder,omitempty"` // 可选,文件夹或分类路径,支持多级,如 "avatars"、"wallpapers/animals"
Bucket string `protobuf:"bytes,5,opt,name=bucket,proto3" json:"bucket,omitempty"` // bucket name
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@ -90,6 +91,13 @@ func (x *UploadRequest) GetFolder() string {
return ""
}
func (x *UploadRequest) GetBucket() string {
if x != nil {
return x.Bucket
}
return ""
}
// 上传响应
type UploadResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
@ -327,12 +335,13 @@ var File_rpc_file_proto protoreflect.FileDescriptor
const file_rpc_file_proto_rawDesc = "" +
"\n" +
"\x0erpc/file.proto\x12\x04file\"\x80\x01\n" +
"\x0erpc/file.proto\x12\x04file\"\x98\x01\n" +
"\rUploadRequest\x12\x1a\n" +
"\bfilename\x18\x01 \x01(\tR\bfilename\x12!\n" +
"\fcontent_type\x18\x02 \x01(\tR\vcontentType\x12\x18\n" +
"\acontent\x18\x03 \x01(\fR\acontent\x12\x16\n" +
"\x06folder\x18\x04 \x01(\tR\x06folder\";\n" +
"\x06folder\x18\x04 \x01(\tR\x06folder\x12\x16\n" +
"\x06bucket\x18\x05 \x01(\tR\x06bucket\";\n" +
"\x0eUploadResponse\x12\x17\n" +
"\afile_id\x18\x01 \x01(\tR\x06fileId\x12\x10\n" +
"\x03url\x18\x02 \x01(\tR\x03url\",\n" +

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.5.1
// - protoc v3.20.3
// - protoc v3.19.4
// source: rpc/file.proto
package file

View File

@ -34,8 +34,8 @@ func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogi
// 上传文件(图片/头像/壁纸等)
func (l *UploadLogic) Upload(in *file.UploadRequest) (*file.UploadResponse, error) {
bucketName := "default"
if in.Folder != "" {
bucketName = in.Folder
if in.Bucket != "" {
bucketName = in.Bucket
}
// 确保 bucket 存在(自动创建)
@ -54,6 +54,10 @@ func (l *UploadLogic) Upload(in *file.UploadRequest) (*file.UploadResponse, erro
ext := getFileExtension(in.Filename)
newFileName := uuid.New().String() + ext
if in.Folder != "" {
newFileName = in.Folder + "/" + newFileName
}
// 上传对象
_, err = l.svcCtx.MinioClient.PutObject(l.ctx, bucketName, newFileName,
bytes.NewReader(in.Content),