Skip to content

Commit

Permalink
Simplified image upload code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreitbart committed Apr 25, 2016
1 parent 9e251bc commit dc953d4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
10 changes: 5 additions & 5 deletions serverError_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestImgurNotSuccess(t *testing.T) {
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")
_, _, err = client.UploadImage(im, "", "file", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestJsonError(t *testing.T) {
}

var im []byte
img, _, err = client.UploadImage(im, "", "binary", "t", "d")
img, _, err = client.UploadImage(im, "", "file", "t", "d")

if err == nil || img != nil {
t.Error("UploadImage() should have failed, but didn't")
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestServerError(t *testing.T) {
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")
_, _, err = client.UploadImage(im, "", "file", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestImgurError(t *testing.T) {
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")
_, _, err = client.UploadImage(im, "", "file", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestServerDown(t *testing.T) {
}

var im []byte
_, _, err = client.UploadImage(im, "", "binary", "t", "d")
_, _, err = client.UploadImage(im, "", "file", "t", "d")

if err == nil {
t.Error("UploadImage() should have failed, but didn't")
Expand Down
20 changes: 5 additions & 15 deletions uploadImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
if image == nil {
return nil, -1, errors.New("Invalid image.")
}
if dtype != "binary" && dtype != "base64" && dtype != "URL" {
return nil, -1, errors.New("Passed invalid dtype: " + dtype + ". Please use binary/base64/URL.")
if dtype != "file" && dtype != "base64" && dtype != "URL" {
return nil, -1, errors.New("Passed invalid dtype: " + dtype + ". Please use file/base64/URL.")
}

form := createUploadForm(image, album, dtype, title, description)
Expand Down Expand Up @@ -77,18 +77,8 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
func createUploadForm(image []byte, album string, dtype string, title string, description string) url.Values {
form := url.Values{}

if dtype == "binary" {
form.Add("image", string(image[:]))
form.Add("type", "file")
}
if dtype == "base64" {
form.Add("image", string(image[:]))
form.Add("type", "base64")
}
if dtype == "URL" {
form.Add("image", string(image[:]))
form.Add("type", "URL")
}
form.Add("image", string(image[:]))
form.Add("type", dtype)

if album != "" {
form.Add("album", album)
Expand Down Expand Up @@ -124,5 +114,5 @@ func (client *Client) UploadImageFromFile(filename string, album string, title s

//base := base64.StdEncoding.EncodeToString(b)

return client.UploadImage(b, album, "binary", title, description)
return client.UploadImage(b, album, "file", title, description)
}

0 comments on commit dc953d4

Please # to comment.