Of course we should determine type on the server....

This commit is contained in:
Justin Hawkins 2022-01-14 13:07:56 +10:30
parent dfe99f4ddb
commit 5adfd08595
3 changed files with 47 additions and 19 deletions

View File

@ -10,7 +10,6 @@ import (
"net"
"os"
"github.com/h2non/filetype"
"github.com/tardisx/netgiv/secure"
)
@ -53,12 +52,19 @@ func (c *Client) Connect() error {
panic(err)
}
data := secure.PacketSendDataStart{
Filename: "",
TotalSize: 0,
}
err = enc.Encode(data)
if err != nil {
panic(err)
}
nBytes, nChunks := int64(0), int64(0)
reader := bufio.NewReader(os.Stdin)
buf := make([]byte, 0, 1024)
startSent := false
for {
n, err := reader.Read(buf[:cap(buf)])
@ -76,21 +82,6 @@ func (c *Client) Connect() error {
nChunks++
nBytes += int64(len(buf))
if !startSent {
kind, _ := filetype.Match(buf)
data := secure.PacketSendDataStart{
Filename: "foobar",
TotalSize: 3,
Kind: kind.MIME.Value,
}
err = enc.Encode(data)
if err != nil {
panic(err)
}
log.Print("done that")
startSent = true
}
send := secure.PacketSendDataNext{
Size: 5000,
Data: buf,

View File

@ -188,7 +188,6 @@ type PacketStart struct {
type PacketSendDataStart struct {
Filename string
TotalSize uint32
Kind string
}
type PacketSendDataNext struct {

View File

@ -10,6 +10,8 @@ import (
"os"
"time"
"github.com/h2non/filetype"
"github.com/tardisx/netgiv/secure"
)
@ -17,6 +19,14 @@ type Server struct {
port int
}
// An NGF is a Netgiv File
type NGF struct {
StorePath string
Filename string // could be empty string if we were not supplied with one
Kind string //
Size uint64 // file size
}
func (s *Server) Run() {
address := fmt.Sprintf(":%d", s.port)
networkAddress, _ := net.ResolveTCPAddr("tcp", address)
@ -78,12 +88,22 @@ func handleConnection(conn *net.TCPConn) {
}
log.Printf("send start looks like: %v", sendStart)
file, err := os.CreateTemp("", "netgiv_")
defer file.Close()
ngf := NGF{
StorePath: file.Name(),
Filename: sendStart.Filename,
Kind: "",
Size: 0,
}
if err != nil {
log.Printf("got error with temp file: %w", err)
return
}
log.Printf("writing data to file: %s", file.Name())
sendData := secure.PacketSendDataNext{}
determinedKind := false
for {
conn.SetDeadline(time.Now().Add(time.Second * 5))
err = dec.Decode(&sendData)
@ -95,8 +115,26 @@ func handleConnection(conn *net.TCPConn) {
log.Printf("error decoding data next: %s", err)
return
}
// filetype.Match needs a few hundred bytes - I guess there is a chance
// we don't have enough in the very first packet? This might need rework.
if !determinedKind {
kind, _ := filetype.Match(sendData.Data)
ngf.Kind = kind.MIME.Value
determinedKind = true
}
file.Write(sendData.Data)
}
info, err := file.Stat()
if err != nil {
log.Printf("couldn't stat file %s", err)
return
}
ngf.Size = uint64(info.Size())
log.Printf("received a %#v", ngf)
file.Close()
return
} else {
log.Printf("bad operation")