checkpoint: can now create media attachments
This commit is contained in:
parent
cb7ac4175d
commit
41770e2b59
3 changed files with 113 additions and 11 deletions
|
@ -1,8 +1,13 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
||||||
)
|
)
|
||||||
|
@ -31,12 +36,86 @@ func (g *Client) GetMediaAttachment(mediaAttachmentID string) (model.Attachment,
|
||||||
return attachment, nil
|
return attachment, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//type CreateMediaAttachmentForm struct {
|
func (g *Client) CreateMediaAttachment(path, description, focus string) (model.Attachment, error) {
|
||||||
// Description string
|
file, err := os.Open(path)
|
||||||
// Focus string
|
if err != nil {
|
||||||
// Filepath string
|
return model.Attachment{}, fmt.Errorf("unable to open the file: %w", err)
|
||||||
//}
|
}
|
||||||
//
|
defer file.Close()
|
||||||
//func (g *Client) CreateMediaAttachment(form CreateMediaAttachmentForm) (model.Attachment, error) {
|
|
||||||
// return model.Attachment{}, nil
|
// create the request body using a writer from the multipart package
|
||||||
//}
|
requestBody := bytes.Buffer{}
|
||||||
|
requestBodyWriter := multipart.NewWriter(&requestBody)
|
||||||
|
|
||||||
|
filename := filepath.Base(path)
|
||||||
|
|
||||||
|
part, err := requestBodyWriter.CreateFormFile("file", filename)
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to create the new part: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(part, file); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to copy the file contents to the form: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the description
|
||||||
|
if description != "" {
|
||||||
|
descriptionFormFieldWriter, err := requestBodyWriter.CreateFormField("description")
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to create the writer for the 'description' form field: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.WriteString(descriptionFormFieldWriter, description); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to write the description to the form: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the focus values
|
||||||
|
if focus != "" {
|
||||||
|
focusFormFieldWriter, err := requestBodyWriter.CreateFormField("focus")
|
||||||
|
if err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to create the writer for the 'focus' form field: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.WriteString(focusFormFieldWriter, focus); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"unable to write the focus values to the form: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := requestBodyWriter.Close(); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf("unable to close the writer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := g.Authentication.Instance + baseMediaPath
|
||||||
|
|
||||||
|
var attachment model.Attachment
|
||||||
|
|
||||||
|
params := requestParameters{
|
||||||
|
httpMethod: http.MethodPost,
|
||||||
|
url: url,
|
||||||
|
requestBody: &requestBody,
|
||||||
|
contentType: requestBodyWriter.FormDataContentType(),
|
||||||
|
output: &attachment,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := g.sendRequest(params); err != nil {
|
||||||
|
return model.Attachment{}, fmt.Errorf(
|
||||||
|
"received an error after sending the request to create the media attachment: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return attachment, nil
|
||||||
|
}
|
||||||
|
|
|
@ -27,7 +27,9 @@ type CreateExecutor struct {
|
||||||
sensitive *bool
|
sensitive *bool
|
||||||
content string
|
content string
|
||||||
contentType string
|
contentType string
|
||||||
|
description string
|
||||||
fromFile string
|
fromFile string
|
||||||
|
focus string
|
||||||
inReplyTo string
|
inReplyTo string
|
||||||
language string
|
language string
|
||||||
resourceType string
|
resourceType string
|
||||||
|
@ -83,6 +85,8 @@ func NewCreateExecutor(printer *printer.Printer, config *config.Config, name, su
|
||||||
// Flags for lists
|
// Flags for lists
|
||||||
createExe.StringVar(&createExe.listTitle, flagListTitle, "", "Specify the title of the list")
|
createExe.StringVar(&createExe.listTitle, flagListTitle, "", "Specify the title of the list")
|
||||||
createExe.StringVar(&createExe.listRepliesPolicy, flagListRepliesPolicy, "list", "Specify the policy of the replies for this list (valid values are followed, list and none)")
|
createExe.StringVar(&createExe.listRepliesPolicy, flagListRepliesPolicy, "list", "Specify the policy of the replies for this list (valid values are followed, list and none)")
|
||||||
|
createExe.StringVar(&createExe.description, flagDescription, "", "The description of the media attachment that will be used as the alt-text")
|
||||||
|
createExe.StringVar(&createExe.focus, flagFocus, "", "The focus of the media file.")
|
||||||
|
|
||||||
createExe.Usage = commandUsageFunc(name, summary, createExe.FlagSet)
|
createExe.Usage = commandUsageFunc(name, summary, createExe.FlagSet)
|
||||||
|
|
||||||
|
@ -100,8 +104,9 @@ func (c *CreateExecutor) Execute() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
funcMap := map[string]func(*client.Client) error{
|
||||||
resourceList: c.createList,
|
resourceList: c.createList,
|
||||||
resourceStatus: c.createStatus,
|
resourceStatus: c.createStatus,
|
||||||
|
resourceMediaAttachment: c.createMediaAttachment,
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[c.resourceType]
|
doFunc, ok := funcMap[c.resourceType]
|
||||||
|
@ -235,3 +240,19 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CreateExecutor) createMediaAttachment(gtsClient *client.Client) error {
|
||||||
|
if c.fromFile == "" {
|
||||||
|
return FlagNotSetError{flagText: flagFromFile}
|
||||||
|
}
|
||||||
|
|
||||||
|
attachment, err := gtsClient.CreateMediaAttachment(c.fromFile, c.description, c.focus);
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create the media attachment: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.printer.PrintSuccess("Successfully created the following media attachment:")
|
||||||
|
c.printer.PrintMediaAttachment(attachment)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -16,12 +16,14 @@ const (
|
||||||
flagBrowser = "browser"
|
flagBrowser = "browser"
|
||||||
flagContentType = "content-type"
|
flagContentType = "content-type"
|
||||||
flagContent = "content"
|
flagContent = "content"
|
||||||
|
flagDescription = "description"
|
||||||
flagEnableFederation = "enable-federation"
|
flagEnableFederation = "enable-federation"
|
||||||
flagEnableLikes = "enable-likes"
|
flagEnableLikes = "enable-likes"
|
||||||
flagEnableReplies = "enable-replies"
|
flagEnableReplies = "enable-replies"
|
||||||
flagEnableReposts = "enable-reposts"
|
flagEnableReposts = "enable-reposts"
|
||||||
flagExcludeBoosts = "exclude-boosts"
|
flagExcludeBoosts = "exclude-boosts"
|
||||||
flagExcludeReplies = "exclude-replies"
|
flagExcludeReplies = "exclude-replies"
|
||||||
|
flagFocus = "focus"
|
||||||
flagFrom = "from"
|
flagFrom = "from"
|
||||||
flagFromFile = "from-file"
|
flagFromFile = "from-file"
|
||||||
flagFull = "full"
|
flagFull = "full"
|
||||||
|
|
Loading…
Reference in a new issue