Skip to content

Commit

Permalink
Merge pull request #8 from Tomohiro/use-go-gyazo-package
Browse files Browse the repository at this point in the history
Use go-gyazo package when access to the Gyazo API
  • Loading branch information
tomohiro committed Oct 22, 2015
2 parents af5b5ed + c3c2818 commit 4bf986a
Showing 1 changed file with 54 additions and 71 deletions.
125 changes: 54 additions & 71 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 4bf986a

Please sign in to comment.