diff --git a/main.go b/main.go index 3031b3b..03731fe 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "encoding/json" "errors" "fmt" "io" @@ -17,6 +16,7 @@ import ( "runtime" "time" + "github.com/Tomohiro/go-gyazo/gyazo" "github.com/codegangsta/cli" "github.com/mitchellh/go-homedir" "github.com/skratchdot/open-golang/open" @@ -47,31 +47,17 @@ func realMain() int { return exitCode } -// Set the endpoint of Gyazo API. +// Set the endpoint of self hosting Gyazo server URL. func init() { if os.Getenv("GYAZO_SERVER_URL") != "" { endpoint = os.Getenv("GYAZO_SERVER_URL") - } else if os.Getenv("GYAZO_ACCESS_TOKEN") != "" { - endpoint = "https://upload.gyazo.com/api/upload" } } -// Image represents a uploaded image on the Gyazo server. -// -// Gyazo API docs: https://gyazo.com/api/docs/image -type Image struct { - ID string `json:"image_id"` - PermalinkURL string `json:"permalink_url"` - ThumbURL string `json:"thumb_url"` - URL string `json:"url"` - Type string `json:"type"` -} - -// Upload a new image to a Gyazo server from the specified local image file. -// -// Gyazo API docs: https://gyazo.com/api/docs/image +// Upload a new image to a Gyazo from the specified local image file. func upload(c *cli.Context) { var filename string + var url string var err error filename = c.Args().First() @@ -99,47 +85,53 @@ func upload(c *cli.Context) { } defer content.Close() - // Create multipart/form-data - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) + if os.Getenv("GYAZO_ACCESS_TOKEN") != "" { + gyazo, _ := gyazo.NewClient(os.Getenv("GYAZO_ACCESS_TOKEN")) + image, err := gyazo.Upload(content) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to upload: %s\n", err) + exitCode = 1 + return + } + url = image.PermalinkURL + } else { + // Create multipart/form-data + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("imagedata", filename) - if err != nil { - exitCode = 1 - return - } + part, err := writer.CreateFormFile("imagedata", filename) + if err != nil { + exitCode = 1 + return + } - if _, err = io.Copy(part, content); err != nil { - exitCode = 1 - return - } + if _, err = io.Copy(part, content); err != nil { + exitCode = 1 + return + } - if os.Getenv("GYAZO_ACCESS_TOKEN") != "" { - writer.WriteField("access_token", os.Getenv("GYAZO_ACCESS_TOKEN")) - } else { writer.WriteField("id", gyazoID()) - } - - err = writer.Close() - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to create multipart/data: %s\n", err) - exitCode = 1 - return - } + err = writer.Close() + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to create multipart/data: %s\n", err) + exitCode = 1 + return + } - res, err := http.Post(endpoint, writer.FormDataContentType(), body) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to upload: %s\n", err) - exitCode = 1 - return - } - defer res.Body.Close() + res, err := http.Post(endpoint, writer.FormDataContentType(), body) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to upload: %s\n", err) + exitCode = 1 + return + } + defer res.Body.Close() - url, err := imageURL(res) - if err != nil { - fmt.Fprintf(os.Stderr, "Response error: %s\n", err) - exitCode = 1 - return + url, err = imageURL(res) + if err != nil { + fmt.Fprintf(os.Stderr, "Response error: %s\n", err) + exitCode = 1 + return + } } fmt.Println(url) @@ -176,28 +168,19 @@ func supportedMimetype(f string) bool { // imageURL returns url of uploaded image. func imageURL(r *http.Response) (string, error) { var url string - var err error - if os.Getenv("GYAZO_ACCESS_TOKEN") != "" { - image := Image{} - if err = json.NewDecoder(r.Body).Decode(&image); err != nil { - return url, err - } - url = image.PermalinkURL - } else { - 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 - } + 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) + } - url = string(body) + body, err := ioutil.ReadAll(r.Body) + if err != nil { + return url, err } + url = string(body) + return url, err }