Of course we should determine type on the server....
This commit is contained in:
parent
dfe99f4ddb
commit
5adfd08595
27
client.go
27
client.go
@ -10,7 +10,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/h2non/filetype"
|
|
||||||
"github.com/tardisx/netgiv/secure"
|
"github.com/tardisx/netgiv/secure"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,12 +52,19 @@ func (c *Client) Connect() error {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data := secure.PacketSendDataStart{
|
||||||
|
Filename: "",
|
||||||
|
TotalSize: 0,
|
||||||
|
}
|
||||||
|
err = enc.Encode(data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
nBytes, nChunks := int64(0), int64(0)
|
nBytes, nChunks := int64(0), int64(0)
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
buf := make([]byte, 0, 1024)
|
buf := make([]byte, 0, 1024)
|
||||||
|
|
||||||
startSent := false
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := reader.Read(buf[:cap(buf)])
|
n, err := reader.Read(buf[:cap(buf)])
|
||||||
|
|
||||||
@ -76,21 +82,6 @@ func (c *Client) Connect() error {
|
|||||||
nChunks++
|
nChunks++
|
||||||
nBytes += int64(len(buf))
|
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{
|
send := secure.PacketSendDataNext{
|
||||||
Size: 5000,
|
Size: 5000,
|
||||||
Data: buf,
|
Data: buf,
|
||||||
|
@ -188,7 +188,6 @@ type PacketStart struct {
|
|||||||
type PacketSendDataStart struct {
|
type PacketSendDataStart struct {
|
||||||
Filename string
|
Filename string
|
||||||
TotalSize uint32
|
TotalSize uint32
|
||||||
Kind string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PacketSendDataNext struct {
|
type PacketSendDataNext struct {
|
||||||
|
38
server.go
38
server.go
@ -10,6 +10,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/h2non/filetype"
|
||||||
|
|
||||||
"github.com/tardisx/netgiv/secure"
|
"github.com/tardisx/netgiv/secure"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,6 +19,14 @@ type Server struct {
|
|||||||
port int
|
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() {
|
func (s *Server) Run() {
|
||||||
address := fmt.Sprintf(":%d", s.port)
|
address := fmt.Sprintf(":%d", s.port)
|
||||||
networkAddress, _ := net.ResolveTCPAddr("tcp", address)
|
networkAddress, _ := net.ResolveTCPAddr("tcp", address)
|
||||||
@ -78,12 +88,22 @@ func handleConnection(conn *net.TCPConn) {
|
|||||||
}
|
}
|
||||||
log.Printf("send start looks like: %v", sendStart)
|
log.Printf("send start looks like: %v", sendStart)
|
||||||
file, err := os.CreateTemp("", "netgiv_")
|
file, err := os.CreateTemp("", "netgiv_")
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
ngf := NGF{
|
||||||
|
StorePath: file.Name(),
|
||||||
|
Filename: sendStart.Filename,
|
||||||
|
Kind: "",
|
||||||
|
Size: 0,
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("got error with temp file: %w", err)
|
log.Printf("got error with temp file: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("writing data to file: %s", file.Name())
|
log.Printf("writing data to file: %s", file.Name())
|
||||||
sendData := secure.PacketSendDataNext{}
|
sendData := secure.PacketSendDataNext{}
|
||||||
|
determinedKind := false
|
||||||
for {
|
for {
|
||||||
conn.SetDeadline(time.Now().Add(time.Second * 5))
|
conn.SetDeadline(time.Now().Add(time.Second * 5))
|
||||||
err = dec.Decode(&sendData)
|
err = dec.Decode(&sendData)
|
||||||
@ -95,8 +115,26 @@ func handleConnection(conn *net.TCPConn) {
|
|||||||
log.Printf("error decoding data next: %s", err)
|
log.Printf("error decoding data next: %s", err)
|
||||||
return
|
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)
|
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
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Printf("bad operation")
|
log.Printf("bad operation")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user