-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (167 loc) · 4.24 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"fmt"
"html/template"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/user"
"path"
"runtime"
)
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width, initial-scale=0.41, maximum-scale=1" />
<title>Simple Upload</title>
<style type="text/css">
* {
color: white;
font-family: sans-serif;
padding: 0;
margin: 0;
cursor: pointer;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#upload {
width: 100%;
top: 0;
position: absolute;
background-color: #f44242;
height: 100%;
}
.text {
position: absolute;
top: 40%;
text-align: center;
width: 100%;
font-size: 3em;
}
input[type="file"] {
display: none;
}
</style>
</head>
<body>
<!-- <input type='file' id='file' /> -->
<form id="file-form" enctype="multipart/form-data" action="/upload" method="post">
<label>
<div id="upload">
<div class="text" id="uploadtext">UPLOAD<br>({{ . }}:8080)</div>
</div>
<input type="file" id='file' name="uploadfile" />
</label>
<input type="submit" value="upload" />
</form>
<script>
var uploadtext = document.getElementById('uploadtext');
var fileelt = document.getElementById('file');
document.body.ondragover = function() { uploadtext.innerHTML = 'DROP YOUR FILE HERE'; return false; };
document.body.ondrop = function(e) {
e.preventDefault();
readfiles(e.dataTransfer.files);
};
fileelt.addEventListener("change", function(e) {
var formData = new FormData(document.getElementById("file-form"));
var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload');
xhr.onload = function() { uploadtext.innerHTML = xhr.responseText; };
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
var complete = (event.loaded / event.total * 100 | 0);
var complete36 = (event.loaded / event.total * 36 | 0);
uploadtext.innerHTML = 'UPLOADING<br>PROGRESS ' + complete + '%';
}
};
xhr.send(formData);
});
</script>
</body>
</html>
`
var ip = ""
func upload(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.New("upload").Parse(html)
t.Execute(w, ip)
} else {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
log.Fatal(err)
return
}
for _, fheaders := range r.MultipartForm.File {
for _, handler := range fheaders {
// open uploaded
infile, err := handler.Open()
if err != nil {
log.Fatal(err)
return
}
// open destination
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
outfile, err := os.OpenFile(path.Join(usr.HomeDir, "Downloads", handler.Filename), os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
fmt.Fprintf(w, "ERROR")
return
}
// 32K buffer copy
if _, err = io.Copy(outfile, infile); nil != err {
log.Fatal(err)
return
}
}
}
fmt.Fprintf(w, "UPLOAD COMPLETE!")
}
}
// open opens the specified URL in the default browser of the user.
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func main() {
addrs, err := net.InterfaceAddrs()
if err != nil {
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
os.Exit(1)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
}
}
}
go open("http://" + ip + ":8080")
http.HandleFunc("/", upload)
err = http.ListenAndServe(":8080", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}