Add UploadMediaFromBytes function

pull/155/head
Darren O'Connor 2022-11-15 02:54:56 +00:00 committed by mattn
parent 309dce6ff3
commit e5c082de35
2 changed files with 18 additions and 1 deletions

View File

@ -409,7 +409,12 @@ func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, err
return c.UploadMediaFromMedia(ctx, &Media{File: f}) return c.UploadMediaFromMedia(ctx, &Media{File: f})
} }
// UploadMediaFromReader uploads a media attachment from a io.Reader. // UploadMediaFromBytes uploads a media attachment from a byte slice.
func (c *Client) UploadMediaFromBytes(ctx context.Context, b []byte) (*Attachment, error) {
return c.UploadMediaFromReader(ctx, bytes.NewReader(b))
}
// UploadMediaFromReader uploads a media attachment from an io.Reader.
func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (*Attachment, error) { func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (*Attachment, error) {
return c.UploadMediaFromMedia(ctx, &Media{File: reader}) return c.UploadMediaFromMedia(ctx, &Media{File: reader})
} }

View File

@ -708,6 +708,18 @@ func TestUploadMedia(t *testing.T) {
if writerAttachment.ID != "123" { if writerAttachment.ID != "123" {
t.Fatalf("want %q but %q", "123", attachment.ID) t.Fatalf("want %q but %q", "123", attachment.ID)
} }
bytes, err := os.ReadFile("testdata/logo.png")
if err != nil {
t.Fatalf("could not open file: %v", err)
}
byteAttachment, err := client.UploadMediaFromBytes(context.Background(), bytes)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if byteAttachment.ID != "123" {
t.Fatalf("want %q but got %q", "123", attachment.ID)
}
} }
func TestGetConversations(t *testing.T) { func TestGetConversations(t *testing.T) {