From 7638564b81a6f6f8df36d6b328cb1cc525a2cac9 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 14 Aug 2024 09:32:53 -0400 Subject: [PATCH] Make makeCall --- pkg/calls/call.go | 11 ++++++++++- pkg/gordio/server/ingest.go | 7 ------- pkg/gordio/sources/http.go | 14 ++++++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) 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")