diff --git a/pkg/calls/call.go b/pkg/calls/call.go index beac2d3..c5e569b 100644 --- a/pkg/calls/call.go +++ b/pkg/calls/call.go @@ -50,6 +50,15 @@ type Call struct { TalkgroupTag *string } +func Make(call *Call) (*Call, error) { + err := call.computeLength() + if err != nil { + return nil, err + } + + return call, nil +} + func toInt64Slice(s []int) []int64 { n := make([]int64, len(s)) for i := range s { @@ -85,7 +94,7 @@ func (c *Call) ToPB() *pb.Call { } } -func (c *Call) ComputeLength() (err error) { +func (c *Call) computeLength() (err error) { var td time.Duration switch c.AudioType { diff --git a/pkg/gordio/server/ingest.go b/pkg/gordio/server/ingest.go index e609e0b..26e8133 100644 --- a/pkg/gordio/server/ingest.go +++ b/pkg/gordio/server/ingest.go @@ -4,15 +4,8 @@ import ( "context" "dynatron.me/x/stillbox/pkg/calls" - - "github.com/rs/zerolog/log" ) func (s *Server) Ingest(ctx context.Context, call *calls.Call) { - err := call.ComputeLength() - if err != nil { - log.Error().Err(err).Msg("compute length failed") - } - s.sinks.EmitCall(context.Background(), call) } diff --git a/pkg/gordio/sources/http.go b/pkg/gordio/sources/http.go index 8f2c938..cd574d8 100644 --- a/pkg/gordio/sources/http.go +++ b/pkg/gordio/sources/http.go @@ -73,8 +73,8 @@ func (car *callUploadRequest) mimeType() string { return "" } -func (car *callUploadRequest) toCall(submitter auth.UserID) *calls.Call { - return &calls.Call{ +func (car *callUploadRequest) toCall(submitter auth.UserID) (*calls.Call, error) { + return calls.Make(&calls.Call{ Submitter: &submitter, System: car.System, Talkgroup: car.Talkgroup, @@ -89,7 +89,7 @@ func (car *callUploadRequest) toCall(submitter auth.UserID) *calls.Call { TalkgroupTag: common.PtrOrNull(car.TalkgroupTag), TalkgroupGroup: common.PtrOrNull(car.TalkgroupGroup), Source: car.Source, - } + }) } func (h *RdioHTTP) routeCallUpload(w http.ResponseWriter, r *http.Request) { @@ -120,7 +120,13 @@ func (h *RdioHTTP) routeCallUpload(w http.ResponseWriter, r *http.Request) { return } - h.ing.Ingest(ctx, cur.toCall(*submitter)) + call, err := cur.toCall(*submitter) + if err != nil { + log.Error().Err(err).Msg("toCall failed") + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + h.ing.Ingest(ctx, call) log.Info().Int("system", cur.System).Int("tgid", cur.Talkgroup).Msg("ingested")