owl-blogs/web/editor/entry_form.go

172 lines
4.2 KiB
Go
Raw Permalink Normal View History

2023-06-25 20:09:27 +00:00
package editor
import (
"fmt"
2023-07-06 17:36:24 +00:00
"mime/multipart"
2023-07-08 10:03:10 +00:00
"owl-blogs/app"
2023-06-25 20:09:27 +00:00
"owl-blogs/domain/model"
"reflect"
"strings"
)
2023-07-06 17:36:24 +00:00
type HttpFormData interface {
// FormFile returns the first file by key from a MultipartForm.
FormFile(key string) (*multipart.FileHeader, error)
// FormValue returns the first value by key from a MultipartForm.
// Search is performed in QueryArgs, PostArgs, MultipartForm and FormFile in this particular order.
// Defaults to the empty string "" if the form value doesn't exist.
// If a default value is given, it will return that value if the form value does not exist.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting instead.
FormValue(key string, defaultValue ...string) string
}
2023-06-25 20:09:27 +00:00
type EditorEntryForm struct {
2023-07-08 10:03:10 +00:00
entry model.Entry
binSvc *app.BinaryService
2023-06-25 20:09:27 +00:00
}
2023-07-05 20:03:00 +00:00
type EntryFormFieldParams struct {
InputType string
2023-07-06 17:36:24 +00:00
Widget string
2023-07-05 20:03:00 +00:00
}
2023-06-25 20:09:27 +00:00
type EntryFormField struct {
Name string
2023-07-05 20:03:00 +00:00
Params EntryFormFieldParams
2023-06-25 20:09:27 +00:00
}
2023-07-08 10:03:10 +00:00
func NewEntryForm(entry model.Entry, binaryService *app.BinaryService) *EditorEntryForm {
2023-06-25 20:09:27 +00:00
return &EditorEntryForm{
2023-07-08 10:03:10 +00:00
entry: entry,
binSvc: binaryService,
2023-06-25 20:09:27 +00:00
}
}
2023-07-05 20:03:00 +00:00
func (s *EntryFormFieldParams) ApplyTag(tagKey string, tagValue string) error {
switch tagKey {
case "inputType":
s.InputType = tagValue
2023-07-06 17:36:24 +00:00
case "widget":
s.Widget = tagValue
2023-07-05 20:03:00 +00:00
default:
return fmt.Errorf("unknown tag key: %v", tagKey)
}
return nil
}
func (s *EntryFormField) Html() string {
2023-07-06 17:36:24 +00:00
html := ""
html += fmt.Sprintf("<label for=\"%v\">%v</label>\n", s.Name, s.Name)
if s.Params.InputType == "text" && s.Params.Widget == "textarea" {
html += fmt.Sprintf("<textarea name=\"%v\" id=\"%v\" rows=\"20\"></textarea>\n", s.Name, s.Name)
} else {
html += fmt.Sprintf("<input type=\"%v\" name=\"%v\" id=\"%v\" />\n", s.Params.InputType, s.Name, s.Name)
}
return html
2023-07-05 20:03:00 +00:00
}
func FieldToFormField(field reflect.StructField) (EntryFormField, error) {
formField := EntryFormField{
Name: field.Name,
Params: EntryFormFieldParams{},
}
tag := field.Tag.Get("owl")
for _, param := range strings.Split(tag, " ") {
parts := strings.Split(param, "=")
if len(parts) != 2 {
continue
}
err := formField.Params.ApplyTag(parts[0], parts[1])
if err != nil {
return EntryFormField{}, err
}
}
return formField, nil
}
func StructToFormFields(meta interface{}) ([]EntryFormField, error) {
2023-06-25 20:09:27 +00:00
entryType := reflect.TypeOf(meta).Elem()
numFields := entryType.NumField()
fields := []EntryFormField{}
for i := 0; i < numFields; i++ {
2023-07-05 20:03:00 +00:00
field, err := FieldToFormField(entryType.Field(i))
if err != nil {
return nil, err
2023-06-25 20:09:27 +00:00
}
fields = append(fields, field)
}
2023-07-05 20:03:00 +00:00
return fields, nil
}
func (s *EditorEntryForm) HtmlForm() (string, error) {
meta := s.entry.MetaData()
fields, err := StructToFormFields(meta)
if err != nil {
return "", err
}
2023-07-08 10:03:10 +00:00
html := "<form method=\"POST\" enctype=\"multipart/form-data\">\n"
2023-07-05 20:03:00 +00:00
for _, field := range fields {
html += field.Html()
}
html += "<input type=\"submit\" value=\"Submit\" />\n"
html += "</form>\n"
2023-06-25 20:09:27 +00:00
2023-07-05 20:03:00 +00:00
return html, nil
2023-06-25 20:09:27 +00:00
}
2023-07-06 17:36:24 +00:00
func (s *EditorEntryForm) Parse(ctx HttpFormData) (model.Entry, error) {
if ctx == nil {
return nil, fmt.Errorf("nil context")
}
meta := s.entry.MetaData()
metaVal := reflect.ValueOf(meta)
if metaVal.Kind() != reflect.Ptr {
return nil, fmt.Errorf("meta data is not a pointer")
}
fields, err := StructToFormFields(meta)
if err != nil {
return nil, err
}
2023-07-08 10:03:10 +00:00
for _, field := range fields {
fieldName := field.Name
if field.Params.InputType == "file" {
file, err := ctx.FormFile(fieldName)
if err != nil {
return nil, err
}
fileData, err := file.Open()
if err != nil {
return nil, err
}
defer fileData.Close()
fileBytes := make([]byte, file.Size)
_, err = fileData.Read(fileBytes)
if err != nil {
return nil, err
}
binaryFile, err := s.binSvc.Create(file.Filename, fileBytes)
if err != nil {
return nil, err
}
metaField := metaVal.Elem().FieldByName(fieldName)
if metaField.IsValid() {
metaField.SetString(binaryFile.Id)
}
} else {
formValue := ctx.FormValue(fieldName)
metaField := metaVal.Elem().FieldByName(fieldName)
if metaField.IsValid() {
metaField.SetString(formValue)
}
2023-07-06 17:36:24 +00:00
}
2023-07-08 10:03:10 +00:00
2023-07-06 17:36:24 +00:00
}
return s.entry, nil
}