-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
136 lines (114 loc) · 2.68 KB
/
tools.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
129
130
131
132
133
134
135
136
package main
import (
"archive/zip"
"io"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
)
var (
charPool = "abcdefghijklmnopqrstuvwxyz0123456789"
filenames []string
currChar byte
ind int
)
// Log with colors
func termLog(text string) {
start := time.Now()
now := start.Format("2008-03-09 15:04:05")
color.Cyan(now)
}
// Fix string to certain length
func fixString(myString string, length int) string {
for len(myString) < length {
myString += ":"
}
return myString
}
// Error handling can be tedius
func handleErr(err error, output string) {
if err != nil {
log.Fatal(err, output)
return
}
}
// Get local IP of current machine
func getThisIP() string {
connection, err := net.Dial("udp", "8.8.8.8:80")
handleErr(err, "")
defer connection.Close()
localAddr := connection.LocalAddr().(*net.UDPAddr)
ip := localAddr.IP
return ip.String() + ":"
}
// Get IP of machine sending the file
func getIP() string {
ip, err := net.ResolveTCPAddr("tcp", "192.168.1.100")
handleErr(err, "")
return ip.IP.String() + ":"
// return "192.168.1.100:"
}
// Wrapper to zipping files
func zipDir(path string) *os.File {
if string(path[len(path)-1]) == "/" {
path = path[0 : len(path)-1]
}
newZip, err := os.Create(path + ".zip")
handleErr(err, "")
path += "/"
writer := zip.NewWriter(newZip)
includeFiles(writer, path, "")
err = writer.Close()
handleErr(err, "")
return newZip
}
// Recursively add files to the zipped directory
func includeFiles(writer *zip.Writer, path, zipPath string) {
files, err := ioutil.ReadDir(path)
handleErr(err, "")
for _, file := range files {
if file.IsDir() {
currDir := file.Name() + "/"
newBase := path + currDir
includeFiles(writer, newBase, currDir)
} else {
chunk, err := ioutil.ReadFile(path + file.Name())
handleErr(err, "Cannot read file")
fileInZip, err := writer.Create(zipPath + file.Name())
handleErr(err, "")
_, err = fileInZip.Write(chunk)
handleErr(err, "")
}
}
}
// Unzip .zip file
func unzip(zipped, output string) {
reader, err := zip.OpenReader(zipped)
handleErr(err, "Unable to open zip")
for _, f := range reader.File {
rc, err := f.Open()
handleErr(err, "")
defer rc.Close()
fpath := filepath.Join(output, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, f.Mode())
} else {
var fdir string
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}
err = os.MkdirAll(fdir, f.Mode())
handleErr(err, "")
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
handleErr(err, "")
_, err = io.Copy(f, rc)
handleErr(err, "")
}
}
}