Call parse first iter

This commit is contained in:
Daniel Ponte 2024-07-24 18:18:04 -04:00
parent 2a02b88822
commit 5bb16ecd73

View file

@ -5,6 +5,8 @@ import (
"io" "io"
"net/http" "net/http"
"reflect" "reflect"
"strconv"
"strings"
"time" "time"
) )
@ -29,24 +31,24 @@ type callUploadRequest struct {
TalkgroupTag string `form:"talkgroupTag"` TalkgroupTag string `form:"talkgroupTag"`
} }
func (car *callUploadRequest) Bind(r *http.Request) error {
return nil
}
func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) { func (s *Server) routeCallUpload(w http.ResponseWriter, r *http.Request) {
call := new(callUploadRequest) call := new(callUploadRequest)
err := fillFields(r, call) err := call.fill(r)
if err != nil { if err != nil {
http.Error(w, "cannot bind upload "+r.Header.Get("Content-Type")+err.Error(), 500) http.Error(w, "cannot bind upload "+err.Error(), 500)
return return
} }
fmt.Printf("%#v\n", call) w.Write([]byte(fmt.Sprintf("%#v", call)))
w.Write([]byte("yay"))
} }
func fillFields(r *http.Request, v *callUploadRequest) error { func (car *callUploadRequest) fill(r *http.Request) error {
rv := reflect.ValueOf(v).Elem() err := r.ParseMultipartForm(1024 * 1024 * 2) // 2MB
if err != nil {
return fmt.Errorf("multipart parse: %w", err)
}
rv := reflect.ValueOf(car).Elem()
rt := rv.Type() rt := rv.Type()
for i := 0; i < rv.NumField(); i++ { for i := 0; i < rv.NumField(); i++ {
@ -56,24 +58,49 @@ func fillFields(r *http.Request, v *callUploadRequest) error {
case "audio": case "audio":
file, _, err := r.FormFile(ff) file, _, err := r.FormFile(ff)
if err != nil { if err != nil {
return err return fmt.Errorf("get form file: %w", err)
} }
audioBytes, err := io.ReadAll(file) audioBytes, err := io.ReadAll(file)
if err != nil { if err != nil {
return err return fmt.Errorf("file read: %w", err)
} }
f.SetBytes(audioBytes) f.SetBytes(audioBytes)
case "dateTime": case "dateTime":
t, err := time.Parse(time.RFC3339, r.Form.Get(ff)) t, err := time.Parse(time.RFC3339, r.Form.Get(ff))
if err != nil { if err != nil {
return err return fmt.Errorf("parse time: %w", err)
} }
f.Set(reflect.ValueOf(t)) f.Set(reflect.ValueOf(t))
case "frequencies": case "frequencies":
val := strings.Trim(r.Form.Get(ff), "[]")
if val == "" {
continue
}
vals := strings.Split(val, ",")
ar := make([]int, 0, len(vals))
for _, v := range vals {
i, err := strconv.Atoi(v)
if err == nil {
ar = append(ar, i)
}
}
f.Set(reflect.ValueOf(ar))
case "frequency", "talkgroup": case "frequency", "talkgroup":
val, err := strconv.Atoi(r.Form.Get(ff))
if err != nil {
return fmt.Errorf("atoi('%s'): %w", ff, err)
}
f.SetInt(int64(val))
case "patches", "sources": case "patches", "sources":
val := strings.Trim(r.Form.Get(ff), "[]")
if val == "" {
continue
}
vals := strings.Split(val, ",")
f.Set(reflect.ValueOf(vals))
default: default:
f.SetString(r.Form.Get(ff)) f.SetString(r.Form.Get(ff))
} }