Skip to content

Commit

Permalink
Uploading reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Bats committed Oct 25, 2024
1 parent f07ec0e commit 93fa85f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/relay/cmd/signalling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func main() {
if err != nil {
log.Fatal(err)
}
app := fiber.New()
app := fiber.New(fiber.Config{
BodyLimit: 50 * 1024 * 1024,
})

server, err := signalling.NewServer(config, app)

if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions packages/relay/internal/signalling/api_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package signalling

import (
"github.com/gofiber/fiber/v2"
"log"
"path"
"strings"
)

func (s *Server) setupAgentApi() {
s.app.Route("/api/agent", func(router fiber.Router) {
router.Post("/:peerName/record_upload", func(c *fiber.Ctx) error {
if s.config.RecordStorageDir == "" {
return c.Status(fiber.StatusMethodNotAllowed).SendString("Record storage is not enabled")
}

peerName := c.Params("peerName")

file, err := c.FormFile("file")
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("No file to upload")
}
if !strings.HasSuffix(file.Filename, ".webm") {
return c.Status(fiber.StatusBadRequest).SendString("File has incorrect extension")
}

log.Printf("Store agent record file %s (%dKB)", file.Filename, file.Size/1024)

destination := path.Join(s.config.RecordStorageDir, peerName+"_"+file.Filename)
if err := c.SaveFile(file, destination); err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Failed to upload file")
}

return nil
})
})
}
11 changes: 10 additions & 1 deletion packages/relay/internal/signalling/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ServerConfig struct {
ServerTLSCrtFile *string `json:"serverTLSCrtFile"`
ServerTLSKeyFile *string `json:"serverTLSKeyFile"`
RecordTimeout uint `json:"recordTimeout"`
RecordStorageDir string `json:"recordStorageDirectory"`
}

type RawServerConfig struct {
Expand All @@ -31,6 +32,7 @@ type RawServerConfig struct {
ServerTLSCrtFile *string `json:"serverTLSCrtFile"`
ServerTLSKeyFile *string `json:"serverTLSKeyFile"`
RecordTimeout uint `json:"recordTimeout"`
RecordStorageDir string `json:"RecordStorageDirectory"`
}

func LoadServerConfig() (ServerConfig, error) {
Expand All @@ -53,12 +55,18 @@ func LoadServerConfig() (ServerConfig, error) {
adminsNetworks, err := parseAdminsNetworks(rawConfig.AdminsRawNetworks)

if err != nil {
return ServerConfig{}, fmt.Errorf("can not parse admins networks, error - %w", err)
return ServerConfig{}, fmt.Errorf("can not parse admins networks, error - %v", err)
}

if rawConfig.RecordTimeout <= 0 {
rawConfig.RecordTimeout = 180000
}
if rawConfig.RecordStorageDir != "" {
err := os.MkdirAll(rawConfig.RecordStorageDir, os.ModePerm)
if err != nil {
return ServerConfig{}, fmt.Errorf("can create record directory, error - %v", err)
}
}

return ServerConfig{
PlayerCredential: rawConfig.PlayerCredential,
Expand All @@ -70,6 +78,7 @@ func LoadServerConfig() (ServerConfig, error) {
ServerTLSCrtFile: rawConfig.ServerTLSCrtFile,
ServerTLSKeyFile: rawConfig.ServerTLSKeyFile,
RecordTimeout: rawConfig.RecordTimeout,
RecordStorageDir: rawConfig.RecordStorageDir,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions packages/relay/internal/signalling/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *Server) SetupWebSocketsAndApi() {
s.setupGrabberSockets()

s.setupAdminApi()
s.setupAgentApi()
}

func (s *Server) isAdminIpAddr(addrPort string) (bool, error) {
Expand Down

0 comments on commit 93fa85f

Please sign in to comment.