-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (76 loc) · 1.76 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
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"os/exec"
"strings"
)
const ApiEndpoint = "https://0x0.st"
func main() {
cmd := exec.Command("wl-paste")
out, _ := cmd.Output()
b := bytes.NewBuffer([]byte(""))
if _, err := b.Write(out); err != nil {
notify(err.Error(), true)
}
file, err := Upload(b)
if err != nil {
notify(err.Error(), true)
}
if err = exec.Command("wl-copy", file).Run(); err != nil {
notify(err.Error(), true)
}
notify("Clipboard uploaded\n"+file, true)
}
// Upload takes a file and uploads that file to a file host.
// It returns the url to the uploaded file as a string and any error encountered.
func Upload(file *bytes.Buffer) (string, error) {
var err error
var result string
result, err = UploadToHost(ApiEndpoint, file)
if err != nil {
return "", err
}
return result, nil
}
//goland:noinspection ALL
func UploadToHost(url string, file *bytes.Buffer) (string, error) {
var (
err error
client http.Client
)
writer := multipart.NewWriter(file)
form, err := writer.CreateFormFile("file", "file")
if err != nil {
return "", err
}
form.Write(file.Bytes())
writer.Close()
req, err := http.NewRequest("POST", url, file)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
// Submit the request
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)
return strings.Replace(bodyString, "\n", "", -1), nil
}
return "", fmt.Errorf("bad status: %s", resp.Status)
}
func notify(msg string, exit bool) {
_ = exec.Command("notify-send", "wl-uploader", msg).Run()
if exit {
os.Exit(1)
}
}