Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhpedersen committed Feb 15, 2024
1 parent 8fcca1c commit c8e1238
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
8 changes: 4 additions & 4 deletions internal/forms/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (b messageBuilder) buildXML() []byte {
SubmissionDatetime: now().UTC().Format("20060102150405"),
SendersCallsign: b.FormsMgr.config.MyCall,
GridSquare: b.FormsMgr.config.Locator,
DisplayForm: filename(b.Template.ViewerURI),
ReplyTemplate: filename(b.Template.ReplyTxtFileURI),
DisplayForm: filename(b.Template.DisplayFormPath),
ReplyTemplate: filename(b.Template.ReplyTemplatePath),
}
for k, v := range b.FormValues {
form.Variables = append(form.Variables, Variable{xml.Name{Local: k}, v})
Expand Down Expand Up @@ -140,7 +140,7 @@ func (b messageBuilder) buildAttachments() []*fbb.File {
}

// Add XML if a viewer is defined for this template
if b.Template.ViewerURI != "" {
if b.Template.DisplayFormPath != "" {
filename := xmlName(b.Template)
attachments = append(attachments, fbb.NewFile(filename, b.buildXML()))
}
Expand Down Expand Up @@ -324,7 +324,7 @@ func insertionTagReplacer(m *Manager, tagStart, tagEnd string) func(string) stri

// xmlName returns the user-visible filename for the message attachment that holds the form instance values
func xmlName(t Template) string {
attachmentName := filepath.Base(t.ViewerURI)
attachmentName := filepath.Base(t.DisplayFormPath)
attachmentName = strings.TrimSuffix(attachmentName, filepath.Ext(attachmentName))
attachmentName = "RMS_Express_Form_" + attachmentName + ".xml"
if len(attachmentName) > 255 {
Expand Down
2 changes: 1 addition & 1 deletion internal/forms/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestBuildXML(t *testing.T) {
Locator: "JO29PJ",
GPSd: cfg.GPSdConfig{Addr: gpsMockAddr},
}},
Template: Template{ViewerURI: "viewer.html", ReplyTxtFileURI: "reply.txt"},
Template: Template{DisplayFormPath: "viewer.html", ReplyTemplatePath: "reply.txt"},
FormValues: map[string]string{
"var1": "foo",
"var2": "bar",
Expand Down
25 changes: 15 additions & 10 deletions internal/forms/forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (m *Manager) GetFormTemplateHandler(w http.ResponseWriter, r *http.Request)
log.Printf("failed to parse requested template (%q): %v", m.rel(templatePath), err)
return
}
formPath := template.InitialURI
formPath := template.InputFormPath
if formPath == "" {
http.Error(w, "requested template does not provide a HTML form", http.StatusNotFound)
return
Expand Down Expand Up @@ -378,8 +378,8 @@ func (m *Manager) RenderForm(data []byte, composeReply bool) (string, error) {
if err != nil {
return "", fmt.Errorf("failed to read referenced reply template: %w", err)
}
submitURL := "/api/form?composereply=true&template=" + url.QueryEscape(template.Path)
return m.fillFormTemplate(template.InitialURI, submitURL, formVars)
submitURL := "/api/form?composereply=true&template=" + url.QueryEscape(m.rel(template.Path))
return m.fillFormTemplate(template.InputFormPath, submitURL, formVars)
default:
displayForm := formParams["display_form"]
if displayForm == "" {
Expand All @@ -397,11 +397,11 @@ func (m *Manager) RenderForm(data []byte, composeReply bool) (string, error) {
}
}

// ComposeTemplate composes a message from a template (tmplPath) by prompting the user through stdio.
// ComposeTemplate composes a message from a template (templatePath) by prompting the user through stdio.
//
// It combines all data needed for the whole template-based message: subject, body, and attachments.
func (m *Manager) ComposeTemplate(tmplPath string, subject string) (Message, error) {
template, err := readTemplate(tmplPath, formFilesFromPath(m.config.FormsPath))
func (m *Manager) ComposeTemplate(templatePath string, subject string) (Message, error) {
template, err := readTemplate(templatePath, formFilesFromPath(m.config.FormsPath))
if err != nil {
return Message{}, err
}
Expand All @@ -410,7 +410,7 @@ func (m *Manager) ComposeTemplate(tmplPath string, subject string) (Message, err
"subjectline": subject,
"templateversion": m.getFormsVersion(),
}
fmt.Printf("Form '%s', version: %s\n", template.Path, formValues["templateversion"])
fmt.Printf("Form '%s', version: %s\n", m.rel(template.Path), formValues["templateversion"])
return messageBuilder{
Template: template,
FormValues: formValues,
Expand Down Expand Up @@ -459,7 +459,7 @@ func (m *Manager) innerRecursiveBuildFormFolder(rootPath string, filesMap formFi
continue
}
tmpl.Path = m.rel(tmpl.Path)
if tmpl.InitialURI != "" || tmpl.ViewerURI != "" {
if tmpl.InputFormPath != "" || tmpl.DisplayFormPath != "" {
folder.Forms = append(folder.Forms, tmpl)
folder.FormCount++
}
Expand All @@ -474,6 +474,9 @@ func (m *Manager) innerRecursiveBuildFormFolder(rootPath string, filesMap formFi
}

// abs returns the absolute path of a path relative to m.FormsPath.
//
// It is primarily used to resolve template references from the web gui, which
// are relative to m.config.FormsPath.
func (m *Manager) abs(path string) string {
if filepath.IsAbs(path) {
return path
Expand All @@ -482,6 +485,8 @@ func (m *Manager) abs(path string) string {
}

// rel returns a path relative to m.FormsPath.
//
// The web gui uses this variant to reference template files.
func (m *Manager) rel(path string) string {
if !filepath.IsAbs(path) {
return path
Expand Down Expand Up @@ -521,8 +526,8 @@ func (m *Manager) gpsPos() (gpsd.Position, error) {
return conn.NextPosTimeout(3 * time.Second)
}

func (m *Manager) fillFormTemplate(tmplPath string, formDestURL string, formVars map[string]string) (string, error) {
data, err := readFile(tmplPath)
func (m *Manager) fillFormTemplate(templatePath string, formDestURL string, formVars map[string]string) (string, error) {
data, err := readFile(templatePath)
if err != nil {
return "", err
}
Expand Down
59 changes: 33 additions & 26 deletions internal/forms/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,25 @@ import (
"github.com/la5nta/pat/internal/directories"
)

// Template holds information about a Winlink form template
// Template holds information about a Winlink template.
type Template struct {
// The name of this template.
Name string `json:"name"`

// Absolute path to the template file represented by this struct.
//
// Note: The web gui uses relative paths, and for these instances the
// value is set accordingly.
Path string `json:"template_path"`

InitialURI string `json:"-"`
ViewerURI string `json:"-"`
ReplyTxtFileURI string `json:"-"`
// Absolute path to the optional HTML Form composer (aka "input form").
InputFormPath string `json:"-"`

// Absolute path to the optional HTML Form viewer (aka "display form").
DisplayFormPath string `json:"-"`

// Absolute path to the optional reply template.
ReplyTemplatePath string `json:"-"`
}

func readTemplate(path string, filesMap formFilesMap) (Template, error) {
Expand All @@ -33,31 +44,27 @@ func readTemplate(path string, filesMap formFilesMap) (Template, error) {
Name: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
Path: path,
}

resolveFileReference := func(kind string, ref string) string {
if ref == "" {
return ""
}
resolved := resolveFileReference(filesMap, filepath.Dir(template.Path), strings.TrimSpace(ref))
if resolved == "" {
debugName, _ := filepath.Rel(filepath.Join(template.Path, "..", ".."), template.Path)
debug.Printf("%s: failed to resolve referenced %s %q", debugName, kind, ref)
}
return resolved
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
switch key, value, _ := strings.Cut(scanner.Text(), ":"); key {
case "Form":
// Form: <composer>,<viewer>
files := strings.Split(value, ",")
// Extend to absolute paths and add missing html extension
for i, name := range files {
name = strings.TrimSpace(name)
files[i] = resolveFileReference(filesMap, filepath.Dir(path), name)
if files[i] == "" {
debug.Printf("%s: failed to resolve referenced file %q", template.Path, name)
}
}
template.InitialURI = files[0]
if len(files) > 1 {
template.ViewerURI = files[1]
}
case "ReplyTemplate":
name := strings.TrimSpace(value)
template.ReplyTxtFileURI = resolveFileReference(filesMap, filepath.Dir(path), name)
if template.ReplyTxtFileURI == "" {
debug.Printf("%s: failed to resolve referenced reply template file %q", template.Path, name)
continue
}
case "Form": // Form: <input form>[,<display form>]
inputForm, displayForm, _ := strings.Cut(value, ",")
template.InputFormPath = resolveFileReference("input from", inputForm)
template.DisplayFormPath = resolveFileReference("display form", displayForm)
case "ReplyTemplate": // ReplyTemplate: <template>
template.ReplyTemplatePath = resolveFileReference("reply template", value)
}
}
return template, err
Expand Down

0 comments on commit c8e1238

Please sign in to comment.