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 d6e29a7..65e88e3 100644 --- a/internal/utilities/file.go +++ b/internal/utilities/file.go @@ -5,8 +5,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) { file, err := os.Open(path) if err != nil {