-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.go
128 lines (112 loc) · 2.75 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"strconv"
)
// BUFFERSIZE is 131,072 bytes of buffer data
const (
BUFFERSIZE = 131072
PORT = "9999"
)
var (
currDir string
currFile string
)
// Main function for send.go
func send(directory, fileName string) {
if fileName != "" {
path := directory + "/" + fileName
if beginServer(path) {
fmt.Println("Sent")
} else {
fmt.Println("Failed to send")
}
} else {
fmt.Println("Please specify filename")
}
}
// Begin server for tcp connection
func beginServer(path string) bool {
ip := getThisIP()
listener, err := net.Listen("tcp", ip+PORT)
handleErr(err, "Could not begin server")
log.Println("Server started")
// Wait for connection
for {
connection, err := listener.Accept()
handleErr(err, "Could not connect to server")
log.Println("Server connected")
fsInfo, err := os.Stat(path)
handleErr(err, "")
// Handling files and directories
mode := fsInfo.Mode()
if mode.IsDir() {
log.Println("dir")
handleErr(err, "Could not open directory")
newZip := zipDir(fsInfo.Name())
handleFile(newZip, connection)
} else if mode.IsRegular() {
log.Println("file")
currFile, err := os.Open(path)
handleErr(err, "Could not open file")
log.Println(currFile.Name())
handleFile(currFile, connection)
}
}
}
// Sending a directory
func handleDirectory(directory *os.File, connection net.Conn) {
defer directory.Close()
fi, err := directory.Readdir(-1)
handleErr(err, "")
directoryInfo(directory.Name(), connection)
for _, fi := range fi {
if fi.Mode().IsRegular() {
currFile, err := os.Open(fi.Name())
fmt.Println(fi.Name(), fi.Size(), "bytes")
handleErr(err, "")
handleFile(currFile, connection)
} else {
currDir, err := os.Open(fi.Name() + "/")
handleErr(err, "")
handleDirectory(currDir, connection)
}
}
}
func directoryInfo(name string, connection net.Conn) {
dirName := fixString(name, 64)
dirSize := fixString("", 10)
connection.Write([]byte(dirSize))
connection.Write([]byte(dirName))
}
// Handle one file
func handleFile(file *os.File, connection net.Conn) {
fileInfo, err := file.Stat()
handleErr(err, "")
fileSize := strconv.FormatInt(fileInfo.Size(), 10)
fileName := fileInfo.Name()
log.Println("Beginning transfer")
log.Println("FileName:\t\t", fileName)
log.Println("FileSize:\t\t", fileSize, " bytes")
fileSize = fixString(fileSize, 10)
fileName = fixString(fileName, 64)
connection.Write([]byte(fileSize))
connection.Write([]byte(fileName))
transfer(file, connection)
}
// Sending file in chunks
func transfer(file *os.File, connection net.Conn) {
currBuffer := make([]byte, BUFFERSIZE)
for {
_, err := file.Read(currBuffer)
if err == io.EOF {
return
}
handleErr(err, "")
connection.Write(currBuffer)
}
}