6 changed files with 611 additions and 27 deletions
@ -0,0 +1,40 @@ |
|||
syntax = "proto3"; |
|||
|
|||
package fileupload; |
|||
|
|||
option go_package = "./fileupload"; |
|||
|
|||
// 文件上传服务定义 |
|||
service FileUploadService { |
|||
// 上传文件的 RPC 方法,使用流式传输 |
|||
rpc UploadFile (stream FileChunk) returns (UploadResponse); |
|||
} |
|||
|
|||
// 文件数据块 |
|||
message FileChunk { |
|||
// 文件元信息,只在第一个数据块中包含 |
|||
FileInfo info = 1; |
|||
// 文件数据 |
|||
bytes content = 2; |
|||
// 当前块的序号 |
|||
int32 chunk_index = 3; |
|||
// 总块数 |
|||
int32 total_chunks = 4; |
|||
} |
|||
|
|||
// 文件元信息 |
|||
message FileInfo { |
|||
string file_name = 1; // 文件名 |
|||
int64 file_size = 2; // 文件大小(字节) |
|||
string content_type = 3; // 文件类型 |
|||
string upload_path = 4; // 上传路径 |
|||
} |
|||
|
|||
// 上传响应 |
|||
message UploadResponse { |
|||
bool success = 1; // 是否成功 |
|||
string message = 2; // 响应消息 |
|||
string file_path = 3; // 服务器保存路径 |
|||
int64 received_size = 4; // 接收的文件大小 |
|||
} |
|||
|
|||
@ -0,0 +1,313 @@ |
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
|||
// versions:
|
|||
// protoc-gen-go v1.36.6
|
|||
// protoc v6.30.2
|
|||
// source: fileupload.proto
|
|||
|
|||
package fileupload |
|||
|
|||
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 FileChunk struct { |
|||
state protoimpl.MessageState `protogen:"open.v1"` |
|||
// 文件元信息,只在第一个数据块中包含
|
|||
Info *FileInfo `protobuf:"bytes,1,opt,name=info,proto3" json:"info,omitempty"` |
|||
// 文件数据
|
|||
Content []byte `protobuf:"bytes,2,opt,name=content,proto3" json:"content,omitempty"` |
|||
// 当前块的序号
|
|||
ChunkIndex int32 `protobuf:"varint,3,opt,name=chunk_index,json=chunkIndex,proto3" json:"chunk_index,omitempty"` |
|||
// 总块数
|
|||
TotalChunks int32 `protobuf:"varint,4,opt,name=total_chunks,json=totalChunks,proto3" json:"total_chunks,omitempty"` |
|||
unknownFields protoimpl.UnknownFields |
|||
sizeCache protoimpl.SizeCache |
|||
} |
|||
|
|||
func (x *FileChunk) Reset() { |
|||
*x = FileChunk{} |
|||
mi := &file_fileupload_proto_msgTypes[0] |
|||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) |
|||
ms.StoreMessageInfo(mi) |
|||
} |
|||
|
|||
func (x *FileChunk) String() string { |
|||
return protoimpl.X.MessageStringOf(x) |
|||
} |
|||
|
|||
func (*FileChunk) ProtoMessage() {} |
|||
|
|||
func (x *FileChunk) ProtoReflect() protoreflect.Message { |
|||
mi := &file_fileupload_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 FileChunk.ProtoReflect.Descriptor instead.
|
|||
func (*FileChunk) Descriptor() ([]byte, []int) { |
|||
return file_fileupload_proto_rawDescGZIP(), []int{0} |
|||
} |
|||
|
|||
func (x *FileChunk) GetInfo() *FileInfo { |
|||
if x != nil { |
|||
return x.Info |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (x *FileChunk) GetContent() []byte { |
|||
if x != nil { |
|||
return x.Content |
|||
} |
|||
return nil |
|||
} |
|||
|
|||
func (x *FileChunk) GetChunkIndex() int32 { |
|||
if x != nil { |
|||
return x.ChunkIndex |
|||
} |
|||
return 0 |
|||
} |
|||
|
|||
func (x *FileChunk) GetTotalChunks() int32 { |
|||
if x != nil { |
|||
return x.TotalChunks |
|||
} |
|||
return 0 |
|||
} |
|||
|
|||
// 文件元信息
|
|||
type FileInfo struct { |
|||
state protoimpl.MessageState `protogen:"open.v1"` |
|||
FileName string `protobuf:"bytes,1,opt,name=file_name,json=fileName,proto3" json:"file_name,omitempty"` // 文件名
|
|||
FileSize int64 `protobuf:"varint,2,opt,name=file_size,json=fileSize,proto3" json:"file_size,omitempty"` // 文件大小(字节)
|
|||
ContentType string `protobuf:"bytes,3,opt,name=content_type,json=contentType,proto3" json:"content_type,omitempty"` // 文件类型
|
|||
UploadPath string `protobuf:"bytes,4,opt,name=upload_path,json=uploadPath,proto3" json:"upload_path,omitempty"` // 上传路径
|
|||
unknownFields protoimpl.UnknownFields |
|||
sizeCache protoimpl.SizeCache |
|||
} |
|||
|
|||
func (x *FileInfo) Reset() { |
|||
*x = FileInfo{} |
|||
mi := &file_fileupload_proto_msgTypes[1] |
|||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) |
|||
ms.StoreMessageInfo(mi) |
|||
} |
|||
|
|||
func (x *FileInfo) String() string { |
|||
return protoimpl.X.MessageStringOf(x) |
|||
} |
|||
|
|||
func (*FileInfo) ProtoMessage() {} |
|||
|
|||
func (x *FileInfo) ProtoReflect() protoreflect.Message { |
|||
mi := &file_fileupload_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 FileInfo.ProtoReflect.Descriptor instead.
|
|||
func (*FileInfo) Descriptor() ([]byte, []int) { |
|||
return file_fileupload_proto_rawDescGZIP(), []int{1} |
|||
} |
|||
|
|||
func (x *FileInfo) GetFileName() string { |
|||
if x != nil { |
|||
return x.FileName |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
func (x *FileInfo) GetFileSize() int64 { |
|||
if x != nil { |
|||
return x.FileSize |
|||
} |
|||
return 0 |
|||
} |
|||
|
|||
func (x *FileInfo) GetContentType() string { |
|||
if x != nil { |
|||
return x.ContentType |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
func (x *FileInfo) GetUploadPath() string { |
|||
if x != nil { |
|||
return x.UploadPath |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
// 上传响应
|
|||
type UploadResponse struct { |
|||
state protoimpl.MessageState `protogen:"open.v1"` |
|||
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` // 是否成功
|
|||
Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` // 响应消息
|
|||
FilePath string `protobuf:"bytes,3,opt,name=file_path,json=filePath,proto3" json:"file_path,omitempty"` // 服务器保存路径
|
|||
ReceivedSize int64 `protobuf:"varint,4,opt,name=received_size,json=receivedSize,proto3" json:"received_size,omitempty"` // 接收的文件大小
|
|||
unknownFields protoimpl.UnknownFields |
|||
sizeCache protoimpl.SizeCache |
|||
} |
|||
|
|||
func (x *UploadResponse) Reset() { |
|||
*x = UploadResponse{} |
|||
mi := &file_fileupload_proto_msgTypes[2] |
|||
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_fileupload_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 UploadResponse.ProtoReflect.Descriptor instead.
|
|||
func (*UploadResponse) Descriptor() ([]byte, []int) { |
|||
return file_fileupload_proto_rawDescGZIP(), []int{2} |
|||
} |
|||
|
|||
func (x *UploadResponse) GetSuccess() bool { |
|||
if x != nil { |
|||
return x.Success |
|||
} |
|||
return false |
|||
} |
|||
|
|||
func (x *UploadResponse) GetMessage() string { |
|||
if x != nil { |
|||
return x.Message |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
func (x *UploadResponse) GetFilePath() string { |
|||
if x != nil { |
|||
return x.FilePath |
|||
} |
|||
return "" |
|||
} |
|||
|
|||
func (x *UploadResponse) GetReceivedSize() int64 { |
|||
if x != nil { |
|||
return x.ReceivedSize |
|||
} |
|||
return 0 |
|||
} |
|||
|
|||
var File_fileupload_proto protoreflect.FileDescriptor |
|||
|
|||
const file_fileupload_proto_rawDesc = "" + |
|||
"\n" + |
|||
"\x10fileupload.proto\x12\n" + |
|||
"fileupload\"\x93\x01\n" + |
|||
"\tFileChunk\x12(\n" + |
|||
"\x04info\x18\x01 \x01(\v2\x14.fileupload.FileInfoR\x04info\x12\x18\n" + |
|||
"\acontent\x18\x02 \x01(\fR\acontent\x12\x1f\n" + |
|||
"\vchunk_index\x18\x03 \x01(\x05R\n" + |
|||
"chunkIndex\x12!\n" + |
|||
"\ftotal_chunks\x18\x04 \x01(\x05R\vtotalChunks\"\x88\x01\n" + |
|||
"\bFileInfo\x12\x1b\n" + |
|||
"\tfile_name\x18\x01 \x01(\tR\bfileName\x12\x1b\n" + |
|||
"\tfile_size\x18\x02 \x01(\x03R\bfileSize\x12!\n" + |
|||
"\fcontent_type\x18\x03 \x01(\tR\vcontentType\x12\x1f\n" + |
|||
"\vupload_path\x18\x04 \x01(\tR\n" + |
|||
"uploadPath\"\x86\x01\n" + |
|||
"\x0eUploadResponse\x12\x18\n" + |
|||
"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x18\n" + |
|||
"\amessage\x18\x02 \x01(\tR\amessage\x12\x1b\n" + |
|||
"\tfile_path\x18\x03 \x01(\tR\bfilePath\x12#\n" + |
|||
"\rreceived_size\x18\x04 \x01(\x03R\freceivedSize2V\n" + |
|||
"\x11FileUploadService\x12A\n" + |
|||
"\n" + |
|||
"UploadFile\x12\x15.fileupload.FileChunk\x1a\x1a.fileupload.UploadResponse(\x01B\x0eZ\f./fileuploadb\x06proto3" |
|||
|
|||
var ( |
|||
file_fileupload_proto_rawDescOnce sync.Once |
|||
file_fileupload_proto_rawDescData []byte |
|||
) |
|||
|
|||
func file_fileupload_proto_rawDescGZIP() []byte { |
|||
file_fileupload_proto_rawDescOnce.Do(func() { |
|||
file_fileupload_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_fileupload_proto_rawDesc), len(file_fileupload_proto_rawDesc))) |
|||
}) |
|||
return file_fileupload_proto_rawDescData |
|||
} |
|||
|
|||
var file_fileupload_proto_msgTypes = make([]protoimpl.MessageInfo, 3) |
|||
var file_fileupload_proto_goTypes = []any{ |
|||
(*FileChunk)(nil), // 0: fileupload.FileChunk
|
|||
(*FileInfo)(nil), // 1: fileupload.FileInfo
|
|||
(*UploadResponse)(nil), // 2: fileupload.UploadResponse
|
|||
} |
|||
var file_fileupload_proto_depIdxs = []int32{ |
|||
1, // 0: fileupload.FileChunk.info:type_name -> fileupload.FileInfo
|
|||
0, // 1: fileupload.FileUploadService.UploadFile:input_type -> fileupload.FileChunk
|
|||
2, // 2: fileupload.FileUploadService.UploadFile:output_type -> fileupload.UploadResponse
|
|||
2, // [2:3] is the sub-list for method output_type
|
|||
1, // [1:2] is the sub-list for method input_type
|
|||
1, // [1:1] is the sub-list for extension type_name
|
|||
1, // [1:1] is the sub-list for extension extendee
|
|||
0, // [0:1] is the sub-list for field type_name
|
|||
} |
|||
|
|||
func init() { file_fileupload_proto_init() } |
|||
func file_fileupload_proto_init() { |
|||
if File_fileupload_proto != nil { |
|||
return |
|||
} |
|||
type x struct{} |
|||
out := protoimpl.TypeBuilder{ |
|||
File: protoimpl.DescBuilder{ |
|||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), |
|||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_fileupload_proto_rawDesc), len(file_fileupload_proto_rawDesc)), |
|||
NumEnums: 0, |
|||
NumMessages: 3, |
|||
NumExtensions: 0, |
|||
NumServices: 1, |
|||
}, |
|||
GoTypes: file_fileupload_proto_goTypes, |
|||
DependencyIndexes: file_fileupload_proto_depIdxs, |
|||
MessageInfos: file_fileupload_proto_msgTypes, |
|||
}.Build() |
|||
File_fileupload_proto = out.File |
|||
file_fileupload_proto_goTypes = nil |
|||
file_fileupload_proto_depIdxs = nil |
|||
} |
|||
@ -0,0 +1,121 @@ |
|||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
|||
// versions:
|
|||
// - protoc-gen-go-grpc v1.5.1
|
|||
// - protoc v6.30.2
|
|||
// source: fileupload.proto
|
|||
|
|||
package fileupload |
|||
|
|||
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 ( |
|||
FileUploadService_UploadFile_FullMethodName = "/fileupload.FileUploadService/UploadFile" |
|||
) |
|||
|
|||
// FileUploadServiceClient is the client API for FileUploadService 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 FileUploadServiceClient interface { |
|||
// 上传文件的 RPC 方法,使用流式传输
|
|||
UploadFile(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[FileChunk, UploadResponse], error) |
|||
} |
|||
|
|||
type fileUploadServiceClient struct { |
|||
cc grpc.ClientConnInterface |
|||
} |
|||
|
|||
func NewFileUploadServiceClient(cc grpc.ClientConnInterface) FileUploadServiceClient { |
|||
return &fileUploadServiceClient{cc} |
|||
} |
|||
|
|||
func (c *fileUploadServiceClient) UploadFile(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[FileChunk, UploadResponse], error) { |
|||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) |
|||
stream, err := c.cc.NewStream(ctx, &FileUploadService_ServiceDesc.Streams[0], FileUploadService_UploadFile_FullMethodName, cOpts...) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
x := &grpc.GenericClientStream[FileChunk, UploadResponse]{ClientStream: stream} |
|||
return x, nil |
|||
} |
|||
|
|||
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
|
|||
type FileUploadService_UploadFileClient = grpc.ClientStreamingClient[FileChunk, UploadResponse] |
|||
|
|||
// FileUploadServiceServer is the server API for FileUploadService service.
|
|||
// All implementations must embed UnimplementedFileUploadServiceServer
|
|||
// for forward compatibility.
|
|||
//
|
|||
// 文件上传服务定义
|
|||
type FileUploadServiceServer interface { |
|||
// 上传文件的 RPC 方法,使用流式传输
|
|||
UploadFile(grpc.ClientStreamingServer[FileChunk, UploadResponse]) error |
|||
mustEmbedUnimplementedFileUploadServiceServer() |
|||
} |
|||
|
|||
// UnimplementedFileUploadServiceServer 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 UnimplementedFileUploadServiceServer struct{} |
|||
|
|||
func (UnimplementedFileUploadServiceServer) UploadFile(grpc.ClientStreamingServer[FileChunk, UploadResponse]) error { |
|||
return status.Errorf(codes.Unimplemented, "method UploadFile not implemented") |
|||
} |
|||
func (UnimplementedFileUploadServiceServer) mustEmbedUnimplementedFileUploadServiceServer() {} |
|||
func (UnimplementedFileUploadServiceServer) testEmbeddedByValue() {} |
|||
|
|||
// UnsafeFileUploadServiceServer may be embedded to opt out of forward compatibility for this service.
|
|||
// Use of this interface is not recommended, as added methods to FileUploadServiceServer will
|
|||
// result in compilation errors.
|
|||
type UnsafeFileUploadServiceServer interface { |
|||
mustEmbedUnimplementedFileUploadServiceServer() |
|||
} |
|||
|
|||
func RegisterFileUploadServiceServer(s grpc.ServiceRegistrar, srv FileUploadServiceServer) { |
|||
// If the following call pancis, it indicates UnimplementedFileUploadServiceServer 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(&FileUploadService_ServiceDesc, srv) |
|||
} |
|||
|
|||
func _FileUploadService_UploadFile_Handler(srv interface{}, stream grpc.ServerStream) error { |
|||
return srv.(FileUploadServiceServer).UploadFile(&grpc.GenericServerStream[FileChunk, UploadResponse]{ServerStream: stream}) |
|||
} |
|||
|
|||
// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
|
|||
type FileUploadService_UploadFileServer = grpc.ClientStreamingServer[FileChunk, UploadResponse] |
|||
|
|||
// FileUploadService_ServiceDesc is the grpc.ServiceDesc for FileUploadService service.
|
|||
// It's only intended for direct use with grpc.RegisterService,
|
|||
// and not to be introspected or modified (even as a copy)
|
|||
var FileUploadService_ServiceDesc = grpc.ServiceDesc{ |
|||
ServiceName: "fileupload.FileUploadService", |
|||
HandlerType: (*FileUploadServiceServer)(nil), |
|||
Methods: []grpc.MethodDesc{}, |
|||
Streams: []grpc.StreamDesc{ |
|||
{ |
|||
StreamName: "UploadFile", |
|||
Handler: _FileUploadService_UploadFile_Handler, |
|||
ClientStreams: true, |
|||
}, |
|||
}, |
|||
Metadata: "fileupload.proto", |
|||
} |
|||
Loading…
Reference in new issue