fileUpload_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/saintfish/chardet"
  10. "github.com/zeromicro/go-zero/core/discov"
  11. "github.com/zeromicro/go-zero/zrpc"
  12. "google.golang.org/grpc"
  13. "log"
  14. "os"
  15. "testing"
  16. )
  17. var (
  18. Domain = "jydoc-docin"
  19. )
  20. func Test_FileUpload(t *testing.T) {
  21. log.Println("-------------")
  22. file, err := os.Open("./2.txt")
  23. if err != nil {
  24. return
  25. }
  26. defer file.Close()
  27. // FileInfo:
  28. stats, err := file.Stat()
  29. if err != nil {
  30. return
  31. }
  32. // []byte
  33. data := make([]byte, stats.Size())
  34. count, err := file.Read(data)
  35. if err != nil {
  36. return
  37. }
  38. // 创建一个 chardet 检测器
  39. detector := chardet.NewTextDetector()
  40. // 检测文件编码
  41. result, err := detector.DetectBest(data)
  42. if err != nil {
  43. fmt.Println("Failed to detect file encoding:", err)
  44. return
  45. }
  46. // 输出编码结果
  47. fmt.Printf("File encoding: %s (confidence: %d)", result.Charset, result.Confidence*100)
  48. log.Println(count)
  49. suffix := "txt"
  50. //获取附件后上传oss
  51. fr := &filesystem.SaveFileReq{
  52. Domain: Domain,
  53. FileId: fmt.Sprintf("%s.%s", "101010003", suffix),
  54. Meta: map[string]string{
  55. "docName": "101010003",
  56. "docSuffix": suffix,
  57. "docSize": "1024",
  58. },
  59. RawFileContent: CompressWithGzip(data),
  60. Charset: result.Charset,
  61. }
  62. if fr != nil && len(fr.RawFileContent) > 0 {
  63. conf := zrpc.RpcClientConf{
  64. Etcd: discov.EtcdConf{
  65. Key: "moapp.filesystem.rpc",
  66. Hosts: []string{"127.0.0.1:2379"},
  67. },
  68. }
  69. grpcOpts := []zrpc.ClientOption{
  70. //zrpc.WithDialOption(grpc.WithWriteBufferSize(40 * 1024 * 1024)), // 设置发送消息的最大字节数,这里是40MB
  71. //zrpc.WithDialOption(grpc.WithReadBufferSize(40 * 1024 * 1024)), // 设置接收消息的最大字节数,这里也是40MB
  72. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(40 * 1024 * 1024))),
  73. zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(40 * 1024 * 1024))),
  74. }
  75. client := zrpc.MustNewClient(conf, grpcOpts...)
  76. jyFilelLib := filesystemclient.NewFileSystem(client)
  77. fRes, err := jyFilelLib.SaveFile(gctx.New(), fr)
  78. log.Println(fRes, "----------", err)
  79. }
  80. }
  81. func Test_GetFile(t *testing.T) {
  82. jyFilelLib := filesystemclient.NewFileSystem(zrpc.MustNewClient(zrpc.RpcClientConf{
  83. Etcd: discov.EtcdConf{
  84. Key: "moapp.filesystem.rpc",
  85. Hosts: []string{"127.0.0.1:2379"},
  86. },
  87. }))
  88. resp, err := jyFilelLib.GetOssUril(gctx.New(), &filesystem.LoadFileReq{
  89. Domain: Domain,
  90. FileId: "7c9d2eed45ff59ccaf7b0e175a3a9d02.docx",
  91. })
  92. log.Println(resp, "-----", err)
  93. }
  94. func CompressWithGzip(data []byte) []byte {
  95. var buf bytes.Buffer
  96. gzipW := gzip.NewWriter(&buf)
  97. if _, err := gzipW.Write(data); err != nil {
  98. return nil
  99. }
  100. if err := gzipW.Close(); err != nil {
  101. return nil
  102. }
  103. return buf.Bytes()
  104. }