-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgur.go
executable file
·89 lines (73 loc) · 1.57 KB
/
imgur.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"os/exec"
)
type JsonResponse struct {
Data Data
}
type Data struct {
Link string
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <path/to/image.(png|jpg)>\n", os.Args[0])
os.Exit(1)
}
buffer := new(bytes.Buffer)
form := multipart.NewWriter(buffer)
formFile, err := form.CreateFormFile("image", os.Args[1])
if err != nil {
log.Fatal(err)
}
localFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(formFile, localFile)
if err != nil {
log.Fatal(err)
}
form.Close()
localFile.Close()
request, err := http.NewRequest("POST", "https://api.imgur.com/3/upload.json", buffer)
if err != nil {
log.Fatal(err)
}
request.Header.Set("Content-Type", form.FormDataContentType())
request.Header.Set("Authorization", "Client-ID c3d5102cafbba4c")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
var jsonResponse JsonResponse
if err = json.NewDecoder(response.Body).Decode(&jsonResponse); err != nil {
log.Fatal(err)
}
if jsonResponse.Data.Link == "" {
exec.Command("notify-send", "Upload error").Run()
os.Exit(1)
}
response.Body.Close()
cmd := exec.Command("xclip", "-selection", "c")
cmdPipe, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
fmt.Fprint(cmdPipe, jsonResponse.Data.Link)
cmdPipe.Close()
cmd.Wait()
if err != nil {
fmt.Print(jsonResponse.Data.Link)
}
exec.Command("notify-send", "Upload complete").Run()
}