-
Notifications
You must be signed in to change notification settings - Fork 241
/
main.go
106 lines (89 loc) · 2.18 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
// Command upload is a chromedp example demonstrating how to upload a file on a
// form.
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/chromedp/chromedp"
)
func main() {
port := flag.Int("port", 8544, "port")
flag.Parse()
// get wd
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
filepath := wd + "/main.go"
// get some info about the file
fi, err := os.Stat(filepath)
if err != nil {
log.Fatal(err)
}
// start upload server
result := make(chan int, 1)
go uploadServer(fmt.Sprintf(":%d", *port), result)
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// run task list
var sz string
err = chromedp.Run(ctx, upload(fmt.Sprintf("http://localhost:%d", *port), filepath, &sz))
if err != nil {
log.Fatal(err)
}
log.Printf("original size: %d, upload size: %d", fi.Size(), <-result)
}
func upload(urlstr string, filepath string, sz *string) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.SendKeys(`input[name="upload"]`, filepath, chromedp.NodeVisible),
chromedp.Click(`input[name="submit"]`),
chromedp.Text(`#result`, sz, chromedp.ByID, chromedp.NodeVisible),
}
}
func uploadServer(addr string, result chan int) error {
// create http server and result channel
mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, uploadHTML)
})
mux.HandleFunc("/upload", func(res http.ResponseWriter, req *http.Request) {
f, _, err := req.FormFile("upload")
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(res, resultHTML, len(buf))
result <- len(buf)
})
return http.ListenAndServe(addr, mux)
}
const (
uploadHTML = `<!doctype html>
<html>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
<input name="upload" type="file"/>
<input name="submit" type="submit"/>
</form>
</body>
</html>`
resultHTML = `<!doctype html>
<html>
<body>
<div id="result">%d</div>
</body>
</html>`
)