-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileupload.go
127 lines (105 loc) · 2.65 KB
/
fileupload.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
package main
import (
"bytes"
"fmt"
"image"
"image/png"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/kbinani/screenshot"
)
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
// Creates a new file upload http request with optional extra params
func newfileUploadRequestRawImage(uri string, params map[string]string, paramName string, image image.Image) (*http.Request, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, "shot.png")
if err != nil {
return nil, err
}
err = png.Encode(part, image)
if err != nil {
return nil, err
}
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", uri, body)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, err
}
func takeShot() *image.RGBA {
// Capture each displays.
n := screenshot.NumActiveDisplays()
if n <= 0 {
panic("Active display not found")
}
var all image.Rectangle = image.Rect(0, 0, 0, 0)
for i := 0; i < n; i++ {
bounds := screenshot.GetDisplayBounds(i)
all = bounds.Union(all)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
panic(err)
}
fileName := fmt.Sprintf("%d_%dx%d.png", i, bounds.Dx(), bounds.Dy())
save(img, fileName)
fmt.Printf("#%d : %v \"%s\"\n", i, bounds, fileName)
}
// Capture all desktop region into an image.
fmt.Printf("%v\n", all)
img, err := screenshot.Capture(all.Min.X, all.Min.Y, all.Dx(), all.Dy())
if err != nil {
panic(err)
}
//save(img, "all.png")
return img
}
func takeShotAndUpload(username string) {
extraParams := map[string]string{
"id": username,
}
img := takeShot()
req, err := newfileUploadRequestRawImage("http://"+ROOT+"/api/screenshots", extraParams, "image", img)
if err != nil {
log.Println(err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
fmt.Println(resp.Status)
}