Add Media struct and UploadMediaFromMedia method
parent
3e91c76504
commit
315df7d916
48
mastodon.go
48
mastodon.go
|
@ -2,18 +2,14 @@
|
||||||
package mastodon
|
package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -58,52 +54,18 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if file, ok := params.(string); ok {
|
} else if media, ok := params.(*Media); ok {
|
||||||
f, err := os.Open(file)
|
r, contentType, err := media.bodyAndContentType()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
req, err = http.NewRequest(method, u.String(), r)
|
||||||
mw := multipart.NewWriter(&buf)
|
|
||||||
part, err := mw.CreateFormFile("file", filepath.Base(file))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(part, f)
|
|
||||||
if err != nil {
|
ct = contentType
|
||||||
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)
|
|
||||||
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()
|
||||||
|
|
84
status.go
84
status.go
|
@ -1,11 +1,15 @@
|
||||||
package mastodon
|
package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -70,6 +74,71 @@ type Conversation struct {
|
||||||
LastStatus *Status `json:"last_status"`
|
LastStatus *Status `json:"last_status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Media is struct to hold media.
|
||||||
|
type Media struct {
|
||||||
|
File io.Reader
|
||||||
|
Thumbnail io.Reader
|
||||||
|
Description string
|
||||||
|
Focus string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Media) bodyAndContentType() (io.Reader, string, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
mw := multipart.NewWriter(&buf)
|
||||||
|
|
||||||
|
fileName := "upload"
|
||||||
|
if f, ok := m.File.(*os.File); ok {
|
||||||
|
fileName = f.Name()
|
||||||
|
}
|
||||||
|
file, err := mw.CreateFormFile("file", fileName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(file, m.File); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Thumbnail != nil {
|
||||||
|
thumbName := "upload"
|
||||||
|
if f, ok := m.Thumbnail.(*os.File); ok {
|
||||||
|
thumbName = f.Name()
|
||||||
|
}
|
||||||
|
thumb, err := mw.CreateFormFile("thumbnail", thumbName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(thumb, m.Thumbnail); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Description != "" {
|
||||||
|
desc, err := mw.CreateFormField("description")
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(desc, strings.NewReader(m.Description)); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Focus != "" {
|
||||||
|
focus, err := mw.CreateFormField("focus")
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
if _, err := io.Copy(focus, strings.NewReader(m.Focus)); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := mw.Close(); err != nil {
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &buf, mw.FormDataContentType(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetFavourites return the favorite list of the current user.
|
// GetFavourites return the favorite list of the current user.
|
||||||
func (c *Client) GetFavourites(ctx context.Context, pg *Pagination) ([]*Status, error) {
|
func (c *Client) GetFavourites(ctx context.Context, pg *Pagination) ([]*Status, error) {
|
||||||
var statuses []*Status
|
var statuses []*Status
|
||||||
|
@ -287,19 +356,24 @@ func (c *Client) Search(ctx context.Context, q string, resolve bool) (*Results,
|
||||||
|
|
||||||
// UploadMedia upload a media attachment from a file.
|
// 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
|
f, err := os.Open(file)
|
||||||
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &attachment, nil
|
defer f.Close()
|
||||||
|
|
||||||
|
return c.UploadMediaFromMedia(ctx, &Media{File: f})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadMediaFromReader uploads a media attachment from a io.Reader.
|
// UploadMediaFromReader uploads a media attachment from a 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})
|
||||||
|
}
|
||||||
|
|
||||||
|
// UploadMediaFromMedia uploads a media attachment from a Media struct.
|
||||||
|
func (c *Client) UploadMediaFromMedia(ctx context.Context, media *Media) (*Attachment, error) {
|
||||||
var attachment Attachment
|
var attachment Attachment
|
||||||
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", reader, &attachment, nil)
|
if err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", media, &attachment, nil); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &attachment, nil
|
return &attachment, nil
|
||||||
|
|
Loading…
Reference in New Issue