From 0065e353997e166e91094fa13820ebb8fe70c4f6 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 15 Aug 2024 02:05:42 +0100 Subject: [PATCH] checkpoint(untested): should now be able to attach media to statuses from file --- internal/executor/create.go | 68 ++++++++++++++++++++++++++++++++++++- internal/utilities/file.go | 11 ++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/internal/executor/create.go b/internal/executor/create.go index c35bbcf..96f3c73 100644 --- a/internal/executor/create.go +++ b/internal/executor/create.go @@ -1,6 +1,7 @@ package executor import ( + "errors" "fmt" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" @@ -67,6 +68,71 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { sensitive bool ) + attachmentIDs := []string(c.attachmentIDs) + + if !c.mediaFiles.Empty() { + descriptionsExists := false + focusValuesExists := false + expectedLength := len(c.mediaFiles) + mediaDescriptions := make([]string, expectedLength) + + if !c.mediaDescriptions.Empty() { + descriptionsExists = true + + if !c.mediaDescriptions.ExpectedLength(expectedLength) { + return errors.New("the number of media descriptions does not match the number of media files") + } + } + + if !c.mediaFocusValues.Empty() { + focusValuesExists = true + + if !c.mediaFocusValues.ExpectedLength(expectedLength) { + return errors.New("the number of media focus values does not match the number of media files") + } + } + + if descriptionsExists { + for ind := 0; ind < expectedLength; ind++ { + content, err := utilities.ReadContents(c.mediaDescriptions[ind]) + if err != nil { + return fmt.Errorf("unable to read the contents from %s: %w", c.mediaDescriptions[ind], err) + } + + mediaDescriptions[ind] = content + } + } + + for ind := 0; ind < expectedLength; ind++ { + var ( + mediaFile string + description string + focus string + ) + + mediaFile = c.mediaFiles[ind] + + if descriptionsExists { + description = mediaDescriptions[ind] + } + + if focusValuesExists { + focus = c.mediaFocusValues[ind] + } + + attachment, err := gtsClient.CreateMediaAttachment( + mediaFile, + description, + focus, + ) + if err != nil { + return fmt.Errorf("unable to create the media attachment for %s: %w", mediaFile, err) + } + + attachmentIDs = append(attachmentIDs, attachment.ID) + } + } + switch { case c.content != "": content = c.content @@ -76,7 +142,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error { return fmt.Errorf("unable to get the status contents from %q: %w", c.fromFile, err) } default: - if c.attachmentIDs.Empty() { + if len(attachmentIDs) == 0 { // TODO: revisit this error type return EmptyContentError{ ResourceType: resourceStatus, diff --git a/internal/utilities/file.go b/internal/utilities/file.go index eb80ccd..328c6dc 100644 --- a/internal/utilities/file.go +++ b/internal/utilities/file.go @@ -4,8 +4,19 @@ import ( "errors" "fmt" "os" + "strings" ) +const filePrefix string = "file@" + +func ReadContents(text string) (string, error) { + if !strings.HasPrefix(text, filePrefix) { + return text, nil + } + + return ReadFile(strings.TrimPrefix(text, filePrefix)) +} + func ReadFile(path string) (string, error) { data, err := os.ReadFile(path) if err != nil {