Skip to content

Commit

Permalink
Support attached_textN/attached_fileN
Browse files Browse the repository at this point in the history
Issue #437
  • Loading branch information
martinhpedersen committed Feb 25, 2024
1 parent 46cab2b commit aa1e96f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions internal/forms/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,24 @@ func (b messageBuilder) buildXML() []byte {

func (b messageBuilder) buildAttachments() []*fbb.File {
var attachments []*fbb.File

// Add FormData.txt attachment if defined by the form
if v, ok := b.FormValues["attached_text"]; ok {
delete(b.FormValues, "attached_text") // Should not be included in the XML.
attachments = append(attachments, fbb.NewFile("FormData.txt", []byte(v)))
// Add optional text attachments defined by some forms as form values
// pairs in the format attached_textN/attached_fileN (N=0 is omitted).
for k := range b.FormValues {
if !strings.HasPrefix(k, "attached_text") {
continue
}
textKey := k
text := b.FormValues[textKey]
nameKey := strings.Replace(k, "attached_text", "attached_file", 1)
name, ok := b.FormValues[nameKey]
if !ok {
debug.Printf("%s defined, but corresponding filename element %q is not set", textKey, nameKey)
name = "FormData.txt" // Fallback (better than nothing)
}
attachments = append(attachments, fbb.NewFile(name, []byte(text)))
delete(b.FormValues, nameKey)
delete(b.FormValues, textKey)
}

// Add XML if a viewer is defined for this template
if b.Template.DisplayFormPath != "" {
filename := xmlName(b.Template)
Expand Down

0 comments on commit aa1e96f

Please sign in to comment.