-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
260 lines (219 loc) · 5.23 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"time"
"github.com/mitchellh/go-homedir"
"github.com/skratchdot/open-golang/open"
"github.com/tomohiro/go-gyazo/gyazo"
"github.com/urfave/cli/v2"
)
const (
// ExitCodeOK represents succeed exit status.
ExitCodeOK int = 0
// ExitCodeError represents failed exit status.
ExitCodeError int = 1
)
var (
// exitCode to terminate.
exitCode = ExitCodeOK
// Default endpoint.
endpoint = "http://upload.gyazo.com/upload.cgi"
)
func main() {
os.Exit(realMain())
}
func realMain() int {
app := &cli.App{
Name: "gyazo-cli",
Usage: "Gyazo command-line uploader",
UsageText: "gyazo-cli [global options] [PATH]",
Action: upload,
Version: fmt.Sprintf("%s (rev %s) [%s %s %s]", Version, revision, runtime.GOOS, runtime.GOARCH, runtime.Version()),
Authors: []*cli.Author{
&cli.Author{
Name: "Tomohiro Taira",
Email: "tomohiro.t@gmail.com",
},
},
Copyright: "© 2019 Tomohiro Taira",
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
exitCode = ExitCodeError
}
return exitCode
}
// Set the endpoint of self hosting Gyazo server URL.
func init() {
if os.Getenv("GYAZO_SERVER_URL") != "" {
endpoint = os.Getenv("GYAZO_SERVER_URL")
}
}
// Upload a new image to a Gyazo from the specified local image file.
func upload(c *cli.Context) error {
var filename string
var url string
var err error
filename = c.Args().First()
if filename == "" {
filename, err = takeScreenshot()
if err != nil {
fmt.Fprint(os.Stderr, "Failed to take a screenshot\n")
return err
}
}
if !supportedMimetype(filename) {
fmt.Fprint(os.Stderr, "Failed to upload: unsupported file type\n")
return err
}
// Open and load the content from an image.
content, err := os.Open(filename)
if err != nil {
return err
}
defer content.Close()
if os.Getenv("GYAZO_ACCESS_TOKEN") != "" {
gyazo, _ := gyazo.NewClient(os.Getenv("GYAZO_ACCESS_TOKEN"))
image, err := gyazo.Upload(content)
if err != nil {
fmt.Fprint(os.Stderr, "Failed to upload via API request\n")
return err
}
url = image.PermalinkURL
} else {
// Create multipart/form-data
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("imagedata", filename)
if err != nil {
return err
}
if _, err = io.Copy(part, content); err != nil {
return err
}
writer.WriteField("id", gyazoID())
err = writer.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create multipart/data: %s\n", err)
return err
}
res, err := http.Post(endpoint, writer.FormDataContentType(), body)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to upload: %s\n", err)
return err
}
defer res.Body.Close()
url, err = imageURL(res)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid response: %s\n", err)
return err
}
}
fmt.Println(url)
err = open.Run(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open by default browser: %s\n", err)
}
return err
}
// takeScreenshot takes a screenshot and then returns it file path.
func takeScreenshot() (string, error) {
var err error
path := fmt.Sprintf("/tmp/image_upload%d.png", os.Getpid())
switch runtime.GOOS {
case "darwin":
err = exec.Command("screencapture", "-i", path).Run()
case "linux":
err = exec.Command("import", path).Run()
case "windows":
err = errors.New("unsupported os")
}
return path, err
}
// supportedMimetype returns result of checked mimetype.
func supportedMimetype(f string) bool {
t := mime.TypeByExtension(filepath.Ext(f))
res, _ := regexp.MatchString("image", t)
return res
}
// imageURL returns url of uploaded image.
func imageURL(r *http.Response) (string, error) {
var url string
id := r.Header.Get("X-Gyazo-Id")
if err := storeGyazoID(id); err != nil {
fmt.Fprintf(os.Stderr, "Failed to store Gyazo ID: %s\n", err)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return url, err
}
url = string(body)
return url, err
}
// gyazoID returns Gyazo ID from stored file.
func gyazoID() string {
var id string
body, err := ioutil.ReadFile(gyazoIDPath())
if err != nil {
return id
}
id = string(body)
return id
}
// storeGyazoID stores Gyazo ID to file.
func storeGyazoID(id string) error {
var err error
if id == "" {
return err
}
path := gyazoIDPath()
dir := filepath.Dir(path)
_, err = os.Stat(dir)
if err != nil {
err = os.Mkdir(dir, 0755)
if err != nil {
return err
}
}
_, err = os.Stat(path)
if err == nil {
newpath := fmt.Sprintf("%s_%s.bak", id, time.Now().Format("20060102150405"))
err = os.Rename(path, newpath)
if err != nil {
return err
}
}
buf := bytes.NewBufferString(id)
err = ioutil.WriteFile(path, buf.Bytes(), 0644)
if err != nil {
return err
}
return err
}
// gyazoIDPath returns path of Gyazo ID file on local filesystem.
func gyazoIDPath() string {
homedir, _ := homedir.Dir()
var path string
switch runtime.GOOS {
case "darwin":
path = fmt.Sprintf("%s/Library/Gyazo/id", homedir)
case "linux":
path = fmt.Sprintf("%s/.gyazo.id", homedir)
case "windows":
path = fmt.Sprintf("%s\\Gyazo\\id.txt", os.Getenv("APPDATA"))
}
return path
}