1- // Copyright 2025 , Command Line Inc.
1+ // Copyright 2026 , Command Line Inc.
22// SPDX-License-Identifier: Apache-2.0
33
44package wshfs
@@ -7,12 +7,12 @@ import (
77 "context"
88 "encoding/base64"
99 "fmt"
10+ "io"
1011 "log"
1112 "os"
1213 "time"
1314
1415 "github.com/wavetermdev/waveterm/pkg/remote/connparse"
15- "github.com/wavetermdev/waveterm/pkg/remote/fileshare/fsutil"
1616 "github.com/wavetermdev/waveterm/pkg/wshrpc"
1717 "github.com/wavetermdev/waveterm/pkg/wshrpc/wshclient"
1818 "github.com/wavetermdev/waveterm/pkg/wshutil"
@@ -41,22 +41,54 @@ func parseConnection(ctx context.Context, path string) (*connparse.Connection, e
4141}
4242
4343func Read (ctx context.Context , data wshrpc.FileData ) (* wshrpc.FileData , error ) {
44+ if data .Info == nil {
45+ return nil , fmt .Errorf ("file info is required" )
46+ }
4447 log .Printf ("Read: %v" , data .Info .Path )
4548 conn , err := parseConnection (ctx , data .Info .Path )
4649 if err != nil {
4750 return nil , err
4851 }
49- rtnCh := readStream (conn , data )
50- return fsutil .ReadStreamToFileData (ctx , rtnCh )
51- }
52-
53- func readStream (conn * connparse.Connection , data wshrpc.FileData ) <- chan wshrpc.RespOrErrorUnion [wshrpc.FileData ] {
52+ broker := RpcClient .StreamBroker
53+ if broker == nil {
54+ return nil , fmt .Errorf ("stream broker not available" )
55+ }
56+ if RpcClientRouteId == "" {
57+ return nil , fmt .Errorf ("no route id available" )
58+ }
59+ readerRouteId := RpcClientRouteId
60+ writerRouteId := wshutil .MakeConnectionRouteId (conn .Host )
61+ reader , streamMeta := broker .CreateStreamReader (readerRouteId , writerRouteId , 256 * 1024 )
62+ defer reader .Close ()
63+ go func () {
64+ <- ctx .Done ()
65+ reader .Close ()
66+ }()
5467 byteRange := ""
5568 if data .At != nil && data .At .Size > 0 {
5669 byteRange = fmt .Sprintf ("%d-%d" , data .At .Offset , data .At .Offset + int64 (data .At .Size )- 1 )
5770 }
58- streamFileData := wshrpc.CommandRemoteStreamFileData {Path : conn .Path , ByteRange : byteRange }
59- return wshclient .RemoteStreamFileCommand (RpcClient , streamFileData , & wshrpc.RpcOpts {Route : wshutil .MakeConnectionRouteId (conn .Host )})
71+ remoteData := wshrpc.CommandRemoteFileStreamData {
72+ Path : conn .Path ,
73+ ByteRange : byteRange ,
74+ StreamMeta : * streamMeta ,
75+ }
76+ fileInfo , err := wshclient .RemoteFileStreamCommand (RpcClient , remoteData , & wshrpc.RpcOpts {Route : writerRouteId })
77+ if err != nil {
78+ return nil , fmt .Errorf ("starting remote file stream: %w" , err )
79+ }
80+ var rawData []byte
81+ if fileInfo != nil && ! fileInfo .IsDir {
82+ rawData , err = io .ReadAll (reader )
83+ if err != nil {
84+ return nil , fmt .Errorf ("reading file stream: %w" , err )
85+ }
86+ }
87+ rtnData := & wshrpc.FileData {Info : fileInfo }
88+ if len (rawData ) > 0 {
89+ rtnData .Data64 = base64 .StdEncoding .EncodeToString (rawData )
90+ }
91+ return rtnData , nil
6092}
6193
6294func GetConnectionRouteId (ctx context.Context , path string ) (string , error ) {
0 commit comments