aboutsummaryrefslogtreecommitdiff
path: root/mastodon
diff options
context:
space:
mode:
authorr <r@freesoftwareextremist.com>2019-12-14 20:19:02 +0000
committerr <r@freesoftwareextremist.com>2019-12-14 20:19:02 +0000
commite129ea922e6cbe8966f9a9f0b1c6c94401516c61 (patch)
tree35be3bc003b787db5898ac1ba8aacca3f944105e /mastodon
parentea66bd539dec12ef2846c23e505593f9f9d9fac3 (diff)
downloadbloat-e129ea922e6cbe8966f9a9f0b1c6c94401516c61.tar.gz
bloat-e129ea922e6cbe8966f9a9f0b1c6c94401516c61.zip
Add attachments support
Diffstat (limited to 'mastodon')
-rw-r--r--mastodon/mastodon.go26
-rw-r--r--mastodon/status.go11
2 files changed, 37 insertions, 0 deletions
diff --git a/mastodon/mastodon.go b/mastodon/mastodon.go
index ff86d2b..22f5a53 100644
--- a/mastodon/mastodon.go
+++ b/mastodon/mastodon.go
@@ -83,6 +83,32 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
return err
}
ct = mw.FormDataContentType()
+ } else if file, ok := params.(*multipart.FileHeader); ok {
+ f, err := file.Open()
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ var buf bytes.Buffer
+ mw := multipart.NewWriter(&buf)
+ part, err := mw.CreateFormFile("file", filepath.Base(file.Filename))
+ if err != nil {
+ return err
+ }
+ _, err = io.Copy(part, f)
+ 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 if reader, ok := params.(io.Reader); ok {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
diff --git a/mastodon/status.go b/mastodon/status.go
index fd69914..b6110d5 100644
--- a/mastodon/status.go
+++ b/mastodon/status.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
+ "mime/multipart"
"net/http"
"net/url"
"time"
@@ -295,3 +296,13 @@ func (c *Client) UploadMediaFromReader(ctx context.Context, reader io.Reader) (*
}
return &attachment, nil
}
+
+// UploadMediaFromReader uploads a media attachment from a io.Reader.
+func (c *Client) UploadMediaFromMultipartFileHeader(ctx context.Context, fh *multipart.FileHeader) (*Attachment, error) {
+ var attachment Attachment
+ err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", fh, &attachment, nil)
+ if err != nil {
+ return nil, err
+ }
+ return &attachment, nil
+}