Skip to content

Commit

Permalink
chore: fix url parsing, update tests (#2)
Browse files Browse the repository at this point in the history
* fix url parsing, update tests

* test ci

* update golang version
  • Loading branch information
tarik02 authored Jul 2, 2024
1 parent 98ff015 commit 911ba27
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ jobs:
strategy:
fail-fast: false
matrix:
go: ['1.13', '1.14']
go: ['1.22.4']
steps:
- uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}

- uses: actions/checkout@v2

- name: Build
run: go build -v .

- name: Test
env:
IMGURCLIENTID: ${{ secrets. IMGURCLIENTID }}
Expand Down
32 changes: 14 additions & 18 deletions fromURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ func matchesSlice(url string, validFormats []string) bool {
return false
}

func extractIdFromUrl(url string) string {
if end := strings.LastIndex(url, "?"); end != -1 {
url = url[:end]
}
start := strings.LastIndex(url, "/") + 1
if pos := strings.LastIndex(url, "-"); pos > start {
start = pos + 1
}
return url[start:]
}

// GetInfoFromURL tries to query imgur based on information identified in the URL.
// returns image/album info, status code of the request, error
func (client *Client) GetInfoFromURL(url string) (*GenericInfo, int, error) {
Expand Down Expand Up @@ -102,12 +113,7 @@ func (client *Client) directImageURL(url string) (*GenericInfo, int, error) {
func (client *Client) albumURL(url string) (*GenericInfo, int, error) {
var ret GenericInfo

start := strings.LastIndex(url, "/") + 1
end := strings.LastIndex(url, "?")
if end == -1 {
end = len(url)
}
id := url[start:end]
id := extractIdFromUrl(url)
if id == "" {
return nil, -1, errors.New("Could not find ID in URL " + url + ". I was going down imgur.com/a/ path.")
}
Expand All @@ -120,12 +126,7 @@ func (client *Client) albumURL(url string) (*GenericInfo, int, error) {
func (client *Client) galleryURL(url string) (*GenericInfo, int, error) {
var ret GenericInfo

start := strings.LastIndex(url, "/") + 1
end := strings.LastIndex(url, "?")
if end == -1 {
end = len(url)
}
id := url[start:end]
id := extractIdFromUrl(url)
if id == "" {
return nil, -1, errors.New("Could not find ID in URL " + url + ". I was going down imgur.com/gallery/ path.")
}
Expand All @@ -145,12 +146,7 @@ func (client *Client) galleryURL(url string) (*GenericInfo, int, error) {
func (client *Client) imageURL(url string) (*GenericInfo, int, error) {
var ret GenericInfo

start := strings.LastIndex(url, "/") + 1
end := strings.LastIndex(url, "?")
if end == -1 {
end = len(url)
}
id := url[start:end]
id := extractIdFromUrl(url)
if id == "" {
return nil, -1, errors.New("Could not find ID in URL " + url + ". I was going down imgur.com/ path.")
}
Expand Down
20 changes: 10 additions & 10 deletions fromURL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestGetFromURLAlbumSimulated(t *testing.T) {
defer server.Close()

client, _ := NewClient(httpC, "testing", "")
ge, status, err := client.GetInfoFromURL("https://imgur.com/a/VZQXk")
ge, status, err := client.GetInfoFromURL("https://imgur.com/a/gianluca-giminis-bikes-VZQXk")
if err != nil {
t.Errorf("GetInfoFromURL() failed with error: %v", err)
t.FailNow()
Expand Down Expand Up @@ -68,8 +68,8 @@ func TestGetFromURLAlbumReal(t *testing.T) {
}

TEST_URLS := []string{
"https://imgur.com/a/VZQXk",
"https://imgur.io/a/VZQXk",
"https://imgur.com/a/gianluca-giminis-bikes-VZQXk",
"https://imgur.io/a/gianluca-giminis-bikes-VZQXk",
}

for _, url := range TEST_URLS {
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestGetFromURLGAlbumSimulated(t *testing.T) {
defer server.Close()

client, _ := NewClient(httpC, "testing", "")
ge, status, err := client.GetInfoFromURL("https://imgur.com/gallery/VZQXk")
ge, status, err := client.GetInfoFromURL("https://imgur.com/gallery/as-turns-out-most-people-cannot-draw-bike-VZQXk")
if err != nil {
t.Errorf("GetInfoFromURL() failed with error: %v", err)
t.FailNow()
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestGetFromURLGAlbumReal(t *testing.T) {
expected map[string]interface{}
}{
{
galleryURL: "https://imgur.com/gallery/VZQXk",
galleryURL: "https://imgur.com/gallery/as-turns-out-most-people-cannot-draw-bike-VZQXk",
expected: map[string]interface{}{
"title": "As it turns out, most people cannot draw a bike.",
"cover": "CJCA0gW",
Expand All @@ -178,7 +178,7 @@ func TestGetFromURLGAlbumReal(t *testing.T) {
},
},
{
galleryURL: "https://imgur.com/gallery/t6l1GiW",
galleryURL: "https://imgur.com/gallery/funny-random-meme-twitter-dump-t6l1GiW",
expected: map[string]interface{}{
"title": "Funny Random Meme and Twitter Dump",
"cover": "60wTouU",
Expand All @@ -190,7 +190,7 @@ func TestGetFromURLGAlbumReal(t *testing.T) {
},
},
{
galleryURL: "https://imgur.io/gallery/VZQXk",
galleryURL: "https://imgur.io/gallery/as-turns-out-most-people-cannot-draw-bike-VZQXk",
expected: map[string]interface{}{
"title": "As it turns out, most people cannot draw a bike.",
"cover": "CJCA0gW",
Expand All @@ -202,7 +202,7 @@ func TestGetFromURLGAlbumReal(t *testing.T) {
},
},
{
galleryURL: "https://imgur.io/gallery/t6l1GiW",
galleryURL: "https://imgur.io/gallery/funny-random-meme-twitter-dump-t6l1GiW",
expected: map[string]interface{}{
"title": "Funny Random Meme and Twitter Dump",
"cover": "60wTouU",
Expand Down Expand Up @@ -292,8 +292,8 @@ func TestGetURLGalleryImageReal(t *testing.T) {
}

TEST_URLS := []string{
"https://imgur.com/gallery/uPI76jY",
"https://imgur.io/gallery/uPI76jY",
"https://imgur.com/gallery/abandoned-chinese-fishing-village-uPI76jY",
"https://imgur.io/gallery/abandoned-chinese-fishing-village-uPI76jY",
}

for _, url := range TEST_URLS {
Expand Down
36 changes: 20 additions & 16 deletions uploadImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
)

// UploadImage uploads the image to imgur
// image Can be a binary file, base64 data, or a URL for an image. (up to 10MB)
// album optional The id of the album you want to add the image to.
// For anonymous albums, album should be the deletehash that is returned at creation.
//
// For anonymous albums, album should be the deletehash that is returned at creation.
//
// dtype The type of the file that's being sent; file, base64 or URL
// title optional The title of the image.
// description optional The description of the image.
Expand All @@ -28,17 +30,20 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
return nil, -1, errors.New("Passed invalid dtype: " + dtype + ". Please use file/base64/URL.")
}

form := createUploadForm(image, album, dtype, title, description)
reqbody := &bytes.Buffer{}
writer := multipart.NewWriter(reqbody)
createUploadForm(writer, image, album, dtype, title, description)
writer.Close()

URL := client.createAPIURL("image")
req, err := http.NewRequest("POST", URL, bytes.NewBufferString(form.Encode()))
req, err := http.NewRequest("POST", URL, reqbody)
client.Log.Debugf("Posting to URL %v\n", URL)
if err != nil {
return nil, -1, errors.New("Could create request for " + URL + " - " + err.Error())
}

req.Header.Add("Authorization", "Client-ID "+client.imgurAccount.clientID)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Type", writer.FormDataContentType())
if client.rapidAPIKey != "" {
req.Header.Add("X-RapidAPI-Key", client.rapidAPIKey)
}
Expand All @@ -50,7 +55,7 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
defer res.Body.Close()

// Read the whole body
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, -1, errors.New("Problem reading the body of " + URL + " - " + err.Error())
}
Expand All @@ -72,23 +77,22 @@ func (client *Client) UploadImage(image []byte, album string, dtype string, titl
return img.Ii, img.Status, nil
}

func createUploadForm(image []byte, album string, dtype string, title string, description string) url.Values {
form := url.Values{}
func createUploadForm(writer *multipart.Writer, image []byte, album string, dtype string, title string, description string) {
part, _ := writer.CreateFormFile("image", "image")
_, _ = part.Write(image)

form.Add("image", string(image[:]))
form.Add("type", dtype)
_ = writer.WriteField("image", string(image[:]))
_ = writer.WriteField("type", dtype)

if album != "" {
form.Add("album", album)
_ = writer.WriteField("album", album)
}
if title != "" {
form.Add("title", title)
_ = writer.WriteField("title", title)
}
if description != "" {
form.Add("description", description)
_ = writer.WriteField("description", description)
}

return form
}

// UploadImageFromFile uploads a file given by the filename string to imgur.
Expand Down

0 comments on commit 911ba27

Please # to comment.