Skip to content

Commit 989bcf3

Browse files
authored
Merge pull request #119 from Quaver/download-mapset-head
Support head method on download mapset
2 parents f669749 + 95ba6e9 commit 989bcf3

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

cmd/api/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ func initializeRoutes(engine *gin.Engine) {
171171
// Download
172172
engine.GET("/v2/download/map/:id", handlers.CreateHandler(handlers.DownloadQua))
173173
engine.GET("/v2/download/replay/:id", handlers.CreateHandler(handlers.DownloadReplay))
174-
engine.GET("/v2/download/mapset/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.DownloadMapset))
174+
engine.Match([]string{"GET", "HEAD"}, "/v2/download/mapset/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.DownloadMapset))
175175
engine.POST("/v2/download/multiplayer/:id/upload", middleware.RequireAuth, handlers.CreateHandler(handlers.UploadMultiplayerMapset))
176-
engine.GET("/v2/download/multiplayer/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.DownloadMultiplayerMapset))
176+
engine.Match([]string{"GET", "HEAD"}, "/v2/download/multiplayer/:id", middleware.RequireAuth, handlers.CreateHandler(handlers.DownloadMultiplayerMapset))
177177

178178
// Logs
179179
engine.POST("/v2/logs/crash", middleware.RequireAuth, handlers.CreateHandler(handlers.AddCrashLog))

handlers/download.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func DownloadMapset(c *gin.Context) *APIError {
7575
return APIErrorNotFound("Mapset")
7676
}
7777

78+
if isHeadRequest(c) {
79+
return setFileContentLength(c, path)
80+
}
81+
7882
if err := db.InsertMapsetDownload(&db.MapsetDownload{
7983
UserId: user.Id,
8084
MapsetId: mapset.Id,
@@ -154,6 +158,29 @@ func DownloadMultiplayerMapset(c *gin.Context) *APIError {
154158
return APIErrorNotFound("Multiplayer Mapset")
155159
}
156160

161+
if isHeadRequest(c) {
162+
return setFileContentLength(c, path)
163+
}
164+
157165
c.FileAttachment(path, fmt.Sprintf("multiplayer_%v.qp", id))
158166
return nil
159167
}
168+
169+
// Check if request is HEAD
170+
func isHeadRequest(c *gin.Context) bool {
171+
return c.Request.Method == "HEAD"
172+
}
173+
174+
func setFileContentLength(c *gin.Context, path string) *APIError {
175+
fileInfo, err := os.Stat(path)
176+
177+
if err != nil {
178+
return APIErrorServerError("Error getting file information", err)
179+
}
180+
181+
// Set Content-Length header
182+
c.Header("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
183+
return nil
184+
}
185+
186+

0 commit comments

Comments
 (0)