forked from im2nguyen/rover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.go
95 lines (82 loc) · 2.66 KB
/
screenshot.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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/chromedp/cdproto/browser"
"github.com/chromedp/chromedp"
)
// Heavily inspired by: https://github.com/chromedp/examples/blob/master/download_file/main.go
func screenshot(s *http.Server) {
// ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithDebugf(log.Printf))
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// create a timeout as a safety net to prevent any infinite wait loops
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
defer cancel()
url := fmt.Sprintf("http://%s", s.Addr)
// this will be used to capture the file name later
var downloadGUID string
downloadComplete := make(chan bool)
chromedp.ListenTarget(ctx, func(v interface{}) {
if ev, ok := v.(*browser.EventDownloadProgress); ok {
if ev.State == browser.DownloadProgressStateCompleted {
downloadGUID = ev.GUID
close(downloadComplete)
}
}
})
if err := chromedp.Run(ctx, chromedp.Tasks{
browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).
WithDownloadPath(os.TempDir()).
WithEventsEnabled(true),
chromedp.Navigate(url),
// wait for graph to be visible
chromedp.WaitVisible(`#cytoscape-div`),
// find and click "Save Graph" button
chromedp.Click(`#saveGraph`, chromedp.NodeVisible),
}); err != nil && !strings.Contains(err.Error(), "net::ERR_ABORTED") {
// Note: Ignoring the net::ERR_ABORTED page error is essential here since downloads
// will cause this error to be emitted, although the download will still succeed.
log.Fatal(err)
}
<-downloadComplete
e := moveFile(fmt.Sprintf("%v/%v", os.TempDir(), downloadGUID), "./rover.svg")
if e != nil {
log.Fatal(e)
}
log.Println("Image generation complete.")
// Shutdown http server
s.Shutdown(context.Background())
}
// This function resolves the "invalid cross-device link" error for moving files
// between volumes for Docker.
// https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b
func moveFile(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("Couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("Couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("Writing to output file failed: %s", err)
}
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("Failed removing original file: %s", err)
}
return nil
}