76 lines
2.0 KiB
Go
Executable File
76 lines
2.0 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"lanrdp/internal/protocol"
|
|
)
|
|
|
|
// fileChunkSize is comfortably under protocol.MaxPayloadSize; small enough
|
|
// that a chunk send doesn't meaningfully delay a mouse-input message
|
|
// queued right behind it on the same connection.
|
|
const fileChunkSize = 256 * 1024
|
|
|
|
// sendFile reads path and streams it to the host as a MsgFileOffer
|
|
// followed by MsgFileChunk messages and a closing MsgFileComplete. Errors
|
|
// are logged, not returned — this always runs in a background goroutine
|
|
// (see handleDropFiles), so there's no caller to report back to directly;
|
|
// the host's MsgFileResult response is the real completion signal and is
|
|
// logged separately in netio.go.
|
|
func sendFile(path string) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
log.Printf("file transfer: opening %s: %v", path, err)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
log.Printf("file transfer: stat %s: %v", path, err)
|
|
return
|
|
}
|
|
if info.IsDir() {
|
|
log.Printf("file transfer: skipping directory %s (drop individual files, not folders)", path)
|
|
return
|
|
}
|
|
|
|
name := filepath.Base(path)
|
|
if err := app.send(protocol.MsgFileOffer, protocol.EncodeFileOffer(protocol.FileOffer{
|
|
Name: name, Size: uint64(info.Size()),
|
|
})); err != nil {
|
|
log.Printf("file transfer: sending offer for %s: %v", name, err)
|
|
return
|
|
}
|
|
|
|
buf := make([]byte, fileChunkSize)
|
|
var sent uint64
|
|
for {
|
|
n, readErr := f.Read(buf)
|
|
if n > 0 {
|
|
if err := app.send(protocol.MsgFileChunk, buf[:n]); err != nil {
|
|
log.Printf("file transfer: sending chunk for %s: %v", name, err)
|
|
return
|
|
}
|
|
sent += uint64(n)
|
|
}
|
|
if readErr == io.EOF {
|
|
break
|
|
}
|
|
if readErr != nil {
|
|
log.Printf("file transfer: reading %s: %v", path, readErr)
|
|
return
|
|
}
|
|
}
|
|
|
|
if err := app.send(protocol.MsgFileComplete, protocol.EncodeFileComplete(protocol.FileComplete{Size: sent})); err != nil {
|
|
log.Printf("file transfer: sending complete for %s: %v", name, err)
|
|
return
|
|
}
|
|
|
|
log.Printf("file transfer: sent %s (%d bytes), waiting for host confirmation", name, sent)
|
|
}
|