123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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/saintfish/chardet"
- "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
- }
- // 创建一个 chardet 检测器
- detector := chardet.NewTextDetector()
- // 检测文件编码
- result, err := detector.DetectBest(data)
- if err != nil {
- fmt.Println("Failed to detect file encoding:", err)
- return
- }
- // 输出编码结果
- fmt.Printf("File encoding: %s (confidence: %d)", result.Charset, result.Confidence*100)
- 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),
- Charset: result.Charset,
- }
- 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: "dc2687989f5a2c2f91c5a9a40d5ffb29.doc",
- })
- 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()
- }
|