Added UploadMediaFromReader

This commit is contained in:
Brian C. Lindner 2019-03-10 22:55:15 -04:00 committed by mattn
parent efa05aa949
commit e804ee7eb2
3 changed files with 42 additions and 1 deletions

View File

@ -83,6 +83,26 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
return err return err
} }
ct = mw.FormDataContentType() ct = mw.FormDataContentType()
} else if reader, ok := params.(io.Reader); ok {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
part, err := mw.CreateFormFile("file", "upload")
if err != nil {
return err
}
_, err = io.Copy(part, reader)
if err != nil {
return err
}
err = mw.Close()
if err != nil {
return err
}
req, err = http.NewRequest(method, u.String(), &buf)
if err != nil {
return err
}
ct = mw.FormDataContentType()
} else { } else {
if method == http.MethodGet && pg != nil { if method == http.MethodGet && pg != nil {
u.RawQuery = pg.toValues().Encode() u.RawQuery = pg.toValues().Encode()

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
"io"
) )
// Status is struct to hold status. // Status is struct to hold status.
@ -265,7 +266,7 @@ func (c *Client) Search(ctx context.Context, q string, resolve bool) (*Results,
return &results, nil return &results, nil
} }
// UploadMedia upload a media attachment. // UploadMedia upload a media attachment from a file.
func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) { func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) {
var attachment Attachment var attachment Attachment
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment, nil) err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment, nil)
@ -274,3 +275,13 @@ func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, err
} }
return &attachment, nil return &attachment, nil
} }
// UploadMediaFromReader uploads a media attachment from a io.Reader.
func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (*Attachment, error) {
var attachment Attachment
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", reader, &attachment, nil)
if err != nil {
return nil, err
}
return &attachment, nil
}

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"os"
) )
func TestGetFavourites(t *testing.T) { func TestGetFavourites(t *testing.T) {
@ -549,4 +550,13 @@ func TestUploadMedia(t *testing.T) {
if attachment.ID != "123" { if attachment.ID != "123" {
t.Fatalf("want %q but %q", "123", attachment.ID) t.Fatalf("want %q but %q", "123", attachment.ID)
} }
file, err := os.Open("testdata/logo.png")
defer file.Close()
writerAttachment, err := client.UploadMediaFromReader(context.Background(), file)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if writerAttachment.ID != "123" {
t.Fatalf("want %q but %q", "123", attachment.ID)
}
} }