fileUpload_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package test
  2. import (
  3. "app.yhyue.com/moapp/jyfs/rpc/filesystem"
  4. "app.yhyue.com/moapp/jyfs/rpc/filesystemclient"
  5. "bytes"
  6. "compress/gzip"
  7. "fmt"
  8. "github.com/gogf/gf/v2/os/gctx"
  9. "github.com/zeromicro/go-zero/core/discov"
  10. "github.com/zeromicro/go-zero/zrpc"
  11. "google.golang.org/grpc"
  12. "log"
  13. "os"
  14. "testing"
  15. )
  16. var (
  17. Domain = "jydocs-std"
  18. )
  19. func Test_FileUpload(t *testing.T) {
  20. log.Println("-------------")
  21. file, err := os.Open("./2.txt")
  22. if err != nil {
  23. return
  24. }
  25. defer file.Close()
  26. // FileInfo:
  27. stats, err := file.Stat()
  28. if err != nil {
  29. return
  30. }
  31. // []byte
  32. data := make([]byte, stats.Size())
  33. count, err := file.Read(data)
  34. if err != nil {
  35. return
  36. }
  37. log.Println(count)
  38. suffix := "txt"
  39. //获取附件后上传oss
  40. fr := &filesystem.SaveFileReq{
  41. Domain: Domain,
  42. FileId: fmt.Sprintf("%s.%s", "101010003", suffix),
  43. Meta: map[string]string{
  44. "docName": "101010003",
  45. "docSuffix": suffix,
  46. "docSize": "1024",
  47. },
  48. RawFileContent: CompressWithGzip(data),
  49. Source: "docin",
  50. }
  51. if fr != nil && len(fr.RawFileContent) > 0 {
  52. conf := zrpc.RpcClientConf{
  53. Etcd: discov.EtcdConf{
  54. Key: "moapp.filesystem.rpc",
  55. Hosts: []string{"127.0.0.1:2379"},
  56. },
  57. }
  58. grpcOpts := []zrpc.ClientOption{
  59. //zrpc.WithDialOption(grpc.WithWriteBufferSize(40 * 1024 * 1024)), // 设置发送消息的最大字节数,这里是40MB
  60. //zrpc.WithDialOption(grpc.WithReadBufferSize(40 * 1024 * 1024)), // 设置接收消息的最大字节数,这里也是40MB
  61. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(40 * 1024 * 1024))),
  62. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(40 * 1024 * 1024))),
  63. }
  64. client := zrpc.MustNewClient(conf, grpcOpts...)
  65. jyFilelLib := filesystemclient.NewFileSystem(client)
  66. fRes, err := jyFilelLib.SaveFile(gctx.New(), fr)
  67. log.Println(fRes, "----------", err)
  68. }
  69. }
  70. func Test_GetFile(t *testing.T) {
  71. jyFilelLib := filesystemclient.NewFileSystem(zrpc.MustNewClient(zrpc.RpcClientConf{
  72. Etcd: discov.EtcdConf{
  73. Key: "moapp.filesystem.rpc",
  74. Hosts: []string{"127.0.0.1:2379"},
  75. },
  76. }))
  77. resp, err := jyFilelLib.GetOssUril(gctx.New(), &filesystem.LoadFileReq{
  78. Domain: Domain,
  79. FileId: "101010003.txt",
  80. })
  81. log.Println(resp, "-----", err)
  82. }
  83. func CompressWithGzip(data []byte) []byte {
  84. var buf bytes.Buffer
  85. gzipW := gzip.NewWriter(&buf)
  86. if _, err := gzipW.Write(data); err != nil {
  87. return nil
  88. }
  89. if err := gzipW.Close(); err != nil {
  90. return nil
  91. }
  92. return buf.Bytes()
  93. }