mirror of
https://gitea.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-13 08:19:20 +02:00
Cleanup file upload functions
This commit is contained in:
parent
81bdc7c705
commit
df031d5edd
|
@ -2,18 +2,14 @@
|
|||
package mastodon
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/tomnomnom/linkheader"
|
||||
|
@ -61,83 +57,6 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if file, ok := params.(string); ok {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
part, err := mw.CreateFormFile("file", filepath.Base(file))
|
||||
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 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)
|
||||
fname := filepath.Base(file.Filename)
|
||||
err = mw.WriteField("description", fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
part, err := mw.CreateFormFile("file", fname)
|
||||
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)
|
||||
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 if mr, ok := params.(*multipartRequest); ok {
|
||||
req, err = http.NewRequest(method, u.String(), mr.Data)
|
||||
if err != nil {
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package mastodon
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -293,30 +295,35 @@ func (c *Client) Search(ctx context.Context, q string, qType string, limit int,
|
|||
return &results, nil
|
||||
}
|
||||
|
||||
// UploadMedia upload a media attachment from a file.
|
||||
func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) {
|
||||
var attachment Attachment
|
||||
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// UploadMediaFromReader uploads a media attachment from a io.Reader.
|
||||
func (c *Client) UploadMediaFromMultipartFileHeader(ctx context.Context, fh *multipart.FileHeader) (*Attachment, error) {
|
||||
f, err := fh.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
fname := filepath.Base(fh.Filename)
|
||||
err = mw.WriteField("description", fname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
part, err := mw.CreateFormFile("file", fname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.Copy(part, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = mw.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
params := &multipartRequest{Data: &buf, ContentType: mw.FormDataContentType()}
|
||||
var attachment Attachment
|
||||
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", fh, &attachment, nil)
|
||||
err = c.doAPI(ctx, http.MethodPost, "/api/v1/media", params, &attachment, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue