(ctx context.Context, readCh <-chan wshrpc.RespOrErrorUnion[wshrpc.FileData], fileInfoCallback func(finfo wshrpc.FileInfo), dirCallback func(entries []*wshrpc.FileInfo) error, fileCallback func(data io.Reader) error)
| 61 | } |
| 62 | |
| 63 | func ReadFileStream(ctx context.Context, readCh <-chan wshrpc.RespOrErrorUnion[wshrpc.FileData], fileInfoCallback func(finfo wshrpc.FileInfo), dirCallback func(entries []*wshrpc.FileInfo) error, fileCallback func(data io.Reader) error) error { |
| 64 | var fileData *wshrpc.FileData |
| 65 | firstPk := true |
| 66 | isDir := false |
| 67 | drain := true |
| 68 | defer func() { |
| 69 | if drain { |
| 70 | utilfn.DrainChannelSafe(readCh, "ReadFileStream") |
| 71 | } |
| 72 | }() |
| 73 | |
| 74 | for { |
| 75 | select { |
| 76 | case <-ctx.Done(): |
| 77 | return fmt.Errorf("context cancelled: %v", context.Cause(ctx)) |
| 78 | case respUnion, ok := <-readCh: |
| 79 | if !ok { |
| 80 | drain = false |
| 81 | return nil |
| 82 | } |
| 83 | if respUnion.Error != nil { |
| 84 | return respUnion.Error |
| 85 | } |
| 86 | resp := respUnion.Response |
| 87 | if firstPk { |
| 88 | firstPk = false |
| 89 | // first packet has the fileinfo |
| 90 | if resp.Info == nil { |
| 91 | return fmt.Errorf("stream file protocol error, first pk fileinfo is empty") |
| 92 | } |
| 93 | fileData = &resp |
| 94 | if fileData.Info.IsDir { |
| 95 | isDir = true |
| 96 | } |
| 97 | fileInfoCallback(*fileData.Info) |
| 98 | continue |
| 99 | } |
| 100 | if isDir { |
| 101 | if len(resp.Entries) == 0 { |
| 102 | continue |
| 103 | } |
| 104 | if resp.Data64 != "" { |
| 105 | return fmt.Errorf("stream file protocol error, directory entry has data") |
| 106 | } |
| 107 | if err := dirCallback(resp.Entries); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | } else { |
| 111 | if resp.Data64 == "" { |
| 112 | continue |
| 113 | } |
| 114 | decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader([]byte(resp.Data64))) |
| 115 | if err := fileCallback(decoder); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
no test coverage detected