1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package test
- import (
- "app.yhyue.com/moapp/jyfs/rpc/filesystem"
- "app.yhyue.com/moapp/jyfs/rpc/filesystemclient"
- "bytes"
- "compress/gzip"
- "fmt"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/zeromicro/go-zero/core/discov"
- "github.com/zeromicro/go-zero/zrpc"
- "google.golang.org/grpc"
- "log"
- "os"
- "testing"
- )
- var (
- Domain = "jydocs-std"
- )
- func Test_FileUpload(t *testing.T) {
- log.Println("-------------")
- file, err := os.Open("./2.txt")
- if err != nil {
- return
- }
- defer file.Close()
- // FileInfo:
- stats, err := file.Stat()
- if err != nil {
- return
- }
- // []byte
- data := make([]byte, stats.Size())
- count, err := file.Read(data)
- if err != nil {
- return
- }
- log.Println(count)
- suffix := "txt"
- //获取附件后上传oss
- fr := &filesystem.SaveFileReq{
- Domain: Domain,
- FileId: fmt.Sprintf("%s.%s", "101010003", suffix),
- Meta: map[string]string{
- "docName": "101010003",
- "docSuffix": suffix,
- "docSize": "1024",
- },
- RawFileContent: CompressWithGzip(data),
- Source: "docin",
- }
- if fr != nil && len(fr.RawFileContent) > 0 {
- conf := zrpc.RpcClientConf{
- Etcd: discov.EtcdConf{
- Key: "moapp.filesystem.rpc",
- Hosts: []string{"127.0.0.1:2379"},
- },
- }
- grpcOpts := []zrpc.ClientOption{
- //zrpc.WithDialOption(grpc.WithWriteBufferSize(40 * 1024 * 1024)), // 设置发送消息的最大字节数,这里是40MB
- //zrpc.WithDialOption(grpc.WithReadBufferSize(40 * 1024 * 1024)), // 设置接收消息的最大字节数,这里也是40MB
- zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(40 * 1024 * 1024))),
- zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(40 * 1024 * 1024))),
- }
- client := zrpc.MustNewClient(conf, grpcOpts...)
- jyFilelLib := filesystemclient.NewFileSystem(client)
- fRes, err := jyFilelLib.SaveFile(gctx.New(), fr)
- log.Println(fRes, "----------", err)
- }
- }
- func Test_GetFile(t *testing.T) {
- jyFilelLib := filesystemclient.NewFileSystem(zrpc.MustNewClient(zrpc.RpcClientConf{
- Etcd: discov.EtcdConf{
- Key: "moapp.filesystem.rpc",
- Hosts: []string{"127.0.0.1:2379"},
- },
- }))
- resp, err := jyFilelLib.GetOssUril(gctx.New(), &filesystem.LoadFileReq{
- Domain: Domain,
- FileId: "101010003.txt",
- })
- log.Println(resp, "-----", err)
- }
- func CompressWithGzip(data []byte) []byte {
- var buf bytes.Buffer
- gzipW := gzip.NewWriter(&buf)
- if _, err := gzipW.Write(data); err != nil {
- return nil
- }
- if err := gzipW.Close(); err != nil {
- return nil
- }
- return buf.Bytes()
- }
|