From e5c082de35cc4cb5a0837572ccce5578c64f32e9 Mon Sep 17 00:00:00 2001 From: Darren O'Connor Date: Tue, 15 Nov 2022 02:54:56 +0000 Subject: [PATCH] Add UploadMediaFromBytes function --- status.go | 7 ++++++- status_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/status.go b/status.go index 1e8f590..e616366 100644 --- a/status.go +++ b/status.go @@ -409,7 +409,12 @@ func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, err 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) { return c.UploadMediaFromMedia(ctx, &Media{File: reader}) } diff --git a/status_test.go b/status_test.go index 0a5231f..617a687 100644 --- a/status_test.go +++ b/status_test.go @@ -708,6 +708,18 @@ func TestUploadMedia(t *testing.T) { if writerAttachment.ID != "123" { 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) {