diff --git a/examples/upload/.gitignore b/examples/upload/.gitignore new file mode 100644 index 0000000..fb68e0c --- /dev/null +++ b/examples/upload/.gitignore @@ -0,0 +1 @@ +upload diff --git a/examples/upload/main.go b/examples/upload/main.go new file mode 100644 index 0000000..e042ecf --- /dev/null +++ b/examples/upload/main.go @@ -0,0 +1,130 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + + cdp "github.com/knq/chromedp" + cdptypes "github.com/knq/chromedp/cdp" + "github.com/knq/chromedp/cdp/dom" +) + +var ( + flagPort = flag.Int("port", 8544, "port") +) + +func main() { + var err error + + flag.Parse() + + // create http server and result channel + result := make(chan int, 1) + 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 := ioutil.ReadAll(f) + if err != nil { + http.Error(res, err.Error(), http.StatusBadRequest) + return + } + + fmt.Fprintf(res, resultHTML, len(buf)) + + result <- len(buf) + }) + + go http.ListenAndServe(fmt.Sprintf(":%d", *flagPort), mux) + + // create context + ctxt, cancel := context.WithCancel(context.Background()) + defer cancel() + + // create chrome instance + c, err := cdp.New(ctxt, cdp.WithLog(log.Printf)) + if err != nil { + log.Fatal(err) + } + + // 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) + } + + // run task list + var sz string + err = c.Run(ctxt, upload(filepath, &sz)) + if err != nil { + log.Fatal(err) + } + + // shutdown chrome + err = c.Shutdown(ctxt) + if err != nil { + log.Fatal(err) + } + + // wait for chrome to finish + err = c.Wait() + if err != nil { + log.Fatal(err) + } + + log.Printf("original size: %d, upload size: %d", fi.Size(), <-result) +} + +func upload(filepath string, sz *string) cdp.Tasks { + var ids []cdptypes.NodeID + return cdp.Tasks{ + cdp.Navigate(fmt.Sprintf("http://localhost:%d", *flagPort)), + cdp.WaitVisible(`input[name="upload"]`), + cdp.NodeIDs(`input[name="upload"]`, &ids, cdp.NodeVisible), + cdp.ActionFunc(func(ctxt context.Context, h cdptypes.Handler) error { + return dom.SetFileInputFiles(ids[0], []string{filepath}).Do(ctxt, h) + }), + cdp.Click(`input[name="submit"]`), + cdp.Text(`#result`, sz, cdp.ByID, cdp.NodeVisible), + } +} + +const ( + uploadHTML = ` + + +
+ + +
+ +` + + resultHTML = ` + + +
%d
+ +` +)