initial commit
This commit is contained in:
14
file/etc/file.yaml
Normal file
14
file/etc/file.yaml
Normal file
@ -0,0 +1,14 @@
|
||||
Name: file.rpc
|
||||
ListenOn: 0.0.0.0:60200
|
||||
Mode: dev
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: file.rpc
|
||||
|
||||
Minio:
|
||||
Endpoint: "localhost:9000"
|
||||
Id: "minioadmin"
|
||||
Secret: "minioadmin"
|
||||
Token: ""
|
||||
Secure: false
|
||||
39
file/file.go
Normal file
39
file/file.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"godemo/file/file"
|
||||
"godemo/file/internal/config"
|
||||
"godemo/file/internal/server"
|
||||
"godemo/file/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/file.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
file.RegisterFileServer(grpcServer, server.NewFileServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
410
file/file/file.pb.go
Normal file
410
file/file/file.pb.go
Normal file
@ -0,0 +1,410 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.6
|
||||
// protoc v3.20.3
|
||||
// source: rpc/file.proto
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
// 上传文件请求
|
||||
type UploadRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
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"
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *UploadRequest) Reset() {
|
||||
*x = UploadRequest{}
|
||||
mi := &file_rpc_file_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *UploadRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*UploadRequest) ProtoMessage() {}
|
||||
|
||||
func (x *UploadRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use UploadRequest.ProtoReflect.Descriptor instead.
|
||||
func (*UploadRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *UploadRequest) GetFilename() string {
|
||||
if x != nil {
|
||||
return x.Filename
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UploadRequest) GetContentType() string {
|
||||
if x != nil {
|
||||
return x.ContentType
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UploadRequest) GetContent() []byte {
|
||||
if x != nil {
|
||||
return x.Content
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *UploadRequest) GetFolder() string {
|
||||
if x != nil {
|
||||
return x.Folder
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 上传响应
|
||||
type UploadResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
FileId string `protobuf:"bytes,1,opt,name=file_id,json=fileId,proto3" json:"file_id,omitempty"` // 文件唯一 ID(或路径)
|
||||
Url string `protobuf:"bytes,2,opt,name=url,proto3" json:"url,omitempty"` // 可访问 URL(带 CDN 或签名)
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *UploadResponse) Reset() {
|
||||
*x = UploadResponse{}
|
||||
mi := &file_rpc_file_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *UploadResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*UploadResponse) ProtoMessage() {}
|
||||
|
||||
func (x *UploadResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use UploadResponse.ProtoReflect.Descriptor instead.
|
||||
func (*UploadResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *UploadResponse) GetFileId() string {
|
||||
if x != nil {
|
||||
return x.FileId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *UploadResponse) GetUrl() string {
|
||||
if x != nil {
|
||||
return x.Url
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获取访问链接请求
|
||||
type GetFileUrlRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
FileId string `protobuf:"bytes,1,opt,name=file_id,json=fileId,proto3" json:"file_id,omitempty"` // 文件唯一 ID 或路径
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetFileUrlRequest) Reset() {
|
||||
*x = GetFileUrlRequest{}
|
||||
mi := &file_rpc_file_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetFileUrlRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetFileUrlRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetFileUrlRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetFileUrlRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetFileUrlRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *GetFileUrlRequest) GetFileId() string {
|
||||
if x != nil {
|
||||
return x.FileId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 获取访问链接响应
|
||||
type GetFileUrlResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Url string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"` // 可访问链接
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *GetFileUrlResponse) Reset() {
|
||||
*x = GetFileUrlResponse{}
|
||||
mi := &file_rpc_file_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *GetFileUrlResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetFileUrlResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetFileUrlResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetFileUrlResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetFileUrlResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *GetFileUrlResponse) GetUrl() string {
|
||||
if x != nil {
|
||||
return x.Url
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 删除文件请求
|
||||
type DeleteRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
FileId string `protobuf:"bytes,1,opt,name=file_id,json=fileId,proto3" json:"file_id,omitempty"` // 要删除的文件 ID
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) Reset() {
|
||||
*x = DeleteRequest{}
|
||||
mi := &file_rpc_file_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*DeleteRequest) ProtoMessage() {}
|
||||
|
||||
func (x *DeleteRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[4]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use DeleteRequest.ProtoReflect.Descriptor instead.
|
||||
func (*DeleteRequest) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) GetFileId() string {
|
||||
if x != nil {
|
||||
return x.FileId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 删除文件响应
|
||||
type DeleteResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` // 是否删除成功
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *DeleteResponse) Reset() {
|
||||
*x = DeleteResponse{}
|
||||
mi := &file_rpc_file_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *DeleteResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*DeleteResponse) ProtoMessage() {}
|
||||
|
||||
func (x *DeleteResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_rpc_file_proto_msgTypes[5]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use DeleteResponse.ProtoReflect.Descriptor instead.
|
||||
func (*DeleteResponse) Descriptor() ([]byte, []int) {
|
||||
return file_rpc_file_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
func (x *DeleteResponse) GetSuccess() bool {
|
||||
if x != nil {
|
||||
return x.Success
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var File_rpc_file_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_rpc_file_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x0erpc/file.proto\x12\x04file\"\x80\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" +
|
||||
"\x0eUploadResponse\x12\x17\n" +
|
||||
"\afile_id\x18\x01 \x01(\tR\x06fileId\x12\x10\n" +
|
||||
"\x03url\x18\x02 \x01(\tR\x03url\",\n" +
|
||||
"\x11GetFileUrlRequest\x12\x17\n" +
|
||||
"\afile_id\x18\x01 \x01(\tR\x06fileId\"&\n" +
|
||||
"\x12GetFileUrlResponse\x12\x10\n" +
|
||||
"\x03url\x18\x01 \x01(\tR\x03url\"(\n" +
|
||||
"\rDeleteRequest\x12\x17\n" +
|
||||
"\afile_id\x18\x01 \x01(\tR\x06fileId\"*\n" +
|
||||
"\x0eDeleteResponse\x12\x18\n" +
|
||||
"\asuccess\x18\x01 \x01(\bR\asuccess2\xb1\x01\n" +
|
||||
"\x04File\x123\n" +
|
||||
"\x06Upload\x12\x13.file.UploadRequest\x1a\x14.file.UploadResponse\x12?\n" +
|
||||
"\n" +
|
||||
"GetFileUrl\x12\x17.file.GetFileUrlRequest\x1a\x18.file.GetFileUrlResponse\x123\n" +
|
||||
"\x06Delete\x12\x13.file.DeleteRequest\x1a\x14.file.DeleteResponseB\bZ\x06./fileb\x06proto3"
|
||||
|
||||
var (
|
||||
file_rpc_file_proto_rawDescOnce sync.Once
|
||||
file_rpc_file_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_rpc_file_proto_rawDescGZIP() []byte {
|
||||
file_rpc_file_proto_rawDescOnce.Do(func() {
|
||||
file_rpc_file_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_rpc_file_proto_rawDesc), len(file_rpc_file_proto_rawDesc)))
|
||||
})
|
||||
return file_rpc_file_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_rpc_file_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_rpc_file_proto_goTypes = []any{
|
||||
(*UploadRequest)(nil), // 0: file.UploadRequest
|
||||
(*UploadResponse)(nil), // 1: file.UploadResponse
|
||||
(*GetFileUrlRequest)(nil), // 2: file.GetFileUrlRequest
|
||||
(*GetFileUrlResponse)(nil), // 3: file.GetFileUrlResponse
|
||||
(*DeleteRequest)(nil), // 4: file.DeleteRequest
|
||||
(*DeleteResponse)(nil), // 5: file.DeleteResponse
|
||||
}
|
||||
var file_rpc_file_proto_depIdxs = []int32{
|
||||
0, // 0: file.File.Upload:input_type -> file.UploadRequest
|
||||
2, // 1: file.File.GetFileUrl:input_type -> file.GetFileUrlRequest
|
||||
4, // 2: file.File.Delete:input_type -> file.DeleteRequest
|
||||
1, // 3: file.File.Upload:output_type -> file.UploadResponse
|
||||
3, // 4: file.File.GetFileUrl:output_type -> file.GetFileUrlResponse
|
||||
5, // 5: file.File.Delete:output_type -> file.DeleteResponse
|
||||
3, // [3:6] is the sub-list for method output_type
|
||||
0, // [0:3] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_rpc_file_proto_init() }
|
||||
func file_rpc_file_proto_init() {
|
||||
if File_rpc_file_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_rpc_file_proto_rawDesc), len(file_rpc_file_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 6,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_rpc_file_proto_goTypes,
|
||||
DependencyIndexes: file_rpc_file_proto_depIdxs,
|
||||
MessageInfos: file_rpc_file_proto_msgTypes,
|
||||
}.Build()
|
||||
File_rpc_file_proto = out.File
|
||||
file_rpc_file_proto_goTypes = nil
|
||||
file_rpc_file_proto_depIdxs = nil
|
||||
}
|
||||
207
file/file/file_grpc.pb.go
Normal file
207
file/file/file_grpc.pb.go
Normal file
@ -0,0 +1,207 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.5.1
|
||||
// - protoc v3.20.3
|
||||
// source: rpc/file.proto
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
File_Upload_FullMethodName = "/file.File/Upload"
|
||||
File_GetFileUrl_FullMethodName = "/file.File/GetFileUrl"
|
||||
File_Delete_FullMethodName = "/file.File/Delete"
|
||||
)
|
||||
|
||||
// FileClient is the client API for File service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
//
|
||||
// 文件服务 - 上传图片、获取地址、删除文件等
|
||||
type FileClient interface {
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*UploadResponse, error)
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
GetFileUrl(ctx context.Context, in *GetFileUrlRequest, opts ...grpc.CallOption) (*GetFileUrlResponse, error)
|
||||
// 删除文件
|
||||
Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error)
|
||||
}
|
||||
|
||||
type fileClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewFileClient(cc grpc.ClientConnInterface) FileClient {
|
||||
return &fileClient{cc}
|
||||
}
|
||||
|
||||
func (c *fileClient) Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*UploadResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(UploadResponse)
|
||||
err := c.cc.Invoke(ctx, File_Upload_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileClient) GetFileUrl(ctx context.Context, in *GetFileUrlRequest, opts ...grpc.CallOption) (*GetFileUrlResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(GetFileUrlResponse)
|
||||
err := c.cc.Invoke(ctx, File_GetFileUrl_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *fileClient) Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(DeleteResponse)
|
||||
err := c.cc.Invoke(ctx, File_Delete_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FileServer is the server API for File service.
|
||||
// All implementations must embed UnimplementedFileServer
|
||||
// for forward compatibility.
|
||||
//
|
||||
// 文件服务 - 上传图片、获取地址、删除文件等
|
||||
type FileServer interface {
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
Upload(context.Context, *UploadRequest) (*UploadResponse, error)
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
GetFileUrl(context.Context, *GetFileUrlRequest) (*GetFileUrlResponse, error)
|
||||
// 删除文件
|
||||
Delete(context.Context, *DeleteRequest) (*DeleteResponse, error)
|
||||
mustEmbedUnimplementedFileServer()
|
||||
}
|
||||
|
||||
// UnimplementedFileServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedFileServer struct{}
|
||||
|
||||
func (UnimplementedFileServer) Upload(context.Context, *UploadRequest) (*UploadResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Upload not implemented")
|
||||
}
|
||||
func (UnimplementedFileServer) GetFileUrl(context.Context, *GetFileUrlRequest) (*GetFileUrlResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method GetFileUrl not implemented")
|
||||
}
|
||||
func (UnimplementedFileServer) Delete(context.Context, *DeleteRequest) (*DeleteResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Delete not implemented")
|
||||
}
|
||||
func (UnimplementedFileServer) mustEmbedUnimplementedFileServer() {}
|
||||
func (UnimplementedFileServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeFileServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to FileServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeFileServer interface {
|
||||
mustEmbedUnimplementedFileServer()
|
||||
}
|
||||
|
||||
func RegisterFileServer(s grpc.ServiceRegistrar, srv FileServer) {
|
||||
// If the following call pancis, it indicates UnimplementedFileServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&File_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _File_Upload_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(UploadRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServer).Upload(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: File_Upload_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServer).Upload(ctx, req.(*UploadRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _File_GetFileUrl_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetFileUrlRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServer).GetFileUrl(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: File_GetFileUrl_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServer).GetFileUrl(ctx, req.(*GetFileUrlRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _File_Delete_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(DeleteRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FileServer).Delete(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: File_Delete_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FileServer).Delete(ctx, req.(*DeleteRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// File_ServiceDesc is the grpc.ServiceDesc for File service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var File_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "file.File",
|
||||
HandlerType: (*FileServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Upload",
|
||||
Handler: _File_Upload_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetFileUrl",
|
||||
Handler: _File_GetFileUrl_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Delete",
|
||||
Handler: _File_Delete_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "rpc/file.proto",
|
||||
}
|
||||
60
file/fileclient/file.go
Normal file
60
file/fileclient/file.go
Normal file
@ -0,0 +1,60 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.8.3
|
||||
// Source: file.proto
|
||||
|
||||
package fileclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/file/file"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
DeleteRequest = file.DeleteRequest
|
||||
DeleteResponse = file.DeleteResponse
|
||||
GetFileUrlRequest = file.GetFileUrlRequest
|
||||
GetFileUrlResponse = file.GetFileUrlResponse
|
||||
UploadRequest = file.UploadRequest
|
||||
UploadResponse = file.UploadResponse
|
||||
|
||||
File interface {
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*UploadResponse, error)
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
GetFileUrl(ctx context.Context, in *GetFileUrlRequest, opts ...grpc.CallOption) (*GetFileUrlResponse, error)
|
||||
// 删除文件
|
||||
Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error)
|
||||
}
|
||||
|
||||
defaultFile struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewFile(cli zrpc.Client) File {
|
||||
return &defaultFile{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
func (m *defaultFile) Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*UploadResponse, error) {
|
||||
client := file.NewFileClient(m.cli.Conn())
|
||||
return client.Upload(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
func (m *defaultFile) GetFileUrl(ctx context.Context, in *GetFileUrlRequest, opts ...grpc.CallOption) (*GetFileUrlResponse, error) {
|
||||
client := file.NewFileClient(m.cli.Conn())
|
||||
return client.GetFileUrl(ctx, in, opts...)
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
func (m *defaultFile) Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error) {
|
||||
client := file.NewFileClient(m.cli.Conn())
|
||||
return client.Delete(ctx, in, opts...)
|
||||
}
|
||||
16
file/internal/config/config.go
Normal file
16
file/internal/config/config.go
Normal file
@ -0,0 +1,16 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Minio struct {
|
||||
Endpoint string
|
||||
Id string
|
||||
Secret string
|
||||
Token string
|
||||
Secure bool
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Minio Minio
|
||||
}
|
||||
51
file/internal/logic/deletelogic.go
Normal file
51
file/internal/logic/deletelogic.go
Normal file
@ -0,0 +1,51 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"godemo/file/file"
|
||||
"godemo/file/internal/svc"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type DeleteLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
|
||||
return &DeleteLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
func (l *DeleteLogic) Delete(in *file.DeleteRequest) (*file.DeleteResponse, error) {
|
||||
// 假设 fileId 格式为: "bucketName/objectName"
|
||||
parts := strings.SplitN(in.FileId, "/", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid fileId format")
|
||||
}
|
||||
|
||||
bucketName := parts[0]
|
||||
objectName := parts[1]
|
||||
|
||||
// 删除对象
|
||||
err := l.svcCtx.MinioClient.RemoveObject(l.ctx, bucketName, objectName, minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
// return nil, err
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return &file.DeleteResponse{
|
||||
Success: true,
|
||||
}, nil
|
||||
}
|
||||
57
file/internal/logic/getfileurllogic.go
Normal file
57
file/internal/logic/getfileurllogic.go
Normal file
@ -0,0 +1,57 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"godemo/file/file"
|
||||
"godemo/file/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type GetFileUrlLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetFileUrlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileUrlLogic {
|
||||
return &GetFileUrlLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
func (l *GetFileUrlLogic) GetFileUrl(in *file.GetFileUrlRequest) (*file.GetFileUrlResponse, error) {
|
||||
// 假设 fileId 格式为: "bucketName/objectName"
|
||||
parts := strings.SplitN(in.FileId, "/", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, status.Error(codes.InvalidArgument, "invalid fileId format")
|
||||
}
|
||||
|
||||
bucketName := parts[0]
|
||||
objectName := parts[1]
|
||||
|
||||
// 生成预签名 URL,有效期 1 小时
|
||||
presignedUrl, err := l.svcCtx.MinioClient.PresignedGetObject(
|
||||
l.ctx,
|
||||
bucketName,
|
||||
objectName,
|
||||
time.Hour,
|
||||
url.Values{},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return &file.GetFileUrlResponse{
|
||||
Url: presignedUrl.String(),
|
||||
}, nil
|
||||
}
|
||||
88
file/internal/logic/uploadlogic.go
Normal file
88
file/internal/logic/uploadlogic.go
Normal file
@ -0,0 +1,88 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"godemo/file/file"
|
||||
"godemo/file/internal/svc"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type UploadLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
|
||||
return &UploadLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
func (l *UploadLogic) Upload(in *file.UploadRequest) (*file.UploadResponse, error) {
|
||||
bucketName := "default"
|
||||
if in.Folder != "" {
|
||||
bucketName = in.Folder
|
||||
}
|
||||
|
||||
// 确保 bucket 存在(自动创建)
|
||||
exists, err := l.svcCtx.MinioClient.BucketExists(l.ctx, bucketName)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
if !exists {
|
||||
err := l.svcCtx.MinioClient.MakeBucket(l.ctx, bucketName, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
ext := getFileExtension(in.Filename)
|
||||
newFileName := uuid.New().String() + ext
|
||||
|
||||
// 上传对象
|
||||
_, err = l.svcCtx.MinioClient.PutObject(l.ctx, bucketName, newFileName,
|
||||
bytes.NewReader(in.Content),
|
||||
int64(len(in.Content)),
|
||||
minio.PutObjectOptions{
|
||||
ContentType: in.ContentType,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
// 生成预签名 URL(有效期1小时)
|
||||
reqParams := make(url.Values)
|
||||
presignedUrl, err := l.svcCtx.MinioClient.PresignedGetObject(l.ctx, bucketName, newFileName, time.Hour, reqParams)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
return &file.UploadResponse{
|
||||
FileId: newFileName,
|
||||
Url: presignedUrl.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 获取文件扩展名(默认 .jpg)
|
||||
func getFileExtension(name string) string {
|
||||
ext := filepath.Ext(name)
|
||||
if ext == "" {
|
||||
return ".jpg"
|
||||
}
|
||||
return ext
|
||||
}
|
||||
42
file/internal/server/fileserver.go
Normal file
42
file/internal/server/fileserver.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.8.3
|
||||
// Source: file.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/file/file"
|
||||
"godemo/file/internal/logic"
|
||||
"godemo/file/internal/svc"
|
||||
)
|
||||
|
||||
type FileServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
file.UnimplementedFileServer
|
||||
}
|
||||
|
||||
func NewFileServer(svcCtx *svc.ServiceContext) *FileServer {
|
||||
return &FileServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件(图片/头像/壁纸等)
|
||||
func (s *FileServer) Upload(ctx context.Context, in *file.UploadRequest) (*file.UploadResponse, error) {
|
||||
l := logic.NewUploadLogic(ctx, s.svcCtx)
|
||||
return l.Upload(in)
|
||||
}
|
||||
|
||||
// 获取文件访问链接(带签名,防盗链)
|
||||
func (s *FileServer) GetFileUrl(ctx context.Context, in *file.GetFileUrlRequest) (*file.GetFileUrlResponse, error) {
|
||||
l := logic.NewGetFileUrlLogic(ctx, s.svcCtx)
|
||||
return l.GetFileUrl(in)
|
||||
}
|
||||
|
||||
// 删除文件
|
||||
func (s *FileServer) Delete(ctx context.Context, in *file.DeleteRequest) (*file.DeleteResponse, error) {
|
||||
l := logic.NewDeleteLogic(ctx, s.svcCtx)
|
||||
return l.Delete(in)
|
||||
}
|
||||
29
file/internal/svc/servicecontext.go
Normal file
29
file/internal/svc/servicecontext.go
Normal file
@ -0,0 +1,29 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"godemo/file/internal/config"
|
||||
"log"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
MinioClient *minio.Client
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// 初始化 MinIO 客户端
|
||||
minioClient, err := minio.New(c.Minio.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(c.Minio.Id, c.Minio.Secret, c.Minio.Token),
|
||||
Secure: c.Minio.Secure, // 本地部署不用 https
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialize minio client: %v", err)
|
||||
}
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MinioClient: minioClient,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user