Browse Source

feat:xiugai

wangchuanjin 1 month ago
parent
commit
873850cb57
4 changed files with 43 additions and 41 deletions
  1. 8 14
      client.go
  2. 3 3
      pb/service.pb.go
  3. 1 1
      pb/service.proto
  4. 31 23
      pb/service_grpc.pb.go

+ 8 - 14
client.go

@@ -283,29 +283,23 @@ func GrpcCall(address string, timeout int64) (pb.ServiceClient, context.Context,
  * @param args.objectName 对象名称
  * @param args.Stream 文件流
  * @param args.Gzip 是否压缩
+ * @param timeout 超时时间
  * @return {"error_code":0,"error_msg":"上传成功"}
  * @return error 错误信息
  */
-func UpLoadByGrpc(address string, argsFunc func() (*pb.UploadRequest, bool), timeout int64) (*pb.Response, error) {
+func UpLoadByGrpc(address string, args *entity.UploadArgs, timeout int64) (*pb.Response, error) {
 	serviceClient, ctx, cancel, err := GrpcCall(address, timeout)
 	defer cancel()
 	result := &pb.Response{}
 	if err != nil {
 		return result, err
 	}
-	stream, err := serviceClient.Upload(ctx)
-	if err != nil {
-		log.Println(err)
-		return nil, err
-	}
-	for {
-		if args, ok := argsFunc(); ok {
-			stream.Send(args)
-		} else {
-			break
-		}
-	}
-	return result, err
+	return serviceClient.Upload(ctx, &pb.UploadRequest{
+		Stream:     args.Stream,
+		Gzip:       args.Gzip,
+		BucketID:   args.BucketID,
+		ObjectName: args.ObjectName,
+	})
 }
 
 /*

+ 3 - 3
pb/service.pb.go

@@ -221,10 +221,10 @@ const file_service_proto_rawDesc = "" +
 	"\bResponse\x12\x12\n" +
 	"\x04data\x18\x01 \x01(\fR\x04data\x12\x1c\n" +
 	"\terrorCode\x18\x02 \x01(\x03R\terrorCode\x12\x1a\n" +
-	"\berrorMsg\x18\x03 \x01(\tR\berrorMsg2\x9c\x01\n" +
+	"\berrorMsg\x18\x03 \x01(\tR\berrorMsg2\x9a\x01\n" +
 	"\aService\x12/\n" +
-	"\bDownload\x12\x13.pb.DownloadRequest\x1a\f.pb.Response0\x01\x12+\n" +
-	"\x06Upload\x12\x11.pb.UploadRequest\x1a\f.pb.Response(\x01\x123\n" +
+	"\bDownload\x12\x13.pb.DownloadRequest\x1a\f.pb.Response0\x01\x12)\n" +
+	"\x06Upload\x12\x11.pb.UploadRequest\x1a\f.pb.Response\x123\n" +
 	"\fGetBidDetail\x12\x13.pb.DownloadRequest\x1a\f.pb.Response0\x01B\aZ\x05../pbb\x06proto3"
 
 var (

+ 1 - 1
pb/service.proto

@@ -6,7 +6,7 @@ option go_package = "../pb";
 
 service Service {
   rpc Download (DownloadRequest) returns (stream Response);
-  rpc Upload (stream UploadRequest) returns (Response);
+  rpc Upload (UploadRequest) returns (Response);
   rpc GetBidDetail (DownloadRequest) returns (stream Response);
 }
 

+ 31 - 23
pb/service_grpc.pb.go

@@ -29,7 +29,7 @@ const (
 // 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 ServiceClient interface {
 	Download(ctx context.Context, in *DownloadRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Response], error)
-	Upload(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[UploadRequest, Response], error)
+	Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*Response, error)
 	GetBidDetail(ctx context.Context, in *DownloadRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Response], error)
 }
 
@@ -60,22 +60,19 @@ func (c *serviceClient) Download(ctx context.Context, in *DownloadRequest, opts
 // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
 type Service_DownloadClient = grpc.ServerStreamingClient[Response]
 
-func (c *serviceClient) Upload(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[UploadRequest, Response], error) {
+func (c *serviceClient) Upload(ctx context.Context, in *UploadRequest, opts ...grpc.CallOption) (*Response, error) {
 	cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
-	stream, err := c.cc.NewStream(ctx, &Service_ServiceDesc.Streams[1], Service_Upload_FullMethodName, cOpts...)
+	out := new(Response)
+	err := c.cc.Invoke(ctx, Service_Upload_FullMethodName, in, out, cOpts...)
 	if err != nil {
 		return nil, err
 	}
-	x := &grpc.GenericClientStream[UploadRequest, Response]{ClientStream: stream}
-	return x, nil
+	return out, nil
 }
 
-// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
-type Service_UploadClient = grpc.ClientStreamingClient[UploadRequest, Response]
-
 func (c *serviceClient) GetBidDetail(ctx context.Context, in *DownloadRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Response], error) {
 	cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
-	stream, err := c.cc.NewStream(ctx, &Service_ServiceDesc.Streams[2], Service_GetBidDetail_FullMethodName, cOpts...)
+	stream, err := c.cc.NewStream(ctx, &Service_ServiceDesc.Streams[1], Service_GetBidDetail_FullMethodName, cOpts...)
 	if err != nil {
 		return nil, err
 	}
@@ -97,7 +94,7 @@ type Service_GetBidDetailClient = grpc.ServerStreamingClient[Response]
 // for forward compatibility.
 type ServiceServer interface {
 	Download(*DownloadRequest, grpc.ServerStreamingServer[Response]) error
-	Upload(grpc.ClientStreamingServer[UploadRequest, Response]) error
+	Upload(context.Context, *UploadRequest) (*Response, error)
 	GetBidDetail(*DownloadRequest, grpc.ServerStreamingServer[Response]) error
 	mustEmbedUnimplementedServiceServer()
 }
@@ -112,8 +109,8 @@ type UnimplementedServiceServer struct{}
 func (UnimplementedServiceServer) Download(*DownloadRequest, grpc.ServerStreamingServer[Response]) error {
 	return status.Errorf(codes.Unimplemented, "method Download not implemented")
 }
-func (UnimplementedServiceServer) Upload(grpc.ClientStreamingServer[UploadRequest, Response]) error {
-	return status.Errorf(codes.Unimplemented, "method Upload not implemented")
+func (UnimplementedServiceServer) Upload(context.Context, *UploadRequest) (*Response, error) {
+	return nil, status.Errorf(codes.Unimplemented, "method Upload not implemented")
 }
 func (UnimplementedServiceServer) GetBidDetail(*DownloadRequest, grpc.ServerStreamingServer[Response]) error {
 	return status.Errorf(codes.Unimplemented, "method GetBidDetail not implemented")
@@ -150,13 +147,24 @@ func _Service_Download_Handler(srv interface{}, stream grpc.ServerStream) error
 // This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
 type Service_DownloadServer = grpc.ServerStreamingServer[Response]
 
-func _Service_Upload_Handler(srv interface{}, stream grpc.ServerStream) error {
-	return srv.(ServiceServer).Upload(&grpc.GenericServerStream[UploadRequest, Response]{ServerStream: stream})
+func _Service_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.(ServiceServer).Upload(ctx, in)
+	}
+	info := &grpc.UnaryServerInfo{
+		Server:     srv,
+		FullMethod: Service_Upload_FullMethodName,
+	}
+	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
+		return srv.(ServiceServer).Upload(ctx, req.(*UploadRequest))
+	}
+	return interceptor(ctx, in, info, handler)
 }
 
-// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name.
-type Service_UploadServer = grpc.ClientStreamingServer[UploadRequest, Response]
-
 func _Service_GetBidDetail_Handler(srv interface{}, stream grpc.ServerStream) error {
 	m := new(DownloadRequest)
 	if err := stream.RecvMsg(m); err != nil {
@@ -174,18 +182,18 @@ type Service_GetBidDetailServer = grpc.ServerStreamingServer[Response]
 var Service_ServiceDesc = grpc.ServiceDesc{
 	ServiceName: "pb.Service",
 	HandlerType: (*ServiceServer)(nil),
-	Methods:     []grpc.MethodDesc{},
+	Methods: []grpc.MethodDesc{
+		{
+			MethodName: "Upload",
+			Handler:    _Service_Upload_Handler,
+		},
+	},
 	Streams: []grpc.StreamDesc{
 		{
 			StreamName:    "Download",
 			Handler:       _Service_Download_Handler,
 			ServerStreams: true,
 		},
-		{
-			StreamName:    "Upload",
-			Handler:       _Service_Upload_Handler,
-			ClientStreams: true,
-		},
 		{
 			StreamName:    "GetBidDetail",
 			Handler:       _Service_GetBidDetail_Handler,