You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_transport.go 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package grpc
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. myio "gitlink.org.cn/cloudream/common/utils/io"
  7. "gitlink.org.cn/cloudream/storage-common/pkgs/proto"
  8. )
  9. type fileReadCloser struct {
  10. io.ReadCloser
  11. stream proto.FileTransport_GetFileClient
  12. cancelFn context.CancelFunc
  13. readData []byte
  14. }
  15. func (s *fileReadCloser) Read(p []byte) (int, error) {
  16. if s.readData == nil {
  17. resp, err := s.stream.Recv()
  18. if err != nil {
  19. return 0, err
  20. }
  21. if resp.Type == proto.FileDataPacketType_Data {
  22. s.readData = resp.Data
  23. } else if resp.Type == proto.FileDataPacketType_EOF {
  24. return 0, io.EOF
  25. } else {
  26. return 0, fmt.Errorf("unsuppoted packt type: %v", resp.Type)
  27. }
  28. }
  29. cnt := copy(p, s.readData)
  30. if len(s.readData) == cnt {
  31. s.readData = nil
  32. } else {
  33. s.readData = s.readData[cnt:]
  34. }
  35. return cnt, nil
  36. }
  37. func (s *fileReadCloser) Close() error {
  38. s.cancelFn()
  39. return nil
  40. }
  41. func GetFileAsStream(client proto.FileTransportClient, fileHash string) (io.ReadCloser, error) {
  42. ctx, cancel := context.WithCancel(context.Background())
  43. stream, err := client.GetFile(ctx, &proto.GetReq{
  44. FileHash: fileHash,
  45. })
  46. if err != nil {
  47. cancel()
  48. return nil, fmt.Errorf("request grpc failed, err: %w", err)
  49. }
  50. return &fileReadCloser{
  51. stream: stream,
  52. cancelFn: cancel,
  53. }, nil
  54. }
  55. type fileWriteCloser struct {
  56. myio.PromiseWriteCloser[string]
  57. stream proto.FileTransport_SendFileClient
  58. }
  59. func (s *fileWriteCloser) Write(p []byte) (int, error) {
  60. err := s.stream.Send(&proto.FileDataPacket{
  61. Type: proto.FileDataPacketType_Data,
  62. Data: p,
  63. })
  64. if err != nil {
  65. return 0, err
  66. }
  67. return len(p), nil
  68. }
  69. func (s *fileWriteCloser) Abort(err error) {
  70. s.stream.CloseSend()
  71. }
  72. func (s *fileWriteCloser) Finish() (string, error) {
  73. err := s.stream.Send(&proto.FileDataPacket{
  74. Type: proto.FileDataPacketType_EOF,
  75. })
  76. if err != nil {
  77. return "", fmt.Errorf("send EOF packet failed, err: %w", err)
  78. }
  79. resp, err := s.stream.CloseAndRecv()
  80. if err != nil {
  81. return "", fmt.Errorf("receive response failed, err: %w", err)
  82. }
  83. return resp.FileHash, nil
  84. }
  85. func SendFileAsStream(client proto.FileTransportClient) (myio.PromiseWriteCloser[string], error) {
  86. stream, err := client.SendFile(context.Background())
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &fileWriteCloser{
  91. stream: stream,
  92. }, nil
  93. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。