Skip to content

Commit d71606b

Browse files
authored
Merge pull request #1 from UDL-TF/feat/migrate-get-demo-endpoint
feat: Migrate get-demo endpoint from UDLDeployer
2 parents 3485c9b + 6a11592 commit d71606b

8 files changed

Lines changed: 101 additions & 236 deletions

File tree

internal/handler/demo.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
"strconv"
8+
9+
"github.com/UDL-TF/UnitedAPI/internal/response"
10+
"github.com/gin-gonic/gin"
11+
)
12+
13+
// GetDemo handles GET /api/v1/demos
14+
// Query params: match_id, round_id
15+
// Returns the demo file for the given match and round
16+
func GetDemo(c *gin.Context) {
17+
matchID := c.Query("match_id")
18+
roundID := c.Query("round_id")
19+
20+
if matchID == "" || roundID == "" {
21+
response.BadRequest(c, "Missing match_id or round_id")
22+
return
23+
}
24+
25+
// Force matchID and roundID to be numbers for security
26+
matchIDNum, err := strconv.Atoi(matchID)
27+
if err != nil {
28+
response.BadRequest(c, "Match ID is not a number")
29+
return
30+
}
31+
32+
roundIDNum, err := strconv.Atoi(roundID)
33+
if err != nil {
34+
response.BadRequest(c, "Round ID is not a number")
35+
return
36+
}
37+
38+
// Get demo path from environment variable or use default
39+
uploadPath := os.Getenv("DEMO_PATH")
40+
if uploadPath == "" {
41+
uploadPath = "./demo"
42+
}
43+
44+
// Find the file with the given match ID and round ID using regex
45+
filePattern := fmt.Sprintf(`match-%d-round-%d`, matchIDNum, roundIDNum)
46+
files, err := os.ReadDir(uploadPath)
47+
if err != nil {
48+
response.InternalServerError(c, "Error reading demo directory")
49+
return
50+
}
51+
52+
var demoFile string
53+
var maxSize int64
54+
55+
for _, file := range files {
56+
if !file.IsDir() && matchRegex(file.Name(), filePattern) {
57+
fileInfo, err := file.Info()
58+
if err != nil {
59+
continue
60+
}
61+
// Get the largest matching file
62+
if fileInfo.Size() > maxSize {
63+
maxSize = fileInfo.Size()
64+
demoFile = file.Name()
65+
}
66+
}
67+
}
68+
69+
if demoFile == "" {
70+
response.NotFound(c, "Demo file not found")
71+
return
72+
}
73+
74+
fmt.Printf("Downloading demo file: %s\n", demoFile)
75+
76+
// Set the original file name in the response header
77+
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", demoFile))
78+
c.File(fmt.Sprintf("%s/%s", uploadPath, demoFile))
79+
}
80+
81+
// matchRegex checks if a string matches a regex pattern
82+
func matchRegex(str, pattern string) bool {
83+
pattern = regexp.QuoteMeta(pattern)
84+
matched, _ := regexp.MatchString(pattern, str)
85+
return matched
86+
}

internal/handler/user.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

internal/model/user.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

internal/repository/user_repository.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

internal/router/v1/demos.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package v1
2+
3+
import (
4+
"github.com/UDL-TF/UnitedAPI/internal/handler"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
// RegisterDemoRoutes registers demo-related routes
9+
func RegisterDemoRoutes(rg *gin.RouterGroup) {
10+
demos := rg.Group("/demos")
11+
{
12+
demos.GET("", handler.GetDemo)
13+
}
14+
}

internal/router/v1/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func RegisterRoutes(rg *gin.RouterGroup) {
1010
v1 := rg.Group("/v1")
1111
v1.Use(middleware.Logger())
1212
{
13-
RegisterUserRoutes(v1)
13+
RegisterDemoRoutes(v1)
1414
RegisterProtectedRoutes(v1)
1515
RegisterAdminRoutes(v1)
1616
}

internal/router/v1/users.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

internal/service/user_service.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)