grpc.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package metadata
  2. import (
  3. "context"
  4. "time"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. "google.golang.org/protobuf/types/known/emptypb"
  8. "github.com/drakkan/sftpgo/v2/sdk/plugin/metadata/proto"
  9. )
  10. const (
  11. rpcTimeout = 20 * time.Second
  12. )
  13. // GRPCClient is an implementation of Metadater interface that talks over RPC.
  14. type GRPCClient struct {
  15. client proto.MetadataClient
  16. }
  17. // SetModificationTime implements the Metadater interface
  18. func (c *GRPCClient) SetModificationTime(storageID, objectPath string, mTime int64) error {
  19. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  20. defer cancel()
  21. _, err := c.client.SetModificationTime(ctx, &proto.SetModificationTimeRequest{
  22. StorageId: storageID,
  23. ObjectPath: objectPath,
  24. ModificationTime: mTime,
  25. })
  26. return c.checkError(err)
  27. }
  28. // GetModificationTime implements the Metadater interface
  29. func (c *GRPCClient) GetModificationTime(storageID, objectPath string) (int64, error) {
  30. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  31. defer cancel()
  32. resp, err := c.client.GetModificationTime(ctx, &proto.GetModificationTimeRequest{
  33. StorageId: storageID,
  34. ObjectPath: objectPath,
  35. })
  36. if err != nil {
  37. return 0, c.checkError(err)
  38. }
  39. return resp.ModificationTime, nil
  40. }
  41. // GetModificationTimes implements the Metadater interface
  42. func (c *GRPCClient) GetModificationTimes(storageID, objectPath string) (map[string]int64, error) {
  43. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout*4)
  44. defer cancel()
  45. resp, err := c.client.GetModificationTimes(ctx, &proto.GetModificationTimesRequest{
  46. StorageId: storageID,
  47. FolderPath: objectPath,
  48. })
  49. if err != nil {
  50. return nil, c.checkError(err)
  51. }
  52. return resp.Pairs, nil
  53. }
  54. // RemoveMetadata implements the Metadater interface
  55. func (c *GRPCClient) RemoveMetadata(storageID, objectPath string) error {
  56. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  57. defer cancel()
  58. _, err := c.client.RemoveMetadata(ctx, &proto.RemoveMetadataRequest{
  59. StorageId: storageID,
  60. ObjectPath: objectPath,
  61. })
  62. return c.checkError(err)
  63. }
  64. // GetFolders implements the Metadater interface
  65. func (c *GRPCClient) GetFolders(storageID string, limit int, from string) ([]string, error) {
  66. ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
  67. defer cancel()
  68. resp, err := c.client.GetFolders(ctx, &proto.GetFoldersRequest{
  69. StorageId: storageID,
  70. Limit: int32(limit),
  71. From: from,
  72. })
  73. if err != nil {
  74. return nil, c.checkError(err)
  75. }
  76. return resp.Folders, nil
  77. }
  78. func (c *GRPCClient) checkError(err error) error {
  79. if err == nil {
  80. return nil
  81. }
  82. if s, ok := status.FromError(err); ok {
  83. if s.Code() == codes.NotFound {
  84. return ErrNoSuchObject
  85. }
  86. }
  87. return err
  88. }
  89. // GRPCServer defines the gRPC server that GRPCClient talks to.
  90. type GRPCServer struct {
  91. Impl Metadater
  92. }
  93. // SetModificationTime implements the server side set modification time method
  94. func (s *GRPCServer) SetModificationTime(ctx context.Context, req *proto.SetModificationTimeRequest) (*emptypb.Empty, error) {
  95. err := s.Impl.SetModificationTime(req.StorageId, req.ObjectPath, req.ModificationTime)
  96. return &emptypb.Empty{}, err
  97. }
  98. // GetModificationTime implements the server side get modification time method
  99. func (s *GRPCServer) GetModificationTime(ctx context.Context, req *proto.GetModificationTimeRequest) (
  100. *proto.GetModificationTimeResponse, error,
  101. ) {
  102. mTime, err := s.Impl.GetModificationTime(req.StorageId, req.ObjectPath)
  103. return &proto.GetModificationTimeResponse{
  104. ModificationTime: mTime,
  105. }, err
  106. }
  107. // GetModificationTimes implements the server side get modification times method
  108. func (s *GRPCServer) GetModificationTimes(ctx context.Context, req *proto.GetModificationTimesRequest) (
  109. *proto.GetModificationTimesResponse, error,
  110. ) {
  111. res, err := s.Impl.GetModificationTimes(req.StorageId, req.FolderPath)
  112. return &proto.GetModificationTimesResponse{
  113. Pairs: res,
  114. }, err
  115. }
  116. // RemoveMetadata implements the server side remove metadata method
  117. func (s *GRPCServer) RemoveMetadata(ctx context.Context, req *proto.RemoveMetadataRequest) (*emptypb.Empty, error) {
  118. err := s.Impl.RemoveMetadata(req.StorageId, req.ObjectPath)
  119. return &emptypb.Empty{}, err
  120. }
  121. // GetFolders implements the server side get folders method
  122. func (s *GRPCServer) GetFolders(ctx context.Context, req *proto.GetFoldersRequest) (*proto.GetFoldersResponse, error) {
  123. res, err := s.Impl.GetFolders(req.StorageId, int(req.Limit), req.From)
  124. return &proto.GetFoldersResponse{
  125. Folders: res,
  126. }, err
  127. }