checkpoint(untested): should now be able to attach media to statuses from file

This commit is contained in:
Dan Anglin 2024-08-15 02:05:42 +01:00
parent 8505d1f7ae
commit 0065e35399
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 78 additions and 1 deletions

View file

@ -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,

View file

@ -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 {